Skip to content

Commit d6d2590

Browse files
committed
nat: ParsePortSpec: combine some conditions
- re-use the `count` variable for clarity - merge some conditional branches Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent ed2082b commit d6d2590

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

nat/nat.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,13 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
207207
if err != nil {
208208
return nil, errors.New("invalid hostPort: " + hostPort)
209209
}
210-
}
211-
212-
if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
213-
// Allow host port range iff containerPort is not a range.
214-
// In this case, use the host port range as the dynamic
215-
// host port range to allocate into.
216-
if endPort != startPort {
217-
return nil, fmt.Errorf("invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
210+
if (endPort - startPort) != (endHostPort - startHostPort) {
211+
// Allow host port range iff containerPort is not a range.
212+
// In this case, use the host port range as the dynamic
213+
// host port range to allocate into.
214+
if endPort != startPort {
215+
return nil, fmt.Errorf("invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
216+
}
218217
}
219218
}
220219

@@ -225,11 +224,11 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
225224
containerPort = strconv.FormatUint(startPort+i, 10)
226225
if hostPort != "" {
227226
hostPort = strconv.FormatUint(startHostPort+i, 10)
228-
}
229-
// Set hostPort to a range only if there is a single container port
230-
// and a dynamic host port.
231-
if startPort == endPort && startHostPort != endHostPort {
232-
hostPort = hostPort + "-" + strconv.FormatUint(endHostPort, 10)
227+
// Set hostPort to a range only if there is a single container port
228+
// and a dynamic host port.
229+
if count == 1 && startHostPort != endHostPort {
230+
hostPort += "-" + strconv.FormatUint(endHostPort, 10)
231+
}
233232
}
234233
ports = append(ports, PortMapping{
235234
Port: Port(containerPort + "/" + proto),

0 commit comments

Comments
 (0)