From 01e13f75efa1159047dea52cccdbc078a9461b19 Mon Sep 17 00:00:00 2001 From: duanmeng Date: Wed, 27 Apr 2022 10:43:08 +0800 Subject: [PATCH] improved the output of 'kubectl get cluster -o wide' Signed-off-by: duanmeng --- pkg/printers/internalversion/printers.go | 4 +++ pkg/printers/internalversion/printers_test.go | 25 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index 2e920289b..5c00bdf3c 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -19,6 +19,7 @@ func AddHandlers(h printers.PrintHandler) { {Name: "Mode", Type: "string", Description: "SyncMode describes how a cluster sync resources from karmada control plane."}, {Name: "Ready", Type: "string", Description: "The aggregate readiness state of this cluster for accepting workloads."}, {Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]}, + {Name: "APIEndpoint", Type: "string", Priority: 1, Description: "The API endpoint of the member cluster."}, } // ignore errors because we enable errcheck golangci-lint. _ = h.TableHandler(clusterColumnDefinitions, printClusterList) @@ -56,6 +57,9 @@ func printCluster(cluster *clusterapis.Cluster, options printers.GenerateOptions cluster.Spec.SyncMode, ready, translateTimestampSince(cluster.CreationTimestamp)) + if options.Wide { + row.Cells = append(row.Cells, cluster.Spec.APIEndpoint) + } return []metav1.TableRow{row}, nil } diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index 1fb42e4e8..f3e2970a9 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -13,8 +13,9 @@ import ( func TestPrintCluster(t *testing.T) { tests := []struct { - cluster clusterapis.Cluster - expect []metav1.TableRow + cluster clusterapis.Cluster + generateOptions printers.GenerateOptions + expect []metav1.TableRow }{ // Test name, kubernetes version, sync mode, cluster ready status, { @@ -30,12 +31,30 @@ func TestPrintCluster(t *testing.T) { }, }, }, + printers.GenerateOptions{Wide: false}, []metav1.TableRow{{Cells: []interface{}{"test1", "1.21.7", clusterapis.ClusterSyncMode("Push"), "True", ""}}}, }, + { + clusterapis.Cluster{ + ObjectMeta: metav1.ObjectMeta{Name: "test2"}, + Spec: clusterapis.ClusterSpec{ + SyncMode: clusterapis.Push, + APIEndpoint: "https://kubernetes.default.svc.cluster.local:6443", + }, + Status: clusterapis.ClusterStatus{ + KubernetesVersion: "1.21.7", + Conditions: []metav1.Condition{ + {Type: clusterapis.ClusterConditionReady, Status: metav1.ConditionTrue}, + }, + }, + }, + printers.GenerateOptions{Wide: true}, + []metav1.TableRow{{Cells: []interface{}{"test2", "1.21.7", clusterapis.ClusterSyncMode("Push"), "True", "", "https://kubernetes.default.svc.cluster.local:6443"}}}, + }, } for i, test := range tests { - rows, err := printCluster(&test.cluster, printers.GenerateOptions{}) + rows, err := printCluster(&test.cluster, test.generateOptions) if err != nil { t.Fatal(err) }