From 2d4e5ffb6de48a2e7ce8bb9c39f964bf489be209 Mon Sep 17 00:00:00 2001 From: Daxin Wang Date: Mon, 6 Jun 2022 12:00:52 +0800 Subject: [PATCH 1/3] set container name empty when multiple containers don't specify ports Signed-off-by: Daxin Wang --- collector/metadata/kubernetes/pod_watch.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/collector/metadata/kubernetes/pod_watch.go b/collector/metadata/kubernetes/pod_watch.go index 9578a6f63..bce3e3835 100644 --- a/collector/metadata/kubernetes/pod_watch.go +++ b/collector/metadata/kubernetes/pod_watch.go @@ -213,7 +213,14 @@ func onAdd(obj interface{}) { HostPortMap: make(map[int32]int32), RefPodInfo: kpi, } + // Not specifying a port DOES NOT prevent that port from being exposed. + // So Ports could be empty, if so we only record its IP address. if len(tmpContainer.Ports) == 0 { + // If there are more than one container that doesn't specify a port, + // we would rather get an empty name empty than get an incorrect name. + if len(pod.Spec.Containers) > 0 { + containerInfo.Name = "" + } // When there are many pods in one pod and only some of them have ports, // the containers at the back will overwrite the one at the front here. MetaDataCache.AddContainerByIpPort(pod.Status.PodIP, 0, containerInfo) From a49003d26c25069685af14cd1db9c0481b1ae5f3 Mon Sep 17 00:00:00 2001 From: Daxin Wang Date: Mon, 6 Jun 2022 15:50:13 +0800 Subject: [PATCH 2/3] fix comments Signed-off-by: Daxin Wang --- collector/metadata/kubernetes/pod_watch.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/collector/metadata/kubernetes/pod_watch.go b/collector/metadata/kubernetes/pod_watch.go index bce3e3835..be68f774f 100644 --- a/collector/metadata/kubernetes/pod_watch.go +++ b/collector/metadata/kubernetes/pod_watch.go @@ -216,13 +216,13 @@ func onAdd(obj interface{}) { // Not specifying a port DOES NOT prevent that port from being exposed. // So Ports could be empty, if so we only record its IP address. if len(tmpContainer.Ports) == 0 { - // If there are more than one container that doesn't specify a port, - // we would rather get an empty name empty than get an incorrect name. + // If there is more than one container that doesn't specify a port, + // we would rather get an empty name than get an incorrect one. if len(pod.Spec.Containers) > 0 { containerInfo.Name = "" } - // When there are many pods in one pod and only some of them have ports, - // the containers at the back will overwrite the one at the front here. + // When there are many containers in one pod and only part of them have ports, + // the containers at the back will overwrite the ones at the front here. MetaDataCache.AddContainerByIpPort(pod.Status.PodIP, 0, containerInfo) continue } From c966929e1469b1bf3ecbbd7b23b4efb3f43ad91a Mon Sep 17 00:00:00 2001 From: Daxin Wang Date: Mon, 6 Jun 2022 16:36:42 +0800 Subject: [PATCH 3/3] delete the containers without ports specified when deleting its pod Signed-off-by: Daxin Wang --- collector/metadata/kubernetes/pod_delete.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/collector/metadata/kubernetes/pod_delete.go b/collector/metadata/kubernetes/pod_delete.go index cc28e8e03..0910aa184 100644 --- a/collector/metadata/kubernetes/pod_delete.go +++ b/collector/metadata/kubernetes/pod_delete.go @@ -60,6 +60,10 @@ func deletePod(pod *corev1.Pod) { } for _, container := range pod.Spec.Containers { + if len(container.Ports) == 0 { + MetaDataCache.DeleteContainerByIpPort(pod.Status.PodIP, 0) + continue + } for _, port := range container.Ports { // Assume that PodIP:Port can't be reused in a few seconds MetaDataCache.DeleteContainerByIpPort(pod.Status.PodIP, uint32(port.ContainerPort))