diff --git a/nat/nat.go b/nat/nat.go index 3805b2e..1fbb13e 100644 --- a/nat/nat.go +++ b/nat/nat.go @@ -13,22 +13,28 @@ import ( ) const ( - PortSpecTemplate = "ip:hostPort:containerPort" - PortSpecTemplateFormat = "ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort" + // portSpecTemplate is the expected format for port specifications + portSpecTemplate = "ip:hostPort:containerPort" ) +// PortBinding represents a binding between a Host IP address and a Host Port type PortBinding struct { - HostIp string + // HostIP is the host IP Address + HostIP string `json:"HostIp"` + // HostPort is the host port number HostPort string } +// PortMap is a collection of PortBinding indexed by Port type PortMap map[Port][]PortBinding +// PortSet is a collection of structs indexed by Port type PortSet map[Port]struct{} -// 80/tcp +// Port is a string containing port number and protocol in the format "80/tcp" type Port string +// NewPort creates a new instance of a Port given a protocol and port number func NewPort(proto, port string) (Port, error) { // Check for parsing issues on "port" now so we can avoid having // to check it later on. @@ -41,6 +47,7 @@ func NewPort(proto, port string) (Port, error) { return Port(fmt.Sprintf("%d/%s", portInt, proto)), nil } +// ParsePort parses the port number string and returns an int func ParsePort(rawPort string) (int, error) { if len(rawPort) == 0 { return 0, nil @@ -52,16 +59,19 @@ func ParsePort(rawPort string) (int, error) { return int(port), nil } +// Proto returns the protocol of a Port func (p Port) Proto() string { proto, _ := SplitProtoPort(string(p)) return proto } +// Port returns the port number of a Port func (p Port) Port() string { _, port := SplitProtoPort(string(p)) return port } +// Int returns the port number of a Port as an int func (p Port) Int() int { portStr := p.Port() if len(portStr) == 0 { @@ -74,7 +84,7 @@ func (p Port) Int() int { return int(port) } -// 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) { parts := strings.Split(rawPort, "/") l := len(parts) @@ -99,8 +109,8 @@ func validateProto(proto string) bool { return false } -// We will receive port specs in the format of ip:public:private/proto and these need to be -// parsed in the internal types +// ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses +// these in to the internal types func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) { var ( exposedPorts = make(map[Port]struct{}, len(ports)) @@ -120,19 +130,19 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, rawPort = fmt.Sprintf(":%s", rawPort) } - parts, err := parsers.PartParser(PortSpecTemplate, rawPort) + parts, err := parsers.PartParser(portSpecTemplate, rawPort) if err != nil { return nil, nil, err } var ( containerPort = parts["containerPort"] - rawIp = parts["ip"] + rawIP = parts["ip"] hostPort = parts["hostPort"] ) - if rawIp != "" && net.ParseIP(rawIp) == nil { - return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIp) + if rawIP != "" && net.ParseIP(rawIP) == nil { + return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP) } if containerPort == "" { return nil, nil, fmt.Errorf("No port specified: %s", rawPort) @@ -173,7 +183,7 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, } binding := PortBinding{ - HostIp: rawIp, + HostIP: rawIP, HostPort: hostPort, } bslice, exists := bindings[port] diff --git a/nat/nat_test.go b/nat/nat_test.go index 0262a53..77baa45 100644 --- a/nat/nat_test.go +++ b/nat/nat_test.go @@ -133,8 +133,8 @@ func TestParsePortSpecs(t *testing.T) { t.Fatalf("%s should have exactly one binding", portspec) } - if bindings[0].HostIp != "" { - t.Fatalf("HostIp should not be set for %s", portspec) + if bindings[0].HostIP != "" { + t.Fatalf("HostIP should not be set for %s", portspec) } if bindings[0].HostPort != "" { @@ -163,8 +163,8 @@ func TestParsePortSpecs(t *testing.T) { t.Fatalf("%s should have exactly one binding", portspec) } - if bindings[0].HostIp != "" { - t.Fatalf("HostIp should not be set for %s", portspec) + if bindings[0].HostIP != "" { + t.Fatalf("HostIP should not be set for %s", portspec) } if bindings[0].HostPort != port { @@ -193,8 +193,8 @@ func TestParsePortSpecs(t *testing.T) { t.Fatalf("%s should have exactly one binding", portspec) } - if bindings[0].HostIp != "0.0.0.0" { - t.Fatalf("HostIp is not 0.0.0.0 for %s", portspec) + if bindings[0].HostIP != "0.0.0.0" { + t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec) } if bindings[0].HostPort != port { @@ -235,8 +235,8 @@ func TestParsePortSpecsWithRange(t *testing.T) { t.Fatalf("%s should have exactly one binding", portspec) } - if bindings[0].HostIp != "" { - t.Fatalf("HostIp should not be set for %s", portspec) + if bindings[0].HostIP != "" { + t.Fatalf("HostIP should not be set for %s", portspec) } if bindings[0].HostPort != "" { @@ -264,8 +264,8 @@ func TestParsePortSpecsWithRange(t *testing.T) { t.Fatalf("%s should have exactly one binding", portspec) } - if bindings[0].HostIp != "" { - t.Fatalf("HostIp should not be set for %s", portspec) + if bindings[0].HostIP != "" { + t.Fatalf("HostIP should not be set for %s", portspec) } if bindings[0].HostPort != port { @@ -289,7 +289,7 @@ func TestParsePortSpecsWithRange(t *testing.T) { for portspec, bindings := range bindingMap { _, port := SplitProtoPort(string(portspec)) - if len(bindings) != 1 || bindings[0].HostIp != "0.0.0.0" || bindings[0].HostPort != port { + if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port { t.Fatalf("Expect single binding to port %s but found %s", port, bindings) } } @@ -337,7 +337,7 @@ func TestParseNetworkOptsPrivateOnly(t *testing.T) { t.Logf("Expected \"\" got %s", s.HostPort) t.Fail() } - if s.HostIp != "192.168.1.100" { + if s.HostIP != "192.168.1.100" { t.Fail() } } @@ -379,7 +379,7 @@ func TestParseNetworkOptsPublic(t *testing.T) { t.Logf("Expected 8080 got %s", s.HostPort) t.Fail() } - if s.HostIp != "192.168.1.100" { + if s.HostIP != "192.168.1.100" { t.Fail() } } @@ -454,7 +454,7 @@ func TestParseNetworkOptsUdp(t *testing.T) { t.Logf("Expected \"\" got %s", s.HostPort) t.Fail() } - if s.HostIp != "192.168.1.100" { + if s.HostIP != "192.168.1.100" { t.Fail() } } diff --git a/nat/sort.go b/nat/sort.go index fa584c1..0a9dd07 100644 --- a/nat/sort.go +++ b/nat/sort.go @@ -26,6 +26,9 @@ func (s *portSorter) Less(i, j int) bool { return s.by(ip, jp) } +// Sort sorts a list of ports using the provided predicate +// This function should compare `i` and `j`, returning true if `i` is +// considered to be less than `j` func Sort(ports []Port, predicate func(i, j Port) bool) { s := &portSorter{ports, predicate} sort.Sort(s) diff --git a/nat/sort_test.go b/nat/sort_test.go index ba24cdb..88ed911 100644 --- a/nat/sort_test.go +++ b/nat/sort_test.go @@ -59,10 +59,10 @@ func TestSortPortMap(t *testing.T) { }, Port("6379/tcp"): []PortBinding{ {}, - {HostIp: "0.0.0.0", HostPort: "32749"}, + {HostIP: "0.0.0.0", HostPort: "32749"}, }, Port("9999/tcp"): []PortBinding{ - {HostIp: "0.0.0.0", HostPort: "40000"}, + {HostIP: "0.0.0.0", HostPort: "40000"}, }, } @@ -77,7 +77,7 @@ func TestSortPortMap(t *testing.T) { t.Errorf("failed to prioritize port with explicit mappings, got %v", ports) } if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{ - {HostIp: "0.0.0.0", HostPort: "32749"}, + {HostIP: "0.0.0.0", HostPort: "32749"}, {}, }) { t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm)