Skip to content

Commit 30b91c8

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 <[email protected]>
1 parent 7b5367d commit 30b91c8

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
@@ -204,14 +204,13 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
204204
if err != nil {
205205
return nil, errors.New("invalid hostPort: " + hostPort)
206206
}
207-
}
208-
209-
if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
210-
// Allow host port range iff containerPort is not a range.
211-
// In this case, use the host port range as the dynamic
212-
// host port range to allocate into.
213-
if endPort != startPort {
214-
return nil, fmt.Errorf("invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
207+
if (endPort - startPort) != (endHostPort - startHostPort) {
208+
// Allow host port range iff containerPort is not a range.
209+
// In this case, use the host port range as the dynamic
210+
// host port range to allocate into.
211+
if endPort != startPort {
212+
return nil, fmt.Errorf("invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
213+
}
215214
}
216215
}
217216

@@ -223,11 +222,11 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
223222
hPort := ""
224223
if hostPort != "" {
225224
hPort = strconv.FormatUint(startHostPort+i, 10)
226-
}
227-
// Set hostPort to a range only if there is a single container port
228-
// and a dynamic host port.
229-
if startPort == endPort && startHostPort != endHostPort {
230-
hPort += "-" + strconv.FormatUint(endHostPort, 10)
225+
// Set hostPort to a range only if there is a single container port
226+
// and a dynamic host port.
227+
if count == 1 && startHostPort != endHostPort {
228+
hPort += "-" + strconv.FormatUint(endHostPort, 10)
229+
}
231230
}
232231
ports = append(ports, PortMapping{
233232
Port: cPort,

0 commit comments

Comments
 (0)