forked from kubernetes/kube-state-metrics
/
node.go
335 lines (302 loc) · 11.6 KB
/
node.go
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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package collectors
import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/kube-state-metrics/pkg/constant"
"k8s.io/kube-state-metrics/pkg/options"
"k8s.io/kubernetes/pkg/apis/core/v1/helper"
)
var (
descNodeLabelsName = "kube_node_labels"
descNodeLabelsHelp = "Kubernetes labels converted to Prometheus labels."
descNodeLabelsDefaultLabels = []string{"node"}
descNodeInfo = prometheus.NewDesc(
"kube_node_info",
"Information about a cluster node.",
append(descNodeLabelsDefaultLabels,
"kernel_version",
"os_image",
"container_runtime_version",
"kubelet_version",
"kubeproxy_version",
"provider_id"),
nil,
)
descNodeCreated = prometheus.NewDesc(
"kube_node_created",
"Unix creation timestamp",
descNodeLabelsDefaultLabels,
nil,
)
descNodeLabels = prometheus.NewDesc(
descNodeLabelsName,
descNodeLabelsHelp,
descNodeLabelsDefaultLabels,
nil,
)
descNodeSpecUnschedulable = prometheus.NewDesc(
"kube_node_spec_unschedulable",
"Whether a node can schedule new pods.",
descNodeLabelsDefaultLabels,
nil,
)
descNodeSpecTaint = prometheus.NewDesc(
"kube_node_spec_taint",
"The taint of a cluster node.",
append(descNodeLabelsDefaultLabels, "key", "value", "effect"),
nil,
)
descNodeStatusCondition = prometheus.NewDesc(
"kube_node_status_condition",
"The condition of a cluster node.",
append(descNodeLabelsDefaultLabels, "condition", "status"),
nil,
)
descNodeStatusPhase = prometheus.NewDesc(
"kube_node_status_phase",
"The phase the node is currently in.",
append(descNodeLabelsDefaultLabels, "phase"),
nil,
)
descNodeStatusCapacity = prometheus.NewDesc(
"kube_node_status_capacity",
"The capacity for different resources of a node.",
append(descNodeLabelsDefaultLabels, "resource", "unit"),
nil,
)
descNodeStatusCapacityPods = prometheus.NewDesc(
"kube_node_status_capacity_pods",
"The total pod resources of the node.",
descNodeLabelsDefaultLabels,
nil,
)
descNodeStatusCapacityCPU = prometheus.NewDesc(
"kube_node_status_capacity_cpu_cores",
"The total CPU resources of the node.",
descNodeLabelsDefaultLabels,
nil,
)
descNodeStatusCapacityMemory = prometheus.NewDesc(
"kube_node_status_capacity_memory_bytes",
"The total memory resources of the node.",
descNodeLabelsDefaultLabels,
nil,
)
descNodeStatusAllocatable = prometheus.NewDesc(
"kube_node_status_allocatable",
"The allocatable for different resources of a node that are available for scheduling.",
append(descNodeLabelsDefaultLabels, "resource", "unit"),
nil,
)
descNodeStatusAllocatablePods = prometheus.NewDesc(
"kube_node_status_allocatable_pods",
"The pod resources of a node that are available for scheduling.",
descNodeLabelsDefaultLabels,
nil,
)
descNodeStatusAllocatableCPU = prometheus.NewDesc(
"kube_node_status_allocatable_cpu_cores",
"The CPU resources of a node that are available for scheduling.",
descNodeLabelsDefaultLabels,
nil,
)
descNodeStatusAllocatableMemory = prometheus.NewDesc(
"kube_node_status_allocatable_memory_bytes",
"The memory resources of a node that are available for scheduling.",
descNodeLabelsDefaultLabels,
nil,
)
)
type NodeLister func() (v1.NodeList, error)
func (l NodeLister) List() (v1.NodeList, error) {
return l()
}
func RegisterNodeCollector(registry prometheus.Registerer, kubeClient kubernetes.Interface, namespaces []string, opts *options.Options) {
client := kubeClient.CoreV1().RESTClient()
glog.Infof("collect node with %s", client.APIVersion())
ninfs := NewSharedInformerList(client, "nodes", []string{metav1.NamespaceAll}, &v1.Node{})
nodeLister := NodeLister(func() (machines v1.NodeList, err error) {
for _, ninf := range *ninfs {
for _, m := range ninf.GetStore().List() {
machines.Items = append(machines.Items, *(m.(*v1.Node)))
}
}
return machines, nil
})
registry.MustRegister(&nodeCollector{store: nodeLister, opts: opts})
ninfs.Run(context.Background().Done())
}
type nodeStore interface {
List() (v1.NodeList, error)
}
// nodeCollector collects metrics about all nodes in the cluster.
type nodeCollector struct {
store nodeStore
opts *options.Options
}
// Describe implements the prometheus.Collector interface.
func (nc *nodeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- descNodeInfo
ch <- descNodeCreated
ch <- descNodeLabels
ch <- descNodeSpecUnschedulable
ch <- descNodeSpecTaint
ch <- descNodeStatusCondition
ch <- descNodeStatusPhase
ch <- descNodeStatusCapacity
ch <- descNodeStatusAllocatable
if !nc.opts.DisableNodeNonGenericResourceMetrics {
ch <- descNodeStatusCapacityCPU
ch <- descNodeStatusCapacityMemory
ch <- descNodeStatusCapacityPods
ch <- descNodeStatusAllocatableCPU
ch <- descNodeStatusAllocatableMemory
ch <- descNodeStatusAllocatablePods
}
}
// Collect implements the prometheus.Collector interface.
func (nc *nodeCollector) Collect(ch chan<- prometheus.Metric) {
nodes, err := nc.store.List()
if err != nil {
ScrapeErrorTotalMetric.With(prometheus.Labels{"resource": "node"}).Inc()
glog.Errorf("listing nodes failed: %s", err)
return
}
ScrapeErrorTotalMetric.With(prometheus.Labels{"resource": "node"}).Add(0)
ResourcesPerScrapeMetric.With(prometheus.Labels{"resource": "node"}).Observe(float64(len(nodes.Items)))
for _, n := range nodes.Items {
nc.collectNode(ch, n)
}
glog.V(4).Infof("collected %d nodes", len(nodes.Items))
}
func nodeLabelsDesc(labelKeys []string) *prometheus.Desc {
return prometheus.NewDesc(
descNodeLabelsName,
descNodeLabelsHelp,
append(descNodeLabelsDefaultLabels, labelKeys...),
nil,
)
}
func (nc *nodeCollector) collectNode(ch chan<- prometheus.Metric, n v1.Node) {
addGauge := func(desc *prometheus.Desc, v float64, lv ...string) {
lv = append([]string{n.Name}, lv...)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, lv...)
}
// NOTE: the instrumentation API requires providing label values in order of declaration
// in the metric descriptor. Be careful when making modifications.
addGauge(descNodeInfo, 1,
n.Status.NodeInfo.KernelVersion,
n.Status.NodeInfo.OSImage,
n.Status.NodeInfo.ContainerRuntimeVersion,
n.Status.NodeInfo.KubeletVersion,
n.Status.NodeInfo.KubeProxyVersion,
n.Spec.ProviderID,
)
if !n.CreationTimestamp.IsZero() {
addGauge(descNodeCreated, float64(n.CreationTimestamp.Unix()))
}
labelKeys, labelValues := kubeLabelsToPrometheusLabels(n.Labels)
addGauge(nodeLabelsDesc(labelKeys), 1, labelValues...)
addGauge(descNodeSpecUnschedulable, boolFloat64(n.Spec.Unschedulable))
// Collect node taints
for _, taint := range n.Spec.Taints {
// Taints are applied to repel pods from nodes that do not have a corresponding
// toleration. Many node conditions are optionally reflected as taints
// by the node controller in order to simplify scheduling constraints.
addGauge(descNodeSpecTaint, 1, taint.Key, taint.Value, string(taint.Effect))
}
// Collect node conditions and while default to false.
for _, c := range n.Status.Conditions {
// This all-in-one metric family contains all conditions for extensibility.
// Third party plugin may report customized condition for cluster node
// (e.g. node-problem-detector), and Kubernetes may add new core
// conditions in future.
addConditionMetrics(ch, descNodeStatusCondition, c.Status, n.Name, string(c.Type))
}
// Set current phase to 1, others to 0 if it is set.
if p := n.Status.Phase; p != "" {
addGauge(descNodeStatusPhase, boolFloat64(p == v1.NodePending), string(v1.NodePending))
addGauge(descNodeStatusPhase, boolFloat64(p == v1.NodeRunning), string(v1.NodeRunning))
addGauge(descNodeStatusPhase, boolFloat64(p == v1.NodeTerminated), string(v1.NodeTerminated))
}
if !nc.opts.DisableNodeNonGenericResourceMetrics {
// Add capacity and allocatable resources if they are set.
addResource := func(d *prometheus.Desc, res v1.ResourceList, n v1.ResourceName) {
if v, ok := res[n]; ok {
addGauge(d, float64(v.MilliValue())/1000)
}
}
addResource(descNodeStatusCapacityCPU, n.Status.Capacity, v1.ResourceCPU)
addResource(descNodeStatusCapacityMemory, n.Status.Capacity, v1.ResourceMemory)
addResource(descNodeStatusCapacityPods, n.Status.Capacity, v1.ResourcePods)
addResource(descNodeStatusAllocatableCPU, n.Status.Allocatable, v1.ResourceCPU)
addResource(descNodeStatusAllocatableMemory, n.Status.Allocatable, v1.ResourceMemory)
addResource(descNodeStatusAllocatablePods, n.Status.Allocatable, v1.ResourcePods)
}
capacity := n.Status.Capacity
allocatable := n.Status.Allocatable
for resourceName, val := range capacity {
switch resourceName {
case v1.ResourceCPU:
addGauge(descNodeStatusCapacity, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitCore))
case v1.ResourceStorage:
fallthrough
case v1.ResourceEphemeralStorage:
fallthrough
case v1.ResourceMemory:
addGauge(descNodeStatusCapacity, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitByte))
case v1.ResourcePods:
addGauge(descNodeStatusCapacity, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitInteger))
default:
if helper.IsHugePageResourceName(resourceName) {
addGauge(descNodeStatusCapacity, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitByte))
}
if helper.IsAttachableVolumeResourceName(resourceName) {
addGauge(descNodeStatusCapacity, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitByte))
}
if helper.IsExtendedResourceName(resourceName) {
addGauge(descNodeStatusCapacity, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitInteger))
}
}
}
for resourceName, val := range allocatable {
switch resourceName {
case v1.ResourceCPU:
addGauge(descNodeStatusAllocatable, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitCore))
case v1.ResourceStorage:
fallthrough
case v1.ResourceEphemeralStorage:
fallthrough
case v1.ResourceMemory:
addGauge(descNodeStatusAllocatable, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitByte))
case v1.ResourcePods:
addGauge(descNodeStatusAllocatable, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitInteger))
default:
if helper.IsHugePageResourceName(resourceName) {
addGauge(descNodeStatusAllocatable, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitByte))
}
if helper.IsAttachableVolumeResourceName(resourceName) {
addGauge(descNodeStatusAllocatable, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitByte))
}
if helper.IsExtendedResourceName(resourceName) {
addGauge(descNodeStatusAllocatable, float64(val.MilliValue())/1000, sanitizeLabelName(string(resourceName)), string(constant.UnitInteger))
}
}
}
}