diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index e1a6a1d383..bc11a7e554 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -1100,6 +1100,9 @@ func makePortString(ports []api.ServicePort) string { for ix := range ports { port := &ports[ix] pieces[ix] = fmt.Sprintf("%d/%s", port.Port, port.Protocol) + if port.NodePort > 0 { + pieces[ix] = fmt.Sprintf("%d:%d/%s", port.Port, port.NodePort, port.Protocol) + } } return strings.Join(pieces, ",") } diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index 7478352c43..c18ead9e24 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -1572,3 +1572,53 @@ func TestPrintPodShowLabels(t *testing.T) { buf.Reset() } } + +func TestPrintService(t *testing.T) { + tests := []struct { + service api.Service + expect string + }{ + { + // Test name, cluster ip, port with protocol + api.Service{ + ObjectMeta: api.ObjectMeta{Name: "test1"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeClusterIP, + Ports: []api.ServicePort{ + {Protocol: "tcp", + Port: 2233}, + }, + ClusterIP: "0.0.0.0", + }, + }, + "test1\t0.0.0.0\t\t2233/tcp\t\n", + }, + { + // Test name, cluster ip, port:nodePort with protocol + api.Service{ + ObjectMeta: api.ObjectMeta{Name: "test2"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeClusterIP, + Ports: []api.ServicePort{ + {Protocol: "tcp", + Port: 8888, + NodePort: 9999, + }, + }, + ClusterIP: "10.9.8.7", + }, + }, + "test2\t10.9.8.7\t\t8888:9999/tcp\t\n", + }, + } + + buf := bytes.NewBuffer([]byte{}) + for _, test := range tests { + printService(&test.service, buf, PrintOptions{false, false, false, false, true, false, false, "", []string{}}) + // We ignore time + if buf.String() != test.expect { + t.Fatalf("Expected: %s, got: %s %d", test.expect, buf.String(), strings.Compare(test.expect, buf.String())) + } + buf.Reset() + } +}