Skip to content

Commit 6d1c11f

Browse files
geojazEric Hole
authored andcommitted
Adds kube_pod_nodeselector metric
1 parent 34a1398 commit 6d1c11f

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

docs/pod-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| kube_pod_completion_time | Gauge | Completion time in unix timestamp for a pod | seconds | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE |
99
| kube_pod_owner | Gauge | Information about the Pod's owner | |`pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `owner_kind`=&lt;owner kind&gt; <br> `owner_name`=&lt;owner name&gt; <br> `owner_is_controller`=&lt;whether owner is controller&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE |
1010
| kube_pod_labels | Gauge | Kubernetes labels converted to Prometheus labels | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `label_POD_LABEL`=&lt;POD_LABEL&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE |
11+
| kube_pod_nodeselector | Gauge | Describes the Pod nodeSelectors | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `nodeselector_NODE_SELECTOR`=&lt;NODE_SELECTOR&gt; <br> `uid`=&lt;pod-uid&gt; | EXPERIMENTAL |
1112
| kube_pod_status_phase | Gauge | The pods current phase | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `phase`=&lt;Pending\|Running\|Succeeded\|Failed\|Unknown&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE |
1213
| kube_pod_status_ready | Gauge | Describes whether the pod is ready to serve requests | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `condition`=&lt;true\|false\|unknown&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE |
1314
| kube_pod_status_scheduled | Gauge | Describes the status of the scheduling process for the pod | |`pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `condition`=&lt;true\|false\|unknown&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE |

internal/store/pod.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
8282
createPodStatusScheduledFamilyGenerator(),
8383
createPodStatusScheduledTimeFamilyGenerator(),
8484
createPodStatusUnschedulableFamilyGenerator(),
85+
createPodNodeSelectorFamilyGenerator(),
8586
}
8687
}
8788

@@ -1358,6 +1359,26 @@ func createPodStatusUnschedulableFamilyGenerator() generator.FamilyGenerator {
13581359
)
13591360
}
13601361

1362+
func createPodNodeSelectorFamilyGenerator() generator.FamilyGenerator {
1363+
return *generator.NewFamilyGenerator(
1364+
"kube_pod_nodeselector",
1365+
"Describes the Pod nodeSelectors.",
1366+
metric.Gauge,
1367+
"",
1368+
wrapPodFunc(func(p *v1.Pod) *metric.Family {
1369+
labelKeys, labelValues := kubeMapToPrometheusLabels("nodeselector", p.Spec.NodeSelector)
1370+
m := metric.Metric{
1371+
LabelKeys: labelKeys,
1372+
LabelValues: labelValues,
1373+
Value: 1,
1374+
}
1375+
return &metric.Family{
1376+
Metrics: []*metric.Metric{&m},
1377+
}
1378+
}),
1379+
)
1380+
}
1381+
13611382
func wrapPodFunc(f func(*v1.Pod) *metric.Family) func(interface{}) *metric.Family {
13621383
return func(obj interface{}) *metric.Family {
13631384
pod := obj.(*v1.Pod)

internal/store/pod_test.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,55 @@ func TestPodStore(t *testing.T) {
18141814
"kube_pod_labels",
18151815
},
18161816
},
1817+
{
1818+
Obj: &v1.Pod{
1819+
ObjectMeta: metav1.ObjectMeta{
1820+
Name: "pod1",
1821+
Namespace: "ns1",
1822+
UID: "uid1",
1823+
},
1824+
Spec: v1.PodSpec{
1825+
NodeSelector: map[string]string{
1826+
// "kubernetes.io/os": "linux",
1827+
// "cloud.google.com/gke-accelerator": "nvidia-tesla-t4",
1828+
"a": "b",
1829+
},
1830+
},
1831+
},
1832+
AllowLabelsList: []string{options.LabelWildcard},
1833+
Want: `
1834+
# HELP kube_pod_nodeselector Describes the Pod nodeSelectors.
1835+
# TYPE kube_pod_nodeselector gauge
1836+
kube_pod_nodeselector{nodeselector_a="b",namespace="ns1",pod="pod1",uid="uid1"} 1
1837+
`,
1838+
MetricNames: []string{
1839+
"kube_pod_nodeselector",
1840+
},
1841+
},
1842+
{
1843+
Obj: &v1.Pod{
1844+
ObjectMeta: metav1.ObjectMeta{
1845+
Name: "pod2",
1846+
Namespace: "ns1",
1847+
UID: "uid6",
1848+
},
1849+
Spec: v1.PodSpec{
1850+
NodeSelector: map[string]string{
1851+
"kubernetes.io/os": "linux",
1852+
"cloud.google.com/gke-accelerator": "nvidia-tesla-t4",
1853+
},
1854+
},
1855+
},
1856+
AllowLabelsList: []string{options.LabelWildcard},
1857+
Want: `
1858+
# HELP kube_pod_nodeselector Describes the Pod nodeSelectors.
1859+
# TYPE kube_pod_nodeselector gauge
1860+
kube_pod_nodeselector{nodeselector_kubernetes_io_os="linux",nodeselector_cloud_google_com_gke_accelerator="nvidia-tesla-t4",namespace="ns1",pod="pod2",uid="uid6"} 1
1861+
`,
1862+
MetricNames: []string{
1863+
"kube_pod_nodeselector",
1864+
},
1865+
},
18171866
{
18181867
Obj: &v1.Pod{
18191868
ObjectMeta: metav1.ObjectMeta{
@@ -1925,7 +1974,7 @@ func BenchmarkPodStore(b *testing.B) {
19251974
},
19261975
}
19271976

1928-
expectedFamilies := 43
1977+
expectedFamilies := 44
19291978
for n := 0; n < b.N; n++ {
19301979
families := f(pod)
19311980
if len(families) != expectedFamilies {

pkg/app/server_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func TestFullScrapeCycle(t *testing.T) {
207207
# HELP kube_pod_init_container_status_waiting Describes whether the init container is currently in waiting state.
208208
# HELP kube_pod_init_container_status_waiting_reason Describes the reason the init container is currently in waiting state.
209209
# HELP kube_pod_labels Kubernetes labels converted to Prometheus labels.
210+
# HELP kube_pod_nodeselector Describes the Pod nodeSelectors.
210211
# HELP kube_pod_overhead_cpu_cores The pod overhead in regards to cpu cores associated with running a pod.
211212
# HELP kube_pod_overhead_memory_bytes The pod overhead in regards to memory associated with running a pod.
212213
# HELP kube_pod_runtimeclass_name_info The runtimeclass associated with the pod.
@@ -250,6 +251,7 @@ func TestFullScrapeCycle(t *testing.T) {
250251
# TYPE kube_pod_init_container_status_waiting gauge
251252
# TYPE kube_pod_init_container_status_waiting_reason gauge
252253
# TYPE kube_pod_labels gauge
254+
# TYPE kube_pod_nodeselector gauge
253255
# TYPE kube_pod_overhead_cpu_cores gauge
254256
# TYPE kube_pod_overhead_memory_bytes gauge
255257
# TYPE kube_pod_runtimeclass_name_info gauge
@@ -296,6 +298,7 @@ kube_pod_container_status_waiting{namespace="default",pod="pod0",uid="abc-0",con
296298
kube_pod_created{namespace="default",pod="pod0",uid="abc-0"} 1.5e+09
297299
kube_pod_info{namespace="default",pod="pod0",uid="abc-0",host_ip="1.1.1.1",pod_ip="1.2.3.4",node="node1",created_by_kind="<none>",created_by_name="<none>",priority_class="",host_network="false"} 1
298300
kube_pod_labels{namespace="default",pod="pod0",uid="abc-0"} 1
301+
kube_pod_nodeselector{namespace="default",pod="pod0",uid="abc-0"} 1
299302
kube_pod_owner{namespace="default",pod="pod0",uid="abc-0",owner_kind="<none>",owner_name="<none>",owner_is_controller="<none>"} 1
300303
kube_pod_restart_policy{namespace="default",pod="pod0",uid="abc-0",type="Always"} 1
301304
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Failed"} 0

0 commit comments

Comments
 (0)