Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set container name empty when multiple containers don't specify ports #238

Merged
merged 3 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions collector/metadata/kubernetes/pod_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions collector/metadata/kubernetes/pod_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down