From 85c6b44237ff00251b6107a311d93993f1402477 Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Mon, 9 May 2022 20:02:09 +0200 Subject: [PATCH] internal/store: fix metrics slice length Problem: In https://github.com/kubernetes/kube-state-metrics/pull/1723 a potential panic in the pod metrics gathering was fixed by working around a disconnect of `Spec.Containers` and `Status.ContainerStatuses`. The slice storing the resulting metrics however was still defined based on the length of the `Status.ContainerStatuses` list. Solution: Make the slice dynamic and append metrics to it. Signed-off-by: Jan Fajerski --- internal/store/pod.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/store/pod.go b/internal/store/pod.go index 5966674d34..e251fef4c4 100644 --- a/internal/store/pod.go +++ b/internal/store/pod.go @@ -127,19 +127,19 @@ func createPodContainerInfoFamilyGenerator() generator.FamilyGenerator { metric.Gauge, "", wrapPodFunc(func(p *v1.Pod) *metric.Family { - ms := make([]*metric.Metric, len(p.Status.ContainerStatuses)) + ms := []*metric.Metric{} labelKeys := []string{"container", "image_spec", "image", "image_id", "container_id"} - for i, c := range p.Spec.Containers { + for _, c := range p.Spec.Containers { for _, cs := range p.Status.ContainerStatuses { if cs.Name != c.Name { continue } - ms[i] = &metric.Metric{ + ms = append(ms, &metric.Metric{ LabelKeys: labelKeys, LabelValues: []string{cs.Name, c.Image, cs.Image, cs.ImageID, cs.ContainerID}, Value: 1, - } + }) } } return &metric.Family{