Skip to content

add warning for dup ports in containers[*].ports #113245

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

Merged
merged 3 commits into from
Jul 15, 2023
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
36 changes: 36 additions & 0 deletions pkg/api/pod/warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,42 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta
return true
})

type portBlock struct {
field *field.Path
port api.ContainerPort
}

// Accumulate ports across all containers
allPorts := map[string][]portBlock{}
pods.VisitContainersWithPath(podSpec, fieldPath.Child("spec"), func(c *api.Container, fldPath *field.Path) bool {
for i, port := range c.Ports {
if port.HostIP != "" && port.HostPort == 0 {
warnings = append(warnings, fmt.Sprintf("%s: hostIP set without hostPort: %+v",
fldPath.Child("ports").Index(i), port))
}
k := fmt.Sprintf("%d/%s", port.ContainerPort, port.Protocol)
if others, found := allPorts[k]; found {
// Someone else has this protcol+port, but it still might not be a conflict.
for _, other := range others {
if port.HostIP == other.port.HostIP && port.HostPort == other.port.HostPort {
// Exactly-equal is obvious. Validation should already filter for this except when these are unspecified.
warnings = append(warnings, fmt.Sprintf("%s: duplicate port definition with %s", fldPath.Child("ports").Index(i), other.field))
} else if port.HostPort == 0 || other.port.HostPort == 0 {
// HostPort = 0 is redundant with any other value, which is odd but not really dangerous. HostIP doesn't matter here.
warnings = append(warnings, fmt.Sprintf("%s: overlapping port definition with %s", fldPath.Child("ports").Index(i), other.field))
} else if a, b := port.HostIP == "", other.port.HostIP == ""; port.HostPort == other.port.HostPort && ((a || b) && !(a && b)) {
// If the HostPorts are the same and either HostIP is not specified while the other is not, the behavior is undefined.
warnings = append(warnings, fmt.Sprintf("%s: dangerously ambiguous port definition with %s", fldPath.Child("ports").Index(i), other.field))
}
}
allPorts[k] = append(allPorts[k], portBlock{field: fldPath.Child("ports").Index(i), port: port})
} else {
allPorts[k] = []portBlock{{field: fldPath.Child("ports").Index(i), port: port}}
}
}
return true
})

// warn if the terminationGracePeriodSeconds is negative.
if podSpec.TerminationGracePeriodSeconds != nil && *podSpec.TerminationGracePeriodSeconds < 0 {
warnings = append(warnings, fmt.Sprintf("%s: must be >= 0; negative values are invalid and will be treated as 1", fieldPath.Child("spec", "terminationGracePeriodSeconds")))
Expand Down
Loading