From 15d01d6e6c57f4b8a39dddd2676a2d6914c62c77 Mon Sep 17 00:00:00 2001
From: Dave Tucker
Date: Tue, 21 Jul 2015 14:43:32 +0100
Subject: [PATCH] golint: Fix issues in pkg/nat
Updates #14756
Signed-off-by: Dave Tucker
---
api/client/port.go | 4 +--
daemon/container_unix.go | 6 ++--
daemon/list.go | 2 +-
integration-cli/docker_api_containers_test.go | 2 +-
pkg/nat/nat.go | 34 ++++++++++++-------
pkg/nat/nat_test.go | 28 +++++++--------
pkg/nat/sort.go | 3 ++
pkg/nat/sort_test.go | 6 ++--
8 files changed, 49 insertions(+), 36 deletions(-)
diff --git a/api/client/port.go b/api/client/port.go
index 09d6b5bd9f..9f83aa47a1 100644
--- a/api/client/port.go
+++ b/api/client/port.go
@@ -54,7 +54,7 @@ func (cli *DockerCli) CmdPort(args ...string) error {
}
if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil {
for _, frontend := range frontends {
- fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIp, frontend.HostPort)
+ fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIP, frontend.HostPort)
}
return nil
}
@@ -63,7 +63,7 @@ func (cli *DockerCli) CmdPort(args ...string) error {
for from, frontends := range c.NetworkSettings.Ports {
for _, frontend := range frontends {
- fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIp, frontend.HostPort)
+ fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIP, frontend.HostPort)
}
}
diff --git a/daemon/container_unix.go b/daemon/container_unix.go
index 24097cb015..25f7a3abc7 100644
--- a/daemon/container_unix.go
+++ b/daemon/container_unix.go
@@ -541,7 +541,7 @@ func (container *Container) buildPortMapInfo(n libnetwork.Network, ep libnetwork
if err != nil {
return nil, err
}
- natBndg := nat.PortBinding{HostIp: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
+ natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg)
}
}
@@ -690,7 +690,7 @@ func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointO
bindings[p] = []nat.PortBinding{}
for _, bb := range b {
bindings[p] = append(bindings[p], nat.PortBinding{
- HostIp: bb.HostIp,
+ HostIP: bb.HostIP,
HostPort: bb.HostPort,
})
}
@@ -721,7 +721,7 @@ func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointO
return nil, fmt.Errorf("Error parsing HostPort value(%s):%v", binding[i].HostPort, err)
}
pbCopy.HostPort = uint16(newP.Int())
- pbCopy.HostIP = net.ParseIP(binding[i].HostIp)
+ pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
pbList = append(pbList, pbCopy)
}
diff --git a/daemon/list.go b/daemon/list.go
index 3e00023fcf..5c8315cb20 100644
--- a/daemon/list.go
+++ b/daemon/list.go
@@ -178,7 +178,7 @@ func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container,
PrivatePort: p,
PublicPort: h,
Type: port.Proto(),
- IP: binding.HostIp,
+ IP: binding.HostIP,
})
}
}
diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go
index 193c66fd7e..c842d9f281 100644
--- a/integration-cli/docker_api_containers_test.go
+++ b/integration-cli/docker_api_containers_test.go
@@ -879,7 +879,7 @@ func (s *DockerSuite) TestContainerApiBadPort(c *check.C) {
"PortBindings": map[string]interface{}{
"8080/tcp": []map[string]interface{}{
{
- "HostIp": "",
+ "HostIP": "",
"HostPort": "aa80",
},
},
diff --git a/pkg/nat/nat.go b/pkg/nat/nat.go
index 3805b2e853..1fbb13e6e9 100644
--- a/pkg/nat/nat.go
+++ b/pkg/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/pkg/nat/nat_test.go b/pkg/nat/nat_test.go
index 0262a53f0b..77baa4576c 100644
--- a/pkg/nat/nat_test.go
+++ b/pkg/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/pkg/nat/sort.go b/pkg/nat/sort.go
index fa584c173b..0a9dd078f0 100644
--- a/pkg/nat/sort.go
+++ b/pkg/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/pkg/nat/sort_test.go b/pkg/nat/sort_test.go
index ba24cdbcb9..88ed911156 100644
--- a/pkg/nat/sort_test.go
+++ b/pkg/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)