|
@@ -34,17 +34,20 @@ type PortSet map[Port]struct{}
|
|
// Port is a string containing port number and protocol in the format "80/tcp"
|
|
// Port is a string containing port number and protocol in the format "80/tcp"
|
|
type Port string
|
|
type Port string
|
|
|
|
|
|
-// NewPort creates a new instance of a Port given a protocol and port number
|
|
|
|
|
|
+// NewPort creates a new instance of a Port given a protocol and port number or port range
|
|
func NewPort(proto, port string) (Port, error) {
|
|
func NewPort(proto, port string) (Port, error) {
|
|
// Check for parsing issues on "port" now so we can avoid having
|
|
// Check for parsing issues on "port" now so we can avoid having
|
|
// to check it later on.
|
|
// to check it later on.
|
|
|
|
|
|
- portInt, err := ParsePort(port)
|
|
|
|
|
|
+ portStartInt, portEndInt, err := ParsePortRange(port)
|
|
if err != nil {
|
|
if err != nil {
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
|
|
- return Port(fmt.Sprintf("%d/%s", portInt, proto)), nil
|
|
|
|
|
|
+ if portStartInt == portEndInt {
|
|
|
|
+ return Port(fmt.Sprintf("%d/%s", portStartInt, proto)), nil
|
|
|
|
+ }
|
|
|
|
+ return Port(fmt.Sprintf("%d-%d/%s", portStartInt, portEndInt, proto)), nil
|
|
}
|
|
}
|
|
|
|
|
|
// ParsePort parses the port number string and returns an int
|
|
// ParsePort parses the port number string and returns an int
|
|
@@ -59,6 +62,18 @@ func ParsePort(rawPort string) (int, error) {
|
|
return int(port), nil
|
|
return int(port), nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ParsePortRange parses the port range string and returns start/end ints
|
|
|
|
+func ParsePortRange(rawPort string) (int, int, error) {
|
|
|
|
+ if len(rawPort) == 0 {
|
|
|
|
+ return 0, 0, nil
|
|
|
|
+ }
|
|
|
|
+ start, end, err := parsers.ParsePortRange(rawPort)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return 0, 0, err
|
|
|
|
+ }
|
|
|
|
+ return int(start), int(end), nil
|
|
|
|
+}
|
|
|
|
+
|
|
// Proto returns the protocol of a Port
|
|
// Proto returns the protocol of a Port
|
|
func (p Port) Proto() string {
|
|
func (p Port) Proto() string {
|
|
proto, _ := SplitProtoPort(string(p))
|
|
proto, _ := SplitProtoPort(string(p))
|
|
@@ -84,6 +99,11 @@ func (p Port) Int() int {
|
|
return int(port)
|
|
return int(port)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Range returns the start/end port numbers of a Port range as ints
|
|
|
|
+func (p Port) Range() (int, int, error) {
|
|
|
|
+ return ParsePortRange(p.Port())
|
|
|
|
+}
|
|
|
|
+
|
|
// SplitProtoPort splits a port in the format of proto/port
|
|
// SplitProtoPort splits a port in the format of proto/port
|
|
func SplitProtoPort(rawPort string) (string, string) {
|
|
func SplitProtoPort(rawPort string) (string, string) {
|
|
parts := strings.Split(rawPort, "/")
|
|
parts := strings.Split(rawPort, "/")
|
|
@@ -162,7 +182,12 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
|
|
}
|
|
}
|
|
|
|
|
|
if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
|
|
if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
|
|
- return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
|
|
|
|
|
|
+ // Allow host port range iff containerPort is not a range.
|
|
|
|
+ // In this case, use the host port range as the dynamic
|
|
|
|
+ // host port range to allocate into.
|
|
|
|
+ if endPort != startPort {
|
|
|
|
+ return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
if !validateProto(strings.ToLower(proto)) {
|
|
if !validateProto(strings.ToLower(proto)) {
|
|
@@ -174,6 +199,11 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
|
|
if len(hostPort) > 0 {
|
|
if len(hostPort) > 0 {
|
|
hostPort = strconv.FormatUint(startHostPort+i, 10)
|
|
hostPort = strconv.FormatUint(startHostPort+i, 10)
|
|
}
|
|
}
|
|
|
|
+ // Set hostPort to a range only if there is a single container port
|
|
|
|
+ // and a dynamic host port.
|
|
|
|
+ if startPort == endPort && startHostPort != endHostPort {
|
|
|
|
+ hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
|
|
|
|
+ }
|
|
port, err := NewPort(strings.ToLower(proto), containerPort)
|
|
port, err := NewPort(strings.ToLower(proto), containerPort)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, nil, err
|
|
return nil, nil, err
|