diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index 61d70784da8..008879869f4 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -666,8 +666,12 @@ func getServiceExternalIP(svc *api.Service, wide bool) string { case api.ServiceTypeLoadBalancer: lbIps := loadBalancerStatusStringer(svc.Status.LoadBalancer, wide) if len(svc.Spec.ExternalIPs) > 0 { - result := append(strings.Split(lbIps, ","), svc.Spec.ExternalIPs...) - return strings.Join(result, ",") + results := []string{} + if len(lbIps) > 0 { + results = append(results, strings.Split(lbIps, ",")...) + } + results = append(results, svc.Spec.ExternalIPs...) + return strings.Join(results, ",") } if len(lbIps) > 0 { return lbIps @@ -696,7 +700,14 @@ func printService(svc *api.Service, w io.Writer, options printers.PrintOptions) namespace := svc.Namespace svcType := svc.Spec.Type internalIP := svc.Spec.ClusterIP + if len(internalIP) == 0 { + internalIP = "" + } externalIP := getServiceExternalIP(svc, options.Wide) + svcPorts := makePortString(svc.Spec.Ports) + if len(svcPorts) == 0 { + svcPorts = "" + } if options.WithNamespace { if _, err := fmt.Fprintf(w, "%s\t", namespace); err != nil { @@ -708,7 +719,7 @@ func printService(svc *api.Service, w io.Writer, options printers.PrintOptions) string(svcType), internalIP, externalIP, - makePortString(svc.Spec.Ports), + svcPorts, translateTimestamp(svc.CreationTimestamp), ); err != nil { return err diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index a2598773a91..ce7b94e740a 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -2322,6 +2322,8 @@ func TestPrintPodShowLabels(t *testing.T) { } func TestPrintService(t *testing.T) { + single_ExternalIP := []string{"80.11.12.10"} + mul_ExternalIP := []string{"80.11.12.10", "80.11.12.11"} tests := []struct { service api.Service expect string @@ -2333,8 +2335,10 @@ func TestPrintService(t *testing.T) { Spec: api.ServiceSpec{ Type: api.ServiceTypeClusterIP, Ports: []api.ServicePort{ - {Protocol: "tcp", - Port: 2233}, + { + Protocol: "tcp", + Port: 2233, + }, }, ClusterIP: "10.9.8.7", }, @@ -2342,13 +2346,14 @@ func TestPrintService(t *testing.T) { "test1\tClusterIP\t10.9.8.7\t\t2233/tcp\t\n", }, { - // Test name, cluster ip, port:nodePort with protocol + // Test NodePort service api.Service{ ObjectMeta: metav1.ObjectMeta{Name: "test2"}, Spec: api.ServiceSpec{ Type: api.ServiceTypeNodePort, Ports: []api.ServicePort{ - {Protocol: "tcp", + { + Protocol: "tcp", Port: 8888, NodePort: 9999, }, @@ -2358,6 +2363,112 @@ func TestPrintService(t *testing.T) { }, "test2\tNodePort\t10.9.8.7\t\t8888:9999/tcp\t\n", }, + { + // Test LoadBalancer service + api.Service{ + ObjectMeta: metav1.ObjectMeta{Name: "test3"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeLoadBalancer, + Ports: []api.ServicePort{ + { + Protocol: "tcp", + Port: 8888, + }, + }, + ClusterIP: "10.9.8.7", + }, + }, + "test3\tLoadBalancer\t10.9.8.7\t\t8888/tcp\t\n", + }, + { + // Test LoadBalancer service with single ExternalIP and no LoadBalancerStatus + api.Service{ + ObjectMeta: metav1.ObjectMeta{Name: "test4"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeLoadBalancer, + Ports: []api.ServicePort{ + { + Protocol: "tcp", + Port: 8888, + }, + }, + ClusterIP: "10.9.8.7", + ExternalIPs: single_ExternalIP, + }, + }, + "test4\tLoadBalancer\t10.9.8.7\t80.11.12.10\t8888/tcp\t\n", + }, + { + // Test LoadBalancer service with single ExternalIP + api.Service{ + ObjectMeta: metav1.ObjectMeta{Name: "test5"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeLoadBalancer, + Ports: []api.ServicePort{ + { + Protocol: "tcp", + Port: 8888, + }, + }, + ClusterIP: "10.9.8.7", + ExternalIPs: single_ExternalIP, + }, + Status: api.ServiceStatus{ + LoadBalancer: api.LoadBalancerStatus{ + Ingress: []api.LoadBalancerIngress{ + { + IP: "3.4.5.6", + Hostname: "test.cluster.com", + }, + }, + }, + }, + }, + "test5\tLoadBalancer\t10.9.8.7\t3.4.5.6,80.11.12.10\t8888/tcp\t\n", + }, + { + // Test LoadBalancer service with mul ExternalIPs + api.Service{ + ObjectMeta: metav1.ObjectMeta{Name: "test6"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeLoadBalancer, + Ports: []api.ServicePort{ + { + Protocol: "tcp", + Port: 8888, + }, + }, + ClusterIP: "10.9.8.7", + ExternalIPs: mul_ExternalIP, + }, + Status: api.ServiceStatus{ + LoadBalancer: api.LoadBalancerStatus{ + Ingress: []api.LoadBalancerIngress{ + { + IP: "2.3.4.5", + Hostname: "test.cluster.local", + }, + { + IP: "3.4.5.6", + Hostname: "test.cluster.com", + }, + }, + }, + }, + }, + "test6\tLoadBalancer\t10.9.8.7\t2.3.4.5,3.4.5.6,80.11.12.10,80.11.12.11\t8888/tcp\t\n", + }, + { + // Test ExternalName service + api.Service{ + ObjectMeta: metav1.ObjectMeta{Name: "test7"}, + Spec: api.ServiceSpec{ + Type: api.ServiceTypeExternalName, + ExternalName: "my.database.example.com", + }, + }, + "test7\tExternalName\t\tmy.database.example.com\t\t\n", + }, } buf := bytes.NewBuffer([]byte{})