| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package k8s_state |
| 4 | |
| 5 | import ( |
| 6 | "sync" |
| 7 | "time" |
| 8 | |
| 9 | appsv1 "k8s.io/api/apps/v1" |
| 10 | batchv1 "k8s.io/api/batch/v1" |
| 11 | corev1 "k8s.io/api/core/v1" |
| 12 | ) |
| 13 | |
| 14 | func newKubeState() *kubeState { |
| 15 | return &kubeState{ |
| 16 | Mutex: &sync.Mutex{}, |
| 17 | nodes: make(map[string]*nodeState), |
| 18 | pods: make(map[string]*podState), |
| 19 | deployments: make(map[string]*deploymentState), |
| 20 | cronJobs: make(map[string]*cronJobState), |
| 21 | jobs: make(map[string]*jobState), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func newNodeState() *nodeState { |
| 26 | return &nodeState{ |
| 27 | new: true, |
| 28 | labels: make(map[string]string), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func newPodState() *podState { |
| 33 | return &podState{ |
| 34 | new: true, |
| 35 | labels: make(map[string]string), |
| 36 | initContainers: make(map[string]*containerState), |
| 37 | containers: make(map[string]*containerState), |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func newContainerState() *containerState { |
| 42 | return &containerState{ |
| 43 | new: true, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func newDeploymentState() *deploymentState { |
| 48 | return &deploymentState{ |
| 49 | new: true, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func newCronJobState() *cronJobState { |
| 54 | return &cronJobState{ |
| 55 | new: true, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func newJobState() *jobState { |
| 60 | return &jobState{ |
| 61 | new: true, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | type kubeState struct { |
| 66 | *sync.Mutex |
| 67 | nodes map[string]*nodeState |
| 68 | pods map[string]*podState |
| 69 | deployments map[string]*deploymentState |
| 70 | cronJobs map[string]*cronJobState |
| 71 | jobs map[string]*jobState |
| 72 | } |
| 73 | |
| 74 | type ( |
| 75 | nodeState struct { |
| 76 | new bool |
| 77 | deleted bool |
| 78 | |
| 79 | name string |
| 80 | unSchedulable bool |
| 81 | labels map[string]string |
| 82 | creationTime time.Time |
| 83 | allocatableCPU int64 |
| 84 | allocatableMem int64 |
| 85 | allocatablePods int64 |
| 86 | conditions []corev1.NodeCondition |
| 87 | |
| 88 | stats nodeStateStats |
| 89 | } |
| 90 | nodeStateStats struct { |
| 91 | reqCPU int64 |
| 92 | limitCPU int64 |
| 93 | reqMem int64 |
| 94 | limitMem int64 |
| 95 | pods int64 |
| 96 | |
| 97 | podsCondPodReady int64 |
| 98 | podsCondPodScheduled int64 |
| 99 | podsCondPodInitialized int64 |
| 100 | podsCondContainersReady int64 |
| 101 | |
| 102 | podsReadinessReady int64 |
| 103 | podsReadinessUnready int64 |
| 104 | |
| 105 | podsPhaseRunning int64 |
| 106 | podsPhaseFailed int64 |
| 107 | podsPhaseSucceeded int64 |
| 108 | podsPhasePending int64 |
| 109 | |
| 110 | containers int64 |
| 111 | initContainers int64 |
| 112 | initContStateRunning int64 |
| 113 | initContStateWaiting int64 |
| 114 | initContStateTerminated int64 |
| 115 | contStateRunning int64 |
| 116 | contStateWaiting int64 |
| 117 | contStateTerminated int64 |
| 118 | } |
| 119 | ) |
| 120 | |
| 121 | func (ns *nodeState) id() string { return ns.name } |
| 122 | func (ns *nodeState) resetStats() { ns.stats = nodeStateStats{} } |
| 123 | |
| 124 | type ( |
| 125 | podState struct { |
| 126 | new bool |
| 127 | deleted bool |
| 128 | unscheduled bool |
| 129 | |
| 130 | name string |
| 131 | nodeName string |
| 132 | namespace string |
| 133 | uid string |
| 134 | labels map[string]string |
| 135 | controllerKind string |
| 136 | controllerName string |
| 137 | qosClass string |
| 138 | creationTime time.Time |
| 139 | reqCPU int64 |
| 140 | reqMem int64 |
| 141 | limitCPU int64 |
| 142 | limitMem int64 |
| 143 | // https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-conditions |
| 144 | condPodScheduled corev1.ConditionStatus |
| 145 | condContainersReady corev1.ConditionStatus |
| 146 | condPodInitialized corev1.ConditionStatus |
| 147 | condPodReady corev1.ConditionStatus |
| 148 | // https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase |
| 149 | phase corev1.PodPhase |
| 150 | statusReason string |
| 151 | |
| 152 | initContainers map[string]*containerState |
| 153 | containers map[string]*containerState |
| 154 | } |
| 155 | ) |
| 156 | |
| 157 | func (ps podState) id() string { return ps.namespace + "_" + ps.name } |
| 158 | |
| 159 | type containerState struct { |
| 160 | new bool |
| 161 | |
| 162 | name string |
| 163 | uid string |
| 164 | |
| 165 | podName string |
| 166 | nodeName string |
| 167 | namespace string |
| 168 | |
| 169 | ready bool |
| 170 | restarts int64 |
| 171 | stateRunning bool |
| 172 | stateWaiting bool |
| 173 | stateTerminated bool |
| 174 | |
| 175 | waitingReason string |
| 176 | terminatedReason string |
| 177 | } |
| 178 | |
| 179 | type deploymentState struct { |
| 180 | new bool |
| 181 | deleted bool |
| 182 | |
| 183 | uid string |
| 184 | name string |
| 185 | namespace string |
| 186 | |
| 187 | conditions []appsv1.DeploymentCondition |
| 188 | |
| 189 | creationTime time.Time |
| 190 | replicas int64 // desired |
| 191 | availableReplicas int64 // current |
| 192 | readyReplicas int64 |
| 193 | } |
| 194 | |
| 195 | func (ds deploymentState) id() string { return ds.namespace + "_" + ds.name } |
| 196 | |
| 197 | type cronJobState struct { |
| 198 | new bool |
| 199 | deleted bool |
| 200 | |
| 201 | uid string |
| 202 | name string |
| 203 | namespace string |
| 204 | creationTime time.Time |
| 205 | |
| 206 | suspend bool |
| 207 | lastScheduleTime *time.Time |
| 208 | lastSuccessfulTime *time.Time |
| 209 | } |
| 210 | |
| 211 | func (cs cronJobState) id() string { return cs.namespace + "_" + cs.name } |
| 212 | |
| 213 | type jobState struct { |
| 214 | new bool |
| 215 | deleted bool |
| 216 | |
| 217 | uid string |
| 218 | name string |
| 219 | namespace string |
| 220 | creationTime time.Time |
| 221 | |
| 222 | controller struct { |
| 223 | kind string |
| 224 | name string |
| 225 | uid string |
| 226 | } |
| 227 | |
| 228 | conditions []batchv1.JobCondition |
| 229 | |
| 230 | startTime *time.Time |
| 231 | completionTime *time.Time |
| 232 | active int32 |
| 233 | } |