From c3b854de62a20a55b1504ee92e0f8cd4958145f7 Mon Sep 17 00:00:00 2001 From: hejunhua Date: Wed, 10 Jul 2024 11:07:07 +0800 Subject: [PATCH] Add more columns for cluster Signed-off-by: whitewindmills --- pkg/printers/internalversion/printers.go | 31 +++++++++++++++++-- pkg/printers/internalversion/printers_test.go | 9 +++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index bf8eba4c1061..77e7151c721d 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -17,6 +17,7 @@ limitations under the License. package internalversion import ( + "strings" "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -35,7 +36,11 @@ 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."}, + {Name: "Zones", Type: "string", Priority: 1, Description: "Zones represents the failure zones(also called availability zones) of the member cluster. The zones are presented as a slice to support the case that cluster runs across multiple failure zones."}, + {Name: "Region", Type: "string", Priority: 1, Description: "Region represents the region of the member cluster locate in."}, + {Name: "Provider", Type: "string", Priority: 1, Description: "Provider represents the cloud provider name of the member cluster."}, + {Name: "API-Endpoint", Type: "string", Priority: 1, Description: "The API endpoint of the member cluster."}, + {Name: "Proxy-URL", Type: "string", Priority: 1, Description: "ProxyURL is the proxy URL for the cluster."}, } // ignore errors because we enable errcheck golangci-lint. _ = h.TableHandler(clusterColumnDefinitions, printClusterList) @@ -74,7 +79,13 @@ func printCluster(cluster *clusterapis.Cluster, options printers.GenerateOptions ready, translateTimestampSince(cluster.CreationTimestamp)) if options.Wide { - row.Cells = append(row.Cells, cluster.Spec.APIEndpoint) + row.Cells = append( + row.Cells, + translateZones(cluster.Spec.Zones), + translateOptionalStringField(cluster.Spec.Region), + translateOptionalStringField(cluster.Spec.Provider), + cluster.Spec.APIEndpoint, + translateOptionalStringField(cluster.Spec.ProxyURL)) } return []metav1.TableRow{row}, nil } @@ -88,3 +99,19 @@ func translateTimestampSince(timestamp metav1.Time) string { return duration.HumanDuration(time.Since(timestamp.Time)) } + +func translateOptionalStringField(field string) string { + if len(field) == 0 { + return "" + } + + return field +} + +func translateZones(zones []string) string { + if len(zones) == 0 { + return "" + } + + return strings.Join(zones, ",") +} diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index 2a29e501d5fb..69806c76a822 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -56,6 +56,8 @@ func TestPrintCluster(t *testing.T) { Spec: clusterapis.ClusterSpec{ SyncMode: clusterapis.Push, APIEndpoint: "https://kubernetes.default.svc.cluster.local:6443", + ProxyURL: "https://anp-server.default.svc.cluster.local:443", + Zones: []string{"foo", "bar"}, }, Status: clusterapis.ClusterStatus{ KubernetesVersion: "1.24.2", @@ -65,7 +67,12 @@ func TestPrintCluster(t *testing.T) { }, }, printers.GenerateOptions{Wide: true}, - []metav1.TableRow{{Cells: []interface{}{"test2", "1.24.2", clusterapis.ClusterSyncMode("Push"), "True", "", "https://kubernetes.default.svc.cluster.local:6443"}}}, + []metav1.TableRow{{Cells: []interface{}{"test2", "1.24.2", clusterapis.ClusterSyncMode("Push"), "True", "", + "foo,bar", + "", + "", + "https://kubernetes.default.svc.cluster.local:6443", + "https://anp-server.default.svc.cluster.local:443"}}}, }, }