improved the output of 'kubectl get cluster -o wide'

Signed-off-by: duanmeng <duanmeng_yewu@cmss.chinamobile.com>
This commit is contained in:
duanmeng 2022-04-27 10:43:08 +08:00
parent 6de167e209
commit 01e13f75ef
2 changed files with 26 additions and 3 deletions

View File

@ -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
}

View File

@ -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", "<unknown>"}}},
},
{
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", "<unknown>", "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)
}