Skip to content

Commit

Permalink
internal/store: fix metrics slice length
Browse files Browse the repository at this point in the history
Problem: In #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 <[email protected]>
  • Loading branch information
jan--f committed May 9, 2022
1 parent 3e12ba0 commit 85c6b44
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/store/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 85c6b44

Please sign in to comment.