Skip to content

Commit f388222

Browse files
committed
nat: slight optimization in ParsePortSpec
- validate protocol before doing additional parsing - use a switch instead of looping over supported protocols - lowercase the proto once - remove some uses of fmt.Sprintf in favor of string-concatenating Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent b1459d9 commit f388222

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

nat/nat.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package nat
33

44
import (
5+
"errors"
56
"fmt"
67
"net"
78
"strconv"
@@ -107,13 +108,14 @@ func SplitProtoPort(rawPort string) (string, string) {
107108
return parts[1], parts[0]
108109
}
109110

110-
func validateProto(proto string) bool {
111-
for _, availableProto := range []string{"tcp", "udp", "sctp"} {
112-
if availableProto == proto {
113-
return true
114-
}
111+
func validateProto(proto string) error {
112+
switch proto {
113+
case "tcp", "udp", "sctp":
114+
// All good
115+
return nil
116+
default:
117+
return errors.New("invalid proto: " + proto)
115118
}
116-
return false
117119
}
118120

119121
// ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
@@ -169,9 +171,12 @@ func splitParts(rawport string) (string, string, string) {
169171

170172
// ParsePortSpec parses a port specification string into a slice of PortMappings
171173
func ParsePortSpec(rawPort string) ([]PortMapping, error) {
172-
var proto string
173174
ip, hostPort, containerPort := splitParts(rawPort)
174-
proto, containerPort = SplitProtoPort(containerPort)
175+
proto, containerPort := SplitProtoPort(containerPort)
176+
proto = strings.ToLower(proto)
177+
if err := validateProto(proto); err != nil {
178+
return nil, err
179+
}
175180

176181
if ip != "" && ip[0] == '[' {
177182
// Strip [] from IPV6 addresses
@@ -210,10 +215,6 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
210215
}
211216
}
212217

213-
if !validateProto(strings.ToLower(proto)) {
214-
return nil, fmt.Errorf("invalid proto: %s", proto)
215-
}
216-
217218
ports := []PortMapping{}
218219
for i := uint64(0); i <= (endPort - startPort); i++ {
219220
containerPort = strconv.FormatUint(startPort+i, 10)
@@ -223,18 +224,17 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
223224
// Set hostPort to a range only if there is a single container port
224225
// and a dynamic host port.
225226
if startPort == endPort && startHostPort != endHostPort {
226-
hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
227+
hostPort = hostPort + "-" + strconv.FormatUint(endHostPort, 10)
227228
}
228-
port, err := NewPort(strings.ToLower(proto), containerPort)
229+
port, err := NewPort(proto, containerPort)
229230
if err != nil {
230231
return nil, err
231232
}
232233

233-
binding := PortBinding{
234-
HostIP: ip,
235-
HostPort: hostPort,
236-
}
237-
ports = append(ports, PortMapping{Port: port, Binding: binding})
234+
ports = append(ports, PortMapping{
235+
Port: port,
236+
Binding: PortBinding{HostIP: ip, HostPort: hostPort},
237+
})
238238
}
239239
return ports, nil
240240
}

0 commit comments

Comments
 (0)