Browse code

Merge pull request #14804 from dave-tucker/golint_nat

golint: Fix issues in pkg/nat

Jessie Frazelle authored on 2015/07/22 12:38:40
Showing 8 changed files
... ...
@@ -54,7 +54,7 @@ func (cli *DockerCli) CmdPort(args ...string) error {
54 54
 		}
55 55
 		if frontends, exists := c.NetworkSettings.Ports[newP]; exists && frontends != nil {
56 56
 			for _, frontend := range frontends {
57
-				fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIp, frontend.HostPort)
57
+				fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIP, frontend.HostPort)
58 58
 			}
59 59
 			return nil
60 60
 		}
... ...
@@ -63,7 +63,7 @@ func (cli *DockerCli) CmdPort(args ...string) error {
63 63
 
64 64
 	for from, frontends := range c.NetworkSettings.Ports {
65 65
 		for _, frontend := range frontends {
66
-			fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIp, frontend.HostPort)
66
+			fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIP, frontend.HostPort)
67 67
 		}
68 68
 	}
69 69
 
... ...
@@ -541,7 +541,7 @@ func (container *Container) buildPortMapInfo(n libnetwork.Network, ep libnetwork
541 541
 			if err != nil {
542 542
 				return nil, err
543 543
 			}
544
-			natBndg := nat.PortBinding{HostIp: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
544
+			natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
545 545
 			networkSettings.Ports[natPort] = append(networkSettings.Ports[natPort], natBndg)
546 546
 		}
547 547
 	}
... ...
@@ -690,7 +690,7 @@ func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointO
690 690
 			bindings[p] = []nat.PortBinding{}
691 691
 			for _, bb := range b {
692 692
 				bindings[p] = append(bindings[p], nat.PortBinding{
693
-					HostIp:   bb.HostIp,
693
+					HostIP:   bb.HostIP,
694 694
 					HostPort: bb.HostPort,
695 695
 				})
696 696
 			}
... ...
@@ -721,7 +721,7 @@ func (container *Container) buildCreateEndpointOptions() ([]libnetwork.EndpointO
721 721
 				return nil, fmt.Errorf("Error parsing HostPort value(%s):%v", binding[i].HostPort, err)
722 722
 			}
723 723
 			pbCopy.HostPort = uint16(newP.Int())
724
-			pbCopy.HostIP = net.ParseIP(binding[i].HostIp)
724
+			pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
725 725
 			pbList = append(pbList, pbCopy)
726 726
 		}
727 727
 
... ...
@@ -178,7 +178,7 @@ func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container,
178 178
 					PrivatePort: p,
179 179
 					PublicPort:  h,
180 180
 					Type:        port.Proto(),
181
-					IP:          binding.HostIp,
181
+					IP:          binding.HostIP,
182 182
 				})
183 183
 			}
184 184
 		}
... ...
@@ -879,7 +879,7 @@ func (s *DockerSuite) TestContainerApiBadPort(c *check.C) {
879 879
 		"PortBindings": map[string]interface{}{
880 880
 			"8080/tcp": []map[string]interface{}{
881 881
 				{
882
-					"HostIp":   "",
882
+					"HostIP":   "",
883 883
 					"HostPort": "aa80",
884 884
 				},
885 885
 			},
... ...
@@ -13,22 +13,28 @@ import (
13 13
 )
14 14
 
15 15
 const (
16
-	PortSpecTemplate       = "ip:hostPort:containerPort"
17
-	PortSpecTemplateFormat = "ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort"
16
+	// portSpecTemplate is the expected format for port specifications
17
+	portSpecTemplate = "ip:hostPort:containerPort"
18 18
 )
19 19
 
20
+// PortBinding represents a binding between a Host IP address and a Host Port
20 21
 type PortBinding struct {
21
-	HostIp   string
22
+	// HostIP is the host IP Address
23
+	HostIP string `json:"HostIp"`
24
+	// HostPort is the host port number
22 25
 	HostPort string
23 26
 }
24 27
 
28
+// PortMap is a collection of PortBinding indexed by Port
25 29
 type PortMap map[Port][]PortBinding
26 30
 
31
+// PortSet is a collection of structs indexed by Port
27 32
 type PortSet map[Port]struct{}
28 33
 
29
-// 80/tcp
34
+// Port is a string containing port number and protocol in the format "80/tcp"
30 35
 type Port string
31 36
 
37
+// NewPort creates a new instance of a Port given a protocol and port number
32 38
 func NewPort(proto, port string) (Port, error) {
33 39
 	// Check for parsing issues on "port" now so we can avoid having
34 40
 	// to check it later on.
... ...
@@ -41,6 +47,7 @@ func NewPort(proto, port string) (Port, error) {
41 41
 	return Port(fmt.Sprintf("%d/%s", portInt, proto)), nil
42 42
 }
43 43
 
44
+// ParsePort parses the port number string and returns an int
44 45
 func ParsePort(rawPort string) (int, error) {
45 46
 	if len(rawPort) == 0 {
46 47
 		return 0, nil
... ...
@@ -52,16 +59,19 @@ func ParsePort(rawPort string) (int, error) {
52 52
 	return int(port), nil
53 53
 }
54 54
 
55
+// Proto returns the protocol of a Port
55 56
 func (p Port) Proto() string {
56 57
 	proto, _ := SplitProtoPort(string(p))
57 58
 	return proto
58 59
 }
59 60
 
61
+// Port returns the port number of a Port
60 62
 func (p Port) Port() string {
61 63
 	_, port := SplitProtoPort(string(p))
62 64
 	return port
63 65
 }
64 66
 
67
+// Int returns the port number of a Port as an int
65 68
 func (p Port) Int() int {
66 69
 	portStr := p.Port()
67 70
 	if len(portStr) == 0 {
... ...
@@ -74,7 +84,7 @@ func (p Port) Int() int {
74 74
 	return int(port)
75 75
 }
76 76
 
77
-// Splits a port in the format of proto/port
77
+// SplitProtoPort splits a port in the format of proto/port
78 78
 func SplitProtoPort(rawPort string) (string, string) {
79 79
 	parts := strings.Split(rawPort, "/")
80 80
 	l := len(parts)
... ...
@@ -99,8 +109,8 @@ func validateProto(proto string) bool {
99 99
 	return false
100 100
 }
101 101
 
102
-// We will receive port specs in the format of ip:public:private/proto and these need to be
103
-// parsed in the internal types
102
+// ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
103
+// these in to the internal types
104 104
 func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
105 105
 	var (
106 106
 		exposedPorts = make(map[Port]struct{}, len(ports))
... ...
@@ -120,19 +130,19 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
120 120
 			rawPort = fmt.Sprintf(":%s", rawPort)
121 121
 		}
122 122
 
123
-		parts, err := parsers.PartParser(PortSpecTemplate, rawPort)
123
+		parts, err := parsers.PartParser(portSpecTemplate, rawPort)
124 124
 		if err != nil {
125 125
 			return nil, nil, err
126 126
 		}
127 127
 
128 128
 		var (
129 129
 			containerPort = parts["containerPort"]
130
-			rawIp         = parts["ip"]
130
+			rawIP         = parts["ip"]
131 131
 			hostPort      = parts["hostPort"]
132 132
 		)
133 133
 
134
-		if rawIp != "" && net.ParseIP(rawIp) == nil {
135
-			return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIp)
134
+		if rawIP != "" && net.ParseIP(rawIP) == nil {
135
+			return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIP)
136 136
 		}
137 137
 		if containerPort == "" {
138 138
 			return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
... ...
@@ -173,7 +183,7 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
173 173
 			}
174 174
 
175 175
 			binding := PortBinding{
176
-				HostIp:   rawIp,
176
+				HostIP:   rawIP,
177 177
 				HostPort: hostPort,
178 178
 			}
179 179
 			bslice, exists := bindings[port]
... ...
@@ -133,8 +133,8 @@ func TestParsePortSpecs(t *testing.T) {
133 133
 			t.Fatalf("%s should have exactly one binding", portspec)
134 134
 		}
135 135
 
136
-		if bindings[0].HostIp != "" {
137
-			t.Fatalf("HostIp should not be set for %s", portspec)
136
+		if bindings[0].HostIP != "" {
137
+			t.Fatalf("HostIP should not be set for %s", portspec)
138 138
 		}
139 139
 
140 140
 		if bindings[0].HostPort != "" {
... ...
@@ -163,8 +163,8 @@ func TestParsePortSpecs(t *testing.T) {
163 163
 			t.Fatalf("%s should have exactly one binding", portspec)
164 164
 		}
165 165
 
166
-		if bindings[0].HostIp != "" {
167
-			t.Fatalf("HostIp should not be set for %s", portspec)
166
+		if bindings[0].HostIP != "" {
167
+			t.Fatalf("HostIP should not be set for %s", portspec)
168 168
 		}
169 169
 
170 170
 		if bindings[0].HostPort != port {
... ...
@@ -193,8 +193,8 @@ func TestParsePortSpecs(t *testing.T) {
193 193
 			t.Fatalf("%s should have exactly one binding", portspec)
194 194
 		}
195 195
 
196
-		if bindings[0].HostIp != "0.0.0.0" {
197
-			t.Fatalf("HostIp is not 0.0.0.0 for %s", portspec)
196
+		if bindings[0].HostIP != "0.0.0.0" {
197
+			t.Fatalf("HostIP is not 0.0.0.0 for %s", portspec)
198 198
 		}
199 199
 
200 200
 		if bindings[0].HostPort != port {
... ...
@@ -235,8 +235,8 @@ func TestParsePortSpecsWithRange(t *testing.T) {
235 235
 			t.Fatalf("%s should have exactly one binding", portspec)
236 236
 		}
237 237
 
238
-		if bindings[0].HostIp != "" {
239
-			t.Fatalf("HostIp should not be set for %s", portspec)
238
+		if bindings[0].HostIP != "" {
239
+			t.Fatalf("HostIP should not be set for %s", portspec)
240 240
 		}
241 241
 
242 242
 		if bindings[0].HostPort != "" {
... ...
@@ -264,8 +264,8 @@ func TestParsePortSpecsWithRange(t *testing.T) {
264 264
 			t.Fatalf("%s should have exactly one binding", portspec)
265 265
 		}
266 266
 
267
-		if bindings[0].HostIp != "" {
268
-			t.Fatalf("HostIp should not be set for %s", portspec)
267
+		if bindings[0].HostIP != "" {
268
+			t.Fatalf("HostIP should not be set for %s", portspec)
269 269
 		}
270 270
 
271 271
 		if bindings[0].HostPort != port {
... ...
@@ -289,7 +289,7 @@ func TestParsePortSpecsWithRange(t *testing.T) {
289 289
 
290 290
 	for portspec, bindings := range bindingMap {
291 291
 		_, port := SplitProtoPort(string(portspec))
292
-		if len(bindings) != 1 || bindings[0].HostIp != "0.0.0.0" || bindings[0].HostPort != port {
292
+		if len(bindings) != 1 || bindings[0].HostIP != "0.0.0.0" || bindings[0].HostPort != port {
293 293
 			t.Fatalf("Expect single binding to port %s but found %s", port, bindings)
294 294
 		}
295 295
 	}
... ...
@@ -337,7 +337,7 @@ func TestParseNetworkOptsPrivateOnly(t *testing.T) {
337 337
 			t.Logf("Expected \"\" got %s", s.HostPort)
338 338
 			t.Fail()
339 339
 		}
340
-		if s.HostIp != "192.168.1.100" {
340
+		if s.HostIP != "192.168.1.100" {
341 341
 			t.Fail()
342 342
 		}
343 343
 	}
... ...
@@ -379,7 +379,7 @@ func TestParseNetworkOptsPublic(t *testing.T) {
379 379
 			t.Logf("Expected 8080 got %s", s.HostPort)
380 380
 			t.Fail()
381 381
 		}
382
-		if s.HostIp != "192.168.1.100" {
382
+		if s.HostIP != "192.168.1.100" {
383 383
 			t.Fail()
384 384
 		}
385 385
 	}
... ...
@@ -454,7 +454,7 @@ func TestParseNetworkOptsUdp(t *testing.T) {
454 454
 			t.Logf("Expected \"\" got %s", s.HostPort)
455 455
 			t.Fail()
456 456
 		}
457
-		if s.HostIp != "192.168.1.100" {
457
+		if s.HostIP != "192.168.1.100" {
458 458
 			t.Fail()
459 459
 		}
460 460
 	}
... ...
@@ -26,6 +26,9 @@ func (s *portSorter) Less(i, j int) bool {
26 26
 	return s.by(ip, jp)
27 27
 }
28 28
 
29
+// Sort sorts a list of ports using the provided predicate
30
+// This function should compare `i` and `j`, returning true if `i` is
31
+// considered to be less than `j`
29 32
 func Sort(ports []Port, predicate func(i, j Port) bool) {
30 33
 	s := &portSorter{ports, predicate}
31 34
 	sort.Sort(s)
... ...
@@ -59,10 +59,10 @@ func TestSortPortMap(t *testing.T) {
59 59
 		},
60 60
 		Port("6379/tcp"): []PortBinding{
61 61
 			{},
62
-			{HostIp: "0.0.0.0", HostPort: "32749"},
62
+			{HostIP: "0.0.0.0", HostPort: "32749"},
63 63
 		},
64 64
 		Port("9999/tcp"): []PortBinding{
65
-			{HostIp: "0.0.0.0", HostPort: "40000"},
65
+			{HostIP: "0.0.0.0", HostPort: "40000"},
66 66
 		},
67 67
 	}
68 68
 
... ...
@@ -77,7 +77,7 @@ func TestSortPortMap(t *testing.T) {
77 77
 		t.Errorf("failed to prioritize port with explicit mappings, got %v", ports)
78 78
 	}
79 79
 	if pm := portMap[Port("6379/tcp")]; !reflect.DeepEqual(pm, []PortBinding{
80
-		{HostIp: "0.0.0.0", HostPort: "32749"},
80
+		{HostIP: "0.0.0.0", HostPort: "32749"},
81 81
 		{},
82 82
 	}) {
83 83
 		t.Errorf("failed to prioritize bindings with explicit mappings, got %v", pm)