Skip to content

Commit

Permalink
Only bind pod ports to host when replicas == 1
Browse files Browse the repository at this point in the history
podman play kube creates a pod per replica and tries to bind all of them to the
same host port. That however can only work when there is one replica as
otherwise multiple pods get bound to the same port on the host.

This fixes containers#16765
  • Loading branch information
dcermak committed Dec 7, 2022
1 parent 306fb5d commit b04914d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ func ToPodOpt(ctx context.Context, podName string, p entities.PodCreateOptions,
}
p.Net.AddHosts = hosts
}
podPorts := getPodPorts(podYAML.Spec.Containers)

bindToHostPort := true
if podYAML.Replicas != nil && *podYAML.Replicas > 1 {
bindToHostPort = false
}
podPorts := getPodPorts(template.Spec.Containers, bindToHostPort)
p.Net.PublishPorts = podPorts

if dnsConfig := template.Spec.DNSConfig; dnsConfig != nil {
Expand Down Expand Up @@ -937,14 +942,14 @@ func getContainerResources(container v1.Container) (v1.ResourceRequirements, err

// getPodPorts converts a slice of kube container descriptions to an
// array of portmapping
func getPodPorts(containers []v1.Container) []types.PortMapping {
func getPodPorts(containers []v1.Container, bindToHostPort bool) []types.PortMapping {
var infraPorts []types.PortMapping
for _, container := range containers {
for _, p := range container.Ports {
if p.HostPort != 0 && p.ContainerPort == 0 {
p.ContainerPort = p.HostPort
}
if p.HostPort == 0 && p.ContainerPort != 0 {
if p.HostPort == 0 && p.ContainerPort != 0 && bindToHostPort {
p.HostPort = p.ContainerPort
}
if p.Protocol == "" {
Expand Down

0 comments on commit b04914d

Please sign in to comment.