Skip to content

Commit

Permalink
Use same hostPort for the same containerPorts with different protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Felipe Takakura committed Oct 10, 2019
1 parent 3f5eb82 commit 6617f7f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions models/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,16 @@ func (p *Pod) configureHostPorts(
for _, container := range p.Containers {
podContainer := getContainerWithName(container.Name, pod)
container.Ports = make([]*Port, len(podContainer.Ports))

portHostPortMap := map[int]int{}
for i, port := range podContainer.Ports {
// use same hostPort for the same port numbers with different protocols (TCP/UDP)
if portHostPortMap[int(port.ContainerPort)] == 0 {
portHostPortMap[int(port.ContainerPort)] = int(port.HostPort)
}
container.Ports[i] = &Port{
ContainerPort: int(port.ContainerPort),
Name: port.Name,
HostPort: int(port.HostPort),
HostPort: portHostPortMap[int(port.ContainerPort)],
Protocol: string(port.Protocol),
}
}
Expand Down Expand Up @@ -337,11 +341,16 @@ func (p *Pod) configureHostPorts(
for _, container := range p.Containers {
ports := GetRandomPorts(start, end, len(container.Ports))
containerPorts := make([]*Port, len(container.Ports))
portHostPortMap := map[int]int{}
for i, port := range ports {
// use same hostPort for the same port numbers with different protocols (TCP/UDP)
if portHostPortMap[container.Ports[i].ContainerPort] == 0 {
portHostPortMap[container.Ports[i].ContainerPort] = port
}
containerPorts[i] = &Port{
ContainerPort: container.Ports[i].ContainerPort,
Name: container.Ports[i].Name,
HostPort: port,
HostPort: portHostPortMap[container.Ports[i].ContainerPort],
Protocol: container.Ports[i].Protocol,
}
}
Expand Down

0 comments on commit 6617f7f

Please sign in to comment.