Skip to content

Commit

Permalink
containermetadata: use full pod name (#68)
Browse files Browse the repository at this point in the history
previously, the logic would truncate the replicaset and pod hash
from the pod name, showing the deployment name instead. this
doesn't let users easily distinguish between two pods from the
same replicaset running on the same node.
  • Loading branch information
Gandem authored Jan 10, 2025
1 parent 5609732 commit 2dfd721
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 31 deletions.
33 changes: 3 additions & 30 deletions containermetadata/containermetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,51 +392,24 @@ func getDockerClient() *client.Client {
// putCache updates the container id metadata cache for the provided pod.
func (p *containerMetadataProvider) putCache(pod *corev1.Pod) {
log.Debugf("Update container metadata cache for pod %s", pod.Name)
podName := getPodName(pod)

for i := range pod.Status.ContainerStatuses {
var containerID string
var err error
if containerID, err = matchContainerID(
pod.Status.ContainerStatuses[i].ContainerID); err != nil {
log.Debugf("failed to get kubernetes container metadata: %v", err)
log.Debugf("failed to get kubernetes container metadata for pod %s: %v", pod.Name, err)
continue
}

p.containerMetadataCache.Add(containerID, ContainerMetadata{
ContainerID: containerID,
PodName: podName,
PodName: pod.Name,
ContainerName: pod.Status.ContainerStatuses[i].Name,
})
}
}

func getPodName(pod *corev1.Pod) string {
podName := pod.Name

for j := range pod.OwnerReferences {
if strings.HasPrefix(podName, pod.OwnerReferences[j].Name) {
switch pod.OwnerReferences[j].Kind {
case "ReplicaSet":
// For replicaSet the Owner references Name contains the replicaset version
// ie 'deployment-replicaset' which we want to remove.
lastIndex := strings.LastIndex(pod.OwnerReferences[j].Name, "-")
if lastIndex < 0 {
// pod.OwnerReferences[j].Name does not contain a '-' so
// we take the full name as PodName and avoid to panic.
podName = pod.OwnerReferences[j].Name
} else {
podName = pod.OwnerReferences[j].Name[:lastIndex]
}
default:
podName = pod.OwnerReferences[j].Name
}
}
}

return podName
}

func matchContainerID(containerIDStr string) (string, error) {
containerIDParts := containerIDPattern.FindStringSubmatch(containerIDStr)
if len(containerIDParts) != 2 {
Expand Down Expand Up @@ -546,7 +519,7 @@ func (p *containerMetadataProvider) getKubernetesPodMetadata(pidContainerID stri
}

for j := range pods.Items {
podName := getPodName(&pods.Items[j])
podName := pods.Items[j].Name
containers := pods.Items[j].Status.ContainerStatuses
for i := range containers {
var containerID string
Expand Down
2 changes: 1 addition & 1 deletion containermetadata/containermetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestGetKubernetesPodMetadata(t *testing.T) {
pid: 1,
expContainerID: "ed89697807a981b82f6245ac3a13be232c1e13435d52bc3f53060d61babe1997",
expContainerName: "testcontainer-ab1c",
expPodName: "testpod",
expPodName: "testpod-abc123-sldfj293",
},
{
name: "matchingPodNotFound",
Expand Down

0 comments on commit 2dfd721

Please sign in to comment.