From c5c6c9a3f833d072c7781c39551ab55c23023f16 Mon Sep 17 00:00:00 2001 From: adinhodovic Date: Thu, 19 Oct 2023 13:59:05 +0200 Subject: [PATCH] feat: Add new metric kube_pod_scheduler_name Signed-off-by: adinhodovic --- docs/pod-metrics.md | 1 + internal/store/pod.go | 22 ++++++++++++++++++++++ internal/store/pod_test.go | 22 +++++++++++++++++++++- pkg/app/server_test.go | 4 ++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/pod-metrics.md b/docs/pod-metrics.md index e284a14da1..4602923036 100644 --- a/docs/pod-metrics.md +++ b/docs/pod-metrics.md @@ -54,6 +54,7 @@ | kube_pod_status_unschedulable | Gauge | Describes the unschedulable status for the pod | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | - | | kube_pod_tolerations | Gauge | Information about the pod tolerations | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid>
`key`=<toleration-key>
`operator`=<toleration-operator>
`value`=<toleration-value>
`effect`=<toleration-effect> `toleration_seconds`=<toleration-seconds> | EXPERIMENTAL | - | | kube_pod_service_account | Gauge | The service account for a pod | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid>
`service_account`=<service_account> | EXPERIMENTAL | - | +| kube_pod_scheduler_name | Gauge | The scheduler name for a pod | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid>
`scheduler_name`=<scheduler_name> | EXPERIMENTAL | - | ## Useful metrics queries diff --git a/internal/store/pod.go b/internal/store/pod.go index 232da98830..8665c76db5 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -94,6 +94,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat createPodTolerationsFamilyGenerator(), createPodNodeSelectorsFamilyGenerator(), createPodServiceAccountFamilyGenerator(), + createPodSchedulerNameFamilyGenerator(), } } @@ -1708,6 +1709,27 @@ func createPodServiceAccountFamilyGenerator() generator.FamilyGenerator { ) } +func createPodSchedulerNameFamilyGenerator() generator.FamilyGenerator { + return *generator.NewFamilyGeneratorWithStability( + "kube_pod_scheduler_name", + "The scheduler name for a pod.", + metric.Gauge, + basemetrics.ALPHA, + "", + wrapPodFunc(func(p *v1.Pod) *metric.Family { + m := metric.Metric{ + LabelKeys: []string{"scheduler_name"}, + LabelValues: []string{p.Spec.SchedulerName}, + Value: 1, + } + + return &metric.Family{ + Metrics: []*metric.Metric{&m}, + } + }), + ) +} + func wrapPodFunc(f func(*v1.Pod) *metric.Family) func(interface{}) *metric.Family { return func(obj interface{}) *metric.Family { pod := obj.(*v1.Pod) diff --git a/internal/store/pod_test.go b/internal/store/pod_test.go index 8ff0065a8c..9f74bd370f 100644 --- a/internal/store/pod_test.go +++ b/internal/store/pod_test.go @@ -2136,6 +2136,26 @@ func TestPodStore(t *testing.T) { "kube_pod_service_account", }, }, + { + Obj: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod1", + Namespace: "ns1", + UID: "uid1", + }, + Spec: v1.PodSpec{ + SchedulerName: "scheduler1", + }, + }, + Want: ` + # HELP kube_pod_scheduler_name The scheduler name for a pod. + # TYPE kube_pod_scheduler_name gauge + kube_pod_scheduler_name{namespace="ns1",pod="pod1",scheduler_name="scheduler1",uid="uid1"} 1 + `, + MetricNames: []string{ + "kube_pod_scheduler_name", + }, + }, } for i, c := range cases { @@ -2231,7 +2251,7 @@ func BenchmarkPodStore(b *testing.B) { }, } - expectedFamilies := 52 + expectedFamilies := 53 for n := 0; n < b.N; n++ { families := f(pod) if len(families) != expectedFamilies { diff --git a/pkg/app/server_test.go b/pkg/app/server_test.go index 636080bffc..1f47b4f0ff 100644 --- a/pkg/app/server_test.go +++ b/pkg/app/server_test.go @@ -235,6 +235,7 @@ func TestFullScrapeCycle(t *testing.T) { # HELP kube_pod_overhead_cpu_cores The pod overhead in regards to cpu cores associated with running a pod. # HELP kube_pod_overhead_memory_bytes The pod overhead in regards to memory associated with running a pod. # HELP kube_pod_runtimeclass_name_info The runtimeclass associated with the pod. +# HELP kube_pod_scheduler_name The scheduler name for a pod. # HELP kube_pod_service_account The service account for a pod. # HELP kube_pod_owner [STABLE] Information about the Pod's owner. # HELP kube_pod_restart_policy [STABLE] Describes the restart policy in use by this pod. @@ -286,6 +287,7 @@ func TestFullScrapeCycle(t *testing.T) { # TYPE kube_pod_overhead_cpu_cores gauge # TYPE kube_pod_overhead_memory_bytes gauge # TYPE kube_pod_runtimeclass_name_info gauge +# TYPE kube_pod_scheduler_name gauge # TYPE kube_pod_service_account gauge # TYPE kube_pod_owner gauge # TYPE kube_pod_restart_policy gauge @@ -336,6 +338,7 @@ kube_pod_created{namespace="default",pod="pod0",uid="abc-0"} 1.5e+09 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="",created_by_name="",priority_class="",host_network="false"} 1 kube_pod_owner{namespace="default",pod="pod0",uid="abc-0",owner_kind="",owner_name="",owner_is_controller=""} 1 kube_pod_restart_policy{namespace="default",pod="pod0",uid="abc-0",type="Always"} 1 +kube_pod_scheduler_name{namespace="default",pod="pod0",uid="abc-0",scheduler_name="scheduler1"} 1 kube_pod_service_account{namespace="default",pod="pod0",uid="abc-0",service_account=""} 1 kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Failed"} 0 kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Pending"} 0 @@ -774,6 +777,7 @@ func pod(client *fake.Clientset, index int) error { UID: types.UID("abc-" + i), }, Spec: v1.PodSpec{ + SchedulerName: "scheduler1", RestartPolicy: v1.RestartPolicyAlways, NodeName: "node1", Containers: []v1.Container{