Skip to content

Commit

Permalink
Search for k8s containers by name, not position in array. (#2983)
Browse files Browse the repository at this point in the history
It is not necessarily true that the items in ContainerStatuses are in
the same order as those in Spec.Containers, so the old logic was
wrong.

container names _are_ unique within a pod, so we can find the container in
the spec for a given ID based on its name.
  • Loading branch information
umanwizard authored Sep 13, 2024
1 parent 3b6e8ef commit 1c8646b
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions reporter/metadata/containermetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ const (
presentValue = model.LabelValue("true")
)

func containerForName(name string, containers []corev1.Container) *corev1.Container {
for i := range containers {
if containers[i].Name == name {
return &containers[i]
}
}
return nil
}

func (p *containerMetadataProvider) addPodContainerLabels(pod *corev1.Pod) {
log.Debugf("Update container metadata cache for pod %s", pod.Name)

Expand All @@ -342,7 +351,14 @@ func (p *containerMetadataProvider) addPodContainerLabels(pod *corev1.Pod) {
continue
}

p.addPodContainerMetadata(pod, &pod.Spec.Containers[i], containerID, false)
name := pod.Status.ContainerStatuses[i].Name
ctr := containerForName(name, pod.Spec.Containers)
if ctr == nil {
log.Infof("failed to find kubernetes container in spec named: %s", name)
continue
}

p.addPodContainerMetadata(pod, ctr, containerID, false)
}

for i := range pod.Status.InitContainerStatuses {
Expand All @@ -354,7 +370,14 @@ func (p *containerMetadataProvider) addPodContainerLabels(pod *corev1.Pod) {
continue
}

p.addPodContainerMetadata(pod, &pod.Spec.InitContainers[i], containerID, true)
name := pod.Status.InitContainerStatuses[i].Name
ctr := containerForName(name, pod.Spec.InitContainers)
if ctr == nil {
log.Infof("failed to find init kubernetes container in spec named: %s", name)
continue
}

p.addPodContainerMetadata(pod, ctr, containerID, false)
}
}

Expand Down Expand Up @@ -591,7 +614,14 @@ func (p *containerMetadataProvider) getKubernetesPodMetadata(pidContainerID stri
continue
}
if containerID == pidContainerID {
return p.addPodContainerMetadata(&pods.Items[j], &pods.Items[j].Spec.Containers[i], containerID, false), nil
name := pods.Items[j].Status.ContainerStatuses[i].Name
ctr := containerForName(name, pods.Items[j].Spec.Containers)
if ctr == nil {
log.Infof("failed to find kubernetes container in spec named: %s", name)
continue
}

return p.addPodContainerMetadata(&pods.Items[j], ctr, containerID, false), nil
}
}

Expand All @@ -605,7 +635,14 @@ func (p *containerMetadataProvider) getKubernetesPodMetadata(pidContainerID stri
continue
}
if containerID == pidContainerID {
return p.addPodContainerMetadata(&pods.Items[j], &pods.Items[j].Spec.InitContainers[i], containerID, true), nil
name := pods.Items[j].Status.InitContainerStatuses[i].Name
ctr := containerForName(name, pods.Items[j].Spec.InitContainers)
if ctr == nil {
log.Infof("failed to find init kubernetes container in spec named: %s", name)
continue
}

return p.addPodContainerMetadata(&pods.Items[j], ctr, containerID, false), nil
}
}
}
Expand Down

0 comments on commit 1c8646b

Please sign in to comment.