mirror of
https://github.com/containers/podman.git
synced 2025-06-22 18:08:11 +08:00
Merge pull request #8043 from saschagrunert/hostport-fix
Fix host to container port mapping for simple ranges
This commit is contained in:
@ -25,7 +25,12 @@ const (
|
|||||||
func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping, map[string]map[string]map[uint16]uint16, map[string]map[string]map[uint16]uint16, error) {
|
func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping, map[string]map[string]map[uint16]uint16, map[string]map[string]map[uint16]uint16, error) {
|
||||||
// First, we need to validate the ports passed in the specgen, and then
|
// First, we need to validate the ports passed in the specgen, and then
|
||||||
// convert them into CNI port mappings.
|
// convert them into CNI port mappings.
|
||||||
finalMappings := []ocicni.PortMapping{}
|
type tempMapping struct {
|
||||||
|
mapping ocicni.PortMapping
|
||||||
|
startOfRange bool
|
||||||
|
isInRange bool
|
||||||
|
}
|
||||||
|
tempMappings := []tempMapping{}
|
||||||
|
|
||||||
// To validate, we need two maps: one for host ports, one for container
|
// To validate, we need two maps: one for host ports, one for container
|
||||||
// ports.
|
// ports.
|
||||||
@ -153,18 +158,32 @@ func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping,
|
|||||||
Protocol: p,
|
Protocol: p,
|
||||||
HostIP: port.HostIP,
|
HostIP: port.HostIP,
|
||||||
}
|
}
|
||||||
finalMappings = append(finalMappings, cniPort)
|
tempMappings = append(
|
||||||
|
tempMappings,
|
||||||
|
tempMapping{
|
||||||
|
mapping: cniPort,
|
||||||
|
startOfRange: port.Range > 0 && index == 0,
|
||||||
|
isInRange: port.Range > 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle any 0 host ports now by setting random container ports.
|
// Handle any 0 host ports now by setting random container ports.
|
||||||
if postAssignHostPort {
|
if postAssignHostPort {
|
||||||
remadeMappings := make([]ocicni.PortMapping, 0, len(finalMappings))
|
remadeMappings := make([]ocicni.PortMapping, 0, len(tempMappings))
|
||||||
|
|
||||||
|
var (
|
||||||
|
candidate int
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
// Iterate over all
|
// Iterate over all
|
||||||
for _, p := range finalMappings {
|
for _, tmp := range tempMappings {
|
||||||
if p.HostPort != 0 {
|
p := tmp.mapping
|
||||||
|
|
||||||
|
if p.HostPort != 0 && !tmp.isInRange {
|
||||||
remadeMappings = append(remadeMappings, p)
|
remadeMappings = append(remadeMappings, p)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -192,9 +211,15 @@ func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping,
|
|||||||
|
|
||||||
// Max retries to ensure we don't loop forever.
|
// Max retries to ensure we don't loop forever.
|
||||||
for i := 0; i < 15; i++ {
|
for i := 0; i < 15; i++ {
|
||||||
candidate, err := getRandomPort()
|
// Only get a random candidate for single entries or the start
|
||||||
if err != nil {
|
// of a range. Otherwise we just increment the candidate.
|
||||||
return nil, nil, nil, errors.Wrapf(err, "error getting candidate host port for container port %d", p.ContainerPort)
|
if !tmp.isInRange || tmp.startOfRange {
|
||||||
|
candidate, err = getRandomPort()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, nil, errors.Wrapf(err, "error getting candidate host port for container port %d", p.ContainerPort)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
candidate++
|
||||||
}
|
}
|
||||||
|
|
||||||
if hostPortMap[uint16(candidate)] == 0 {
|
if hostPortMap[uint16(candidate)] == 0 {
|
||||||
@ -213,6 +238,11 @@ func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping,
|
|||||||
return remadeMappings, containerPortValidate, hostPortValidate, nil
|
return remadeMappings, containerPortValidate, hostPortValidate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
finalMappings := []ocicni.PortMapping{}
|
||||||
|
for _, m := range tempMappings {
|
||||||
|
finalMappings = append(finalMappings, m.mapping)
|
||||||
|
}
|
||||||
|
|
||||||
return finalMappings, containerPortValidate, hostPortValidate, nil
|
return finalMappings, containerPortValidate, hostPortValidate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user