From f01f9a9035ed3b00b7b229582e8141ff653abd7b Mon Sep 17 00:00:00 2001 From: FengyunPan Date: Sun, 18 Jun 2017 12:19:57 +0800 Subject: [PATCH] Output TYPE for getting service Now service already supported 4 ServiceTypes, ServiceTypes is friendly to distinguish services, so outputing service type better when running 'kubectl get service'. --- pkg/kubectl/cmd/cmd_test.go | 8 +++++--- pkg/printers/internalversion/printers.go | 10 +++++----- pkg/printers/internalversion/printers_test.go | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pkg/kubectl/cmd/cmd_test.go b/pkg/kubectl/cmd/cmd_test.go index 5ad282e0e73..20e307fdc46 100644 --- a/pkg/kubectl/cmd/cmd_test.go +++ b/pkg/kubectl/cmd/cmd_test.go @@ -594,6 +594,7 @@ func Example_printServiceWithNamespacesAndLabels() { "s": "magic", }, ClusterIP: "10.1.1.1", + Type: api.ServiceTypeClusterIP, }, Status: api.ServiceStatus{}, }, @@ -615,6 +616,7 @@ func Example_printServiceWithNamespacesAndLabels() { "s": "kazam", }, ClusterIP: "10.1.1.2", + Type: api.ServiceTypeClusterIP, }, Status: api.ServiceStatus{}, }}, @@ -627,9 +629,9 @@ func Example_printServiceWithNamespacesAndLabels() { fmt.Printf("Unexpected error: %v", err) } // Output: - // |NAMESPACE NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE L1| - // |ns1 svc1 10.1.1.1 53/UDP,53/TCP 10y value| - // |ns2 svc2 10.1.1.2 80/TCP,8080/TCP 10y dolla-bill-yall| + // |NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE L1| + // |ns1 svc1 ClusterIP 10.1.1.1 53/UDP,53/TCP 10y value| + // |ns2 svc2 ClusterIP 10.1.1.2 80/TCP,8080/TCP 10y dolla-bill-yall| // || } diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index a5bcedb8555..c3d478ef493 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -67,7 +67,7 @@ var ( jobColumns = []string{"NAME", "DESIRED", "SUCCESSFUL", "AGE"} cronJobColumns = []string{"NAME", "SCHEDULE", "SUSPEND", "ACTIVE", "LAST-SCHEDULE"} batchJobWideColumns = []string{"CONTAINER(S)", "IMAGE(S)", "SELECTOR"} - serviceColumns = []string{"NAME", "CLUSTER-IP", "EXTERNAL-IP", "PORT(S)", "AGE"} + serviceColumns = []string{"NAME", "TYPE", "CLUSTER-IP", "EXTERNAL-IP", "PORT(S)", "AGE"} serviceWideColumns = []string{"SELECTOR"} ingressColumns = []string{"NAME", "HOSTS", "ADDRESS", "PORTS", "AGE"} statefulSetColumns = []string{"NAME", "DESIRED", "CURRENT", "AGE"} @@ -711,7 +711,7 @@ func getServiceExternalIP(svc *api.Service, wide bool) string { if len(svc.Spec.ExternalIPs) > 0 { return strings.Join(svc.Spec.ExternalIPs, ",") } - return "" + return "" case api.ServiceTypeLoadBalancer: lbIps := loadBalancerStatusStringer(svc.Status.LoadBalancer, wide) if len(svc.Spec.ExternalIPs) > 0 { @@ -742,9 +742,8 @@ func makePortString(ports []api.ServicePort) string { func printService(svc *api.Service, w io.Writer, options printers.PrintOptions) error { name := printers.FormatResourceName(options.Kind, svc.Name, options.WithKind) - namespace := svc.Namespace - + svcType := svc.Spec.Type internalIP := svc.Spec.ClusterIP externalIP := getServiceExternalIP(svc, options.Wide) @@ -753,8 +752,9 @@ func printService(svc *api.Service, w io.Writer, options printers.PrintOptions) return err } } - if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s", + if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s", name, + string(svcType), internalIP, externalIP, makePortString(svc.Spec.Ports), diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index b1afd8ca326..b1212f9239d 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -2289,17 +2289,17 @@ func TestPrintService(t *testing.T) { {Protocol: "tcp", Port: 2233}, }, - ClusterIP: "0.0.0.0", + ClusterIP: "10.9.8.7", }, }, - "test1\t0.0.0.0\t\t2233/tcp\t\n", + "test1\tClusterIP\t10.9.8.7\t\t2233/tcp\t\n", }, { // Test name, cluster ip, port:nodePort with protocol api.Service{ ObjectMeta: metav1.ObjectMeta{Name: "test2"}, Spec: api.ServiceSpec{ - Type: api.ServiceTypeClusterIP, + Type: api.ServiceTypeNodePort, Ports: []api.ServicePort{ {Protocol: "tcp", Port: 8888, @@ -2309,7 +2309,7 @@ func TestPrintService(t *testing.T) { ClusterIP: "10.9.8.7", }, }, - "test2\t10.9.8.7\t\t8888:9999/tcp\t\n", + "test2\tNodePort\t10.9.8.7\t\t8888:9999/tcp\t\n", }, }