diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index 44f78bbb3be..7a6e76916a2 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -126,8 +126,8 @@ func printObjectSpecificMessage(obj runtime.Object, out io.Writer) { An external load-balanced service was created. On many platforms (e.g. Google Compute Engine), you will also need to explicitly open a Firewall rule for the service port(s) (%s) to serve traffic. - See https://github.com/GoogleCloudPlatform/kubernetes/tree/master/docs/services-firewall.md for more details. - `, makePortsString(obj.Spec.Ports)) + See https://github.com/GoogleCloudPlatform/kubernetes/tree/master/docs/services-firewalls.md for more details. + `, makePortsString(obj.Spec.Ports, false)) out.Write([]byte(msg)) } if obj.Spec.Type == api.ServiceTypeNodePort { @@ -136,17 +136,23 @@ func printObjectSpecificMessage(obj runtime.Object, out io.Writer) { If you want to expose this service to the external internet, you may need to set up firewall rules for the service port(s) (%s) to serve traffic. - See https://github.com/GoogleCloudPlatform/kubernetes/tree/master/docs/services-firewall.md for more details. - `, makePortsString(obj.Spec.Ports)) + See https://github.com/GoogleCloudPlatform/kubernetes/tree/master/docs/services-firewalls.md for more details. + `, makePortsString(obj.Spec.Ports, true)) out.Write([]byte(msg)) } } } -func makePortsString(ports []api.ServicePort) string { +func makePortsString(ports []api.ServicePort, useNodePort bool) string { pieces := make([]string, len(ports)) for ix := range ports { - pieces[ix] = fmt.Sprintf("%s:%d", strings.ToLower(string(ports[ix].Protocol)), ports[ix].Port) + var port int + if useNodePort { + port = ports[ix].NodePort + } else { + port = ports[ix].Port + } + pieces[ix] = fmt.Sprintf("%s:%d", strings.ToLower(string(ports[ix].Protocol)), port) } return strings.Join(pieces, ",") } diff --git a/pkg/kubectl/cmd/create_test.go b/pkg/kubectl/cmd/create_test.go index 1d2418b5979..82c527b04e9 100644 --- a/pkg/kubectl/cmd/create_test.go +++ b/pkg/kubectl/cmd/create_test.go @@ -169,6 +169,7 @@ func TestPrintObjectSpecificMessage(t *testing.T) { func TestMakePortsString(t *testing.T) { tests := []struct { ports []api.ServicePort + useNodePort bool expectedOutput string }{ {ports: nil, expectedOutput: ""}, @@ -197,9 +198,24 @@ func TestMakePortsString(t *testing.T) { }, expectedOutput: "tcp:80,udp:8080,tcp:9000", }, + {ports: []api.ServicePort{ + { + Port: 80, + NodePort: 9090, + Protocol: "TCP", + }, + { + Port: 8080, + NodePort: 80, + Protocol: "UDP", + }, + }, + useNodePort: true, + expectedOutput: "tcp:9090,udp:80", + }, } for _, test := range tests { - output := makePortsString(test.ports) + output := makePortsString(test.ports, test.useNodePort) if output != test.expectedOutput { t.Errorf("expected: %s, saw: %s.", test.expectedOutput, output) }