From 47692032482e3d900be621b4a09c7e7f1cf8dfb6 Mon Sep 17 00:00:00 2001 From: changhyuni Date: Sat, 18 Nov 2023 23:45:52 +0900 Subject: [PATCH] pods: update init_cotainer_info metric pods: update init_cotainer_info metric pods: update init_cotainer_info metric --- docs/pod-metrics.md | 2 +- internal/store/pod.go | 9 +++++++-- internal/store/pod_test.go | 4 +++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/pod-metrics.md b/docs/pod-metrics.md index 224bb7bcb1..1fa6f4dfb3 100644 --- a/docs/pod-metrics.md +++ b/docs/pod-metrics.md @@ -36,7 +36,7 @@ | kube_pod_created | Gauge | Unix creation timestamp | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | - | | kube_pod_deletion_timestamp | Gauge | Unix deletion timestamp | seconds | `pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | EXPERIMENTAL | - | | kube_pod_restart_policy | Gauge | Describes the restart policy in use by this pod | | `pod`=<pod-name>
`namespace`=<pod-namespace>
`type`=<Always\|Never\|OnFailure>
`uid`=<pod-uid> | STABLE | - | -| kube_pod_init_container_info | Gauge | Information about an init container in a pod | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`image`=<image-name>
`image_id`=<image-id>
`image_spec`=<image-spec>
`container_id`=<containerid>
`uid`=<pod-uid> | STABLE | - | +| kube_pod_init_container_info | Gauge | Information about an init container in a pod | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`image`=<image-name>
`image_id`=<image-id>
`image_spec`=<image-spec>
`container_id`=<containerid>
`uid`=<pod-uid>
`type`=<Always> | STABLE | - | | kube_pod_init_container_status_waiting | Gauge | Describes whether the init container is currently in waiting state | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | - | | kube_pod_init_container_status_waiting_reason | Gauge | Describes the reason the init container is currently in waiting state | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`reason`=<container-waiting-reason>
`uid`=<pod-uid> | EXPERIMENTAL | - | | kube_pod_init_container_status_running | Gauge | Describes whether the init container is currently in running state | | `container`=<container-name>
`pod`=<pod-name>
`namespace`=<pod-namespace>
`uid`=<pod-uid> | STABLE | - | diff --git a/internal/store/pod.go b/internal/store/pod.go index 7fc866a9d6..735d94792c 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -677,16 +677,21 @@ func createPodInitContainerInfoFamilyGenerator() generator.FamilyGenerator { "", wrapPodFunc(func(p *v1.Pod) *metric.Family { ms := []*metric.Metric{} - labelKeys := []string{"container", "image_spec", "image", "image_id", "container_id"} + labelKeys := []string{"container", "image_spec", "image", "image_id", "container_id", "type"} for _, c := range p.Spec.InitContainers { + restartPolicy := "" + if c.RestartPolicy != nil { + restartPolicy = string(*c.RestartPolicy) + } + for _, cs := range p.Status.InitContainerStatuses { if cs.Name != c.Name { continue } ms = append(ms, &metric.Metric{ LabelKeys: labelKeys, - LabelValues: []string{cs.Name, c.Image, cs.Image, cs.ImageID, cs.ContainerID}, + LabelValues: []string{cs.Name, c.Image, cs.Image, cs.ImageID, cs.ContainerID, restartPolicy}, Value: 1, }) } diff --git a/internal/store/pod_test.go b/internal/store/pod_test.go index 05e12ba7b0..4bab61241a 100644 --- a/internal/store/pod_test.go +++ b/internal/store/pod_test.go @@ -33,6 +33,7 @@ func TestPodStore(t *testing.T) { runtimeclass := "foo" startTime := 1501569018 metav1StartTime := metav1.Unix(int64(startTime), 0) + restartPolicyAlways := v1.ContainerRestartPolicyAlways cases := []generateMetricsTestCase{ { @@ -89,6 +90,7 @@ func TestPodStore(t *testing.T) { { Name: "initContainer", Image: "k8s.gcr.io/initfoo_spec", + RestartPolicy: &restartPolicyAlways, }, }, }, @@ -124,7 +126,7 @@ func TestPodStore(t *testing.T) { # TYPE kube_pod_init_container_info gauge kube_pod_container_info{container="container2",container_id="docker://cd456",image_spec="k8s.gcr.io/hyperkube2_spec",image="k8s.gcr.io/hyperkube2",image_id="docker://sha256:bbb",namespace="ns2",pod="pod2",uid="uid2"} 1 kube_pod_container_info{container="container3",container_id="docker://ef789",image_spec="k8s.gcr.io/hyperkube3_spec",image="k8s.gcr.io/hyperkube3",image_id="docker://sha256:ccc",namespace="ns2",pod="pod2",uid="uid2"} 1 - kube_pod_init_container_info{container="initContainer",container_id="docker://ef123",image_spec="k8s.gcr.io/initfoo_spec",image="k8s.gcr.io/initfoo",image_id="docker://sha256:wxyz",namespace="ns2",pod="pod2",uid="uid2"} 1`, + kube_pod_init_container_info{container="initContainer",container_id="docker://ef123",image_spec="k8s.gcr.io/initfoo_spec",image="k8s.gcr.io/initfoo",image_id="docker://sha256:wxyz",namespace="ns2",pod="pod2",uid="uid2",type="Always"} 1`, MetricNames: []string{"kube_pod_container_info", "kube_pod_init_container_info"}, }, {