1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
// SPDX-License-Identifier: GPL-3.0-or-later
package k8s_state
import (
"errors"
"fmt"
"slices"
"time"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
corev1 "k8s.io/api/core/v1"
)
const precision = 1000
var (
podStatusReasons = []string{
"Evicted",
"NodeAffinity",
"NodeLost",
"Shutdown",
"UnexpectedAdmissionError",
"Other",
}
containerWaitingStateReasons = []string{
"ContainerCreating",
"CrashLoopBackOff",
"CreateContainerConfigError",
"CreateContainerError",
"ErrImagePull",
"ImagePullBackOff",
"InvalidImageName",
"PodInitializing",
"Other",
}
containerTerminatedStateReasons = []string{
"Completed",
"ContainerCannotRun",
"DeadlineExceeded",
"Error",
"Evicted",
"OOMKilled",
"Other",
}
)
var (
nodeConditionStatuses = []string{
"Ready",
"DiskPressure",
"MemoryPressure",
"NetworkUnavailable",
"PIDPressure",
}
)
func (ks *KubeState) collect() (map[string]int64, error) {
if ks.discoverer == nil {
return nil, errors.New("nil discoverer")
}
ks.once.Do(func() {
ks.startTime = time.Now()
in := make(chan resource)
ks.wg.Add(1)
go func() { defer ks.wg.Done(); ks.runUpdateState(in) }()
ks.wg.Add(1)
go func() { defer ks.wg.Done(); ks.discoverer.run(ks.ctx, in) }()
ks.kubeClusterID = ks.getKubeClusterID()
ks.kubeClusterName = ks.getKubeClusterName()
if chart := ks.Charts().Get(discoveryStatusChart.ID); chart != nil {
chart.Labels = []module.Label{
{Key: labelKeyClusterID, Value: ks.kubeClusterID, Source: module.LabelSourceK8s},
{Key: labelKeyClusterName, Value: ks.kubeClusterName, Source: module.LabelSourceK8s},
}
}
})
mx := map[string]int64{
"discovery_node_discoverer_state": 1,
"discovery_pod_discoverer_state": 1,
}
if !ks.discoverer.ready() || time.Since(ks.startTime) < ks.initDelay {
return mx, nil
}
ks.state.Lock()
defer ks.state.Unlock()
ks.collectKubeState(mx)
return mx, nil
}
func (ks *KubeState) collectKubeState(mx map[string]int64) {
for _, ns := range ks.state.nodes {
ns.resetStats()
}
ks.collectPodsState(mx)
ks.collectNodesState(mx)
}
func (ks *KubeState) collectPodsState(mx map[string]int64) {
now := time.Now()
for _, ps := range ks.state.pods {
// Skip cronjobs (each of them is a unique container because the name contains hash)
// to avoid overwhelming Netdata with high cardinality metrics.
// Related issue https://github.com/netdata/netdata/issues/16412
if ps.controllerKind == "Job" {
continue
}
if ps.deleted {
delete(ks.state.pods, podSource(ps.namespace, ps.name))
ks.removePodCharts(ps)
continue
}
if ps.new {
ps.new = false
ks.addPodCharts(ps)
ps.unscheduled = ps.nodeName == ""
} else if ps.unscheduled && ps.nodeName != "" {
ps.unscheduled = false
ks.updatePodChartsNodeLabel(ps)
}
ns := ks.state.nodes[nodeSource(ps.nodeName)]
if ns != nil {
ns.stats.pods++
ns.stats.reqCPU += ps.reqCPU
ns.stats.limitCPU += ps.limitCPU
ns.stats.reqMem += ps.reqMem
ns.stats.limitMem += ps.limitMem
ns.stats.podsCondPodReady += condStatusToInt(ps.condPodReady)
ns.stats.podsCondPodScheduled += condStatusToInt(ps.condPodScheduled)
ns.stats.podsCondPodInitialized += condStatusToInt(ps.condPodInitialized)
ns.stats.podsCondContainersReady += condStatusToInt(ps.condContainersReady)
ns.stats.podsReadinessReady += boolToInt(ps.condPodReady == corev1.ConditionTrue)
ns.stats.podsReadinessUnready += boolToInt(ps.condPodReady != corev1.ConditionTrue)
ns.stats.podsPhasePending += boolToInt(ps.phase == corev1.PodPending)
ns.stats.podsPhaseRunning += boolToInt(ps.phase == corev1.PodRunning)
ns.stats.podsPhaseSucceeded += boolToInt(ps.phase == corev1.PodSucceeded)
ns.stats.podsPhaseFailed += boolToInt(ps.phase == corev1.PodFailed)
for _, cs := range ps.initContainers {
ns.stats.initContainers++
ns.stats.initContStateRunning += boolToInt(cs.stateRunning)
ns.stats.initContStateWaiting += boolToInt(cs.stateWaiting)
ns.stats.initContStateTerminated += boolToInt(cs.stateTerminated)
}
for _, cs := range ps.containers {
ns.stats.containers++
ns.stats.contStateRunning += boolToInt(cs.stateRunning)
ns.stats.contStateWaiting += boolToInt(cs.stateWaiting)
ns.stats.contStateTerminated += boolToInt(cs.stateTerminated)
}
}
px := fmt.Sprintf("pod_%s_", ps.id())
mx[px+"cond_podready"] = condStatusToInt(ps.condPodReady)
mx[px+"cond_podscheduled"] = condStatusToInt(ps.condPodScheduled)
mx[px+"cond_podinitialized"] = condStatusToInt(ps.condPodInitialized)
mx[px+"cond_containersready"] = condStatusToInt(ps.condContainersReady)
mx[px+"phase_running"] = boolToInt(ps.phase == corev1.PodRunning)
mx[px+"phase_failed"] = boolToInt(ps.phase == corev1.PodFailed)
mx[px+"phase_succeeded"] = boolToInt(ps.phase == corev1.PodSucceeded)
mx[px+"phase_pending"] = boolToInt(ps.phase == corev1.PodPending)
mx[px+"age"] = int64(now.Sub(ps.creationTime).Seconds())
for _, v := range podStatusReasons {
mx[px+"status_reason_"+v] = 0
}
if v := ps.statusReason; v != "" {
if !slices.Contains(podStatusReasons, v) {
v = "Other"
}
mx[px+"status_reason_"+v] = 1
}
mx[px+"cpu_requests_used"] = ps.reqCPU
mx[px+"cpu_limits_used"] = ps.limitCPU
mx[px+"mem_requests_used"] = ps.reqMem
mx[px+"mem_limits_used"] = ps.limitMem
mx[px+"init_containers"] = int64(len(ps.initContainers))
mx[px+"containers"] = int64(len(ps.containers))
mx[px+"init_containers_state_running"] = 0
mx[px+"init_containers_state_waiting"] = 0
mx[px+"init_containers_state_terminated"] = 0
for _, cs := range ps.initContainers {
mx[px+"init_containers_state_running"] += boolToInt(cs.stateRunning)
mx[px+"init_containers_state_waiting"] += boolToInt(cs.stateWaiting)
mx[px+"init_containers_state_terminated"] += boolToInt(cs.stateTerminated)
}
mx[px+"containers_state_running"] = 0
mx[px+"containers_state_waiting"] = 0
mx[px+"containers_state_terminated"] = 0
for _, cs := range ps.containers {
if cs.new {
cs.new = false
ks.addContainerCharts(ps, cs)
}
mx[px+"containers_state_running"] += boolToInt(cs.stateRunning)
mx[px+"containers_state_waiting"] += boolToInt(cs.stateWaiting)
mx[px+"containers_state_terminated"] += boolToInt(cs.stateTerminated)
ppx := fmt.Sprintf("%scontainer_%s_", px, cs.name)
mx[ppx+"state_running"] = boolToInt(cs.stateRunning)
mx[ppx+"state_waiting"] = boolToInt(cs.stateWaiting)
mx[ppx+"state_terminated"] = boolToInt(cs.stateTerminated)
mx[ppx+"readiness"] = boolToInt(cs.ready)
mx[ppx+"restarts"] = cs.restarts
for _, v := range containerWaitingStateReasons {
mx[ppx+"state_waiting_reason_"+v] = 0
}
if v := cs.waitingReason; v != "" {
if !slices.Contains(containerWaitingStateReasons, v) {
v = "Other"
}
mx[ppx+"state_waiting_reason_"+v] = 1
}
for _, v := range containerTerminatedStateReasons {
mx[ppx+"state_terminated_reason_"+v] = 0
}
if v := cs.terminatedReason; v != "" {
if !slices.Contains(containerTerminatedStateReasons, v) {
v = "Other"
}
mx[ppx+"state_terminated_reason_"+v] = 1
}
}
}
}
func (ks *KubeState) collectNodesState(mx map[string]int64) {
now := time.Now()
for _, ns := range ks.state.nodes {
if ns.deleted {
delete(ks.state.nodes, nodeSource(ns.name))
ks.removeNodeCharts(ns)
continue
}
if ns.new {
ns.new = false
ks.addNodeCharts(ns)
}
px := fmt.Sprintf("node_%s_", ns.id())
for _, v := range nodeConditionStatuses {
mx[px+"cond_"+v] = 0
}
for _, v := range ns.conditions {
mx[px+"cond_"+string(v.Type)] = condStatusToInt(v.Status)
}
mx[px+"age"] = int64(now.Sub(ns.creationTime).Seconds())
mx[px+"alloc_pods_util"] = calcPercentage(ns.stats.pods, ns.allocatablePods)
mx[px+"pods_readiness_ready"] = ns.stats.podsReadinessReady
mx[px+"pods_readiness_unready"] = ns.stats.podsReadinessUnready
mx[px+"pods_readiness"] = calcPercentage(ns.stats.podsReadinessReady, ns.stats.pods)
mx[px+"pods_phase_running"] = ns.stats.podsPhaseRunning
mx[px+"pods_phase_failed"] = ns.stats.podsPhaseFailed
mx[px+"pods_phase_succeeded"] = ns.stats.podsPhaseSucceeded
mx[px+"pods_phase_pending"] = ns.stats.podsPhasePending
mx[px+"pods_cond_podready"] = ns.stats.podsCondPodReady
mx[px+"pods_cond_podscheduled"] = ns.stats.podsCondPodScheduled
mx[px+"pods_cond_podinitialized"] = ns.stats.podsCondPodInitialized
mx[px+"pods_cond_containersready"] = ns.stats.podsCondContainersReady
mx[px+"pods_cond_containersready"] = ns.stats.podsCondContainersReady
mx[px+"schedulability_schedulable"] = boolToInt(!ns.unSchedulable)
mx[px+"schedulability_unschedulable"] = boolToInt(ns.unSchedulable)
mx[px+"alloc_pods_available"] = ns.allocatablePods - ns.stats.pods
mx[px+"alloc_pods_allocated"] = ns.stats.pods
mx[px+"alloc_cpu_requests_util"] = calcPercentage(ns.stats.reqCPU, ns.allocatableCPU)
mx[px+"alloc_cpu_limits_util"] = calcPercentage(ns.stats.limitCPU, ns.allocatableCPU)
mx[px+"alloc_mem_requests_util"] = calcPercentage(ns.stats.reqMem, ns.allocatableMem)
mx[px+"alloc_mem_limits_util"] = calcPercentage(ns.stats.limitMem, ns.allocatableMem)
mx[px+"alloc_cpu_requests_used"] = ns.stats.reqCPU
mx[px+"alloc_cpu_limits_used"] = ns.stats.limitCPU
mx[px+"alloc_mem_requests_used"] = ns.stats.reqMem
mx[px+"alloc_mem_limits_used"] = ns.stats.limitMem
mx[px+"init_containers"] = ns.stats.initContainers
mx[px+"containers"] = ns.stats.containers
mx[px+"containers_state_running"] = ns.stats.contStateRunning
mx[px+"containers_state_waiting"] = ns.stats.contStateWaiting
mx[px+"containers_state_terminated"] = ns.stats.contStateTerminated
mx[px+"init_containers_state_running"] = ns.stats.initContStateRunning
mx[px+"init_containers_state_waiting"] = ns.stats.initContStateWaiting
mx[px+"init_containers_state_terminated"] = ns.stats.initContStateTerminated
}
}
func boolToInt(v bool) int64 {
if v {
return 1
}
return 0
}
func condStatusToInt(cs corev1.ConditionStatus) int64 {
switch cs {
case corev1.ConditionFalse:
return 0
case corev1.ConditionTrue:
return 1
case corev1.ConditionUnknown:
return 0
default:
return 0
}
}
func calcPercentage(value, total int64) int64 {
if total == 0 {
return 0
}
return int64(float64(value) / float64(total) * 100 * precision)
}
|