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)) diff --git a/collector/metadata/kubernetes/pod_watch.go b/collector/metadata/kubernetes/pod_watch.go index 9578a6f63..be68f774f 100644 --- a/collector/metadata/kubernetes/pod_watch.go +++ b/collector/metadata/kubernetes/pod_watch.go @@ -213,9 +213,16 @@ 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 { - // 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. + // 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 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 }