mirror of https://github.com/k3s-io/k3s
Merge pull request #53771 from dixudx/update_cluster_printer
Automatic merge from submit-queue (batch tested with PRs 53749, 53642, 53813, 53771, 53762). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. update cluster printer to enable --show-labels **What this PR does / why we need it**: **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #53729 **Special notes for your reviewer**: **Release note**: ```release-note update cluster printer to enable --show-labels ```pull/6/head
commit
53241b931c
|
@ -15,6 +15,7 @@ go_test(
|
|||
],
|
||||
library = ":go_default_library",
|
||||
deps = [
|
||||
"//federation/apis/federation:go_default_library",
|
||||
"//federation/apis/federation/v1beta1:go_default_library",
|
||||
"//federation/client/clientset_generated/federation_clientset/fake:go_default_library",
|
||||
"//pkg/api:go_default_library",
|
||||
|
|
|
@ -365,6 +365,7 @@ func AddHandlers(h printers.PrintHandler) {
|
|||
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
|
||||
{Name: "Status", Type: "string", Description: "Status of the cluster"},
|
||||
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
|
||||
{Name: "Labels", Type: "string", Description: "The labels of the cluster"},
|
||||
}
|
||||
h.TableHandler(clusterColumnDefinitions, printCluster)
|
||||
h.TableHandler(clusterColumnDefinitions, printClusterList)
|
||||
|
@ -764,6 +765,9 @@ func printCluster(obj *federation.Cluster, options printers.PrintOptions) ([]met
|
|||
statuses = append(statuses, "Unknown")
|
||||
}
|
||||
row.Cells = append(row.Cells, obj.Name, strings.Join(statuses, ","), translateTimestamp(obj.CreationTimestamp))
|
||||
if options.ShowLabels {
|
||||
row.Cells = append(row.Cells, labels.FormatLabels(obj.Labels))
|
||||
}
|
||||
return []metav1alpha1.TableRow{row}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ import (
|
|||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/kubernetes/federation/apis/federation"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/apis/apps"
|
||||
|
@ -1573,6 +1574,7 @@ func TestPrintPodTable(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintPod(t *testing.T) {
|
||||
tests := []struct {
|
||||
pod api.Pod
|
||||
|
@ -3131,3 +3133,124 @@ func TestPrintStorageClass(t *testing.T) {
|
|||
buf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintCluster(t *testing.T) {
|
||||
tests := []struct {
|
||||
cluster federation.Cluster
|
||||
expect []metav1alpha1.TableRow
|
||||
showLabels bool
|
||||
}{
|
||||
{
|
||||
federation.Cluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "cluster1",
|
||||
CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
|
||||
},
|
||||
},
|
||||
[]metav1alpha1.TableRow{{Cells: []interface{}{"cluster1", "Unknown", "0s"}}},
|
||||
false,
|
||||
},
|
||||
{
|
||||
federation.Cluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "cluster2",
|
||||
CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
|
||||
Labels: map[string]string{"label1": "", "label2": "cluster"},
|
||||
},
|
||||
Status: federation.ClusterStatus{
|
||||
Conditions: []federation.ClusterCondition{
|
||||
{
|
||||
Status: api.ConditionTrue,
|
||||
Type: federation.ClusterReady,
|
||||
},
|
||||
{
|
||||
Status: api.ConditionFalse,
|
||||
Type: federation.ClusterOffline,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
[]metav1alpha1.TableRow{{Cells: []interface{}{"cluster2", "Ready,NotOffline", "0s", "label1=,label2=cluster"}}},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
rows, err := printCluster(&test.cluster, printers.PrintOptions{ShowLabels: test.showLabels})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := range rows {
|
||||
rows[i].Object.Object = nil
|
||||
}
|
||||
if !reflect.DeepEqual(test.expect, rows) {
|
||||
t.Errorf("%d mismatch: %s", i, diff.ObjectReflectDiff(test.expect, rows))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintClusterList(t *testing.T) {
|
||||
tests := []struct {
|
||||
clusters federation.ClusterList
|
||||
expect []metav1alpha1.TableRow
|
||||
showLabels bool
|
||||
}{
|
||||
// Test podList's pod: name, num of containers, restarts, container ready status
|
||||
{
|
||||
federation.ClusterList{
|
||||
Items: []federation.Cluster{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "cluster1",
|
||||
CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
|
||||
},
|
||||
Status: federation.ClusterStatus{
|
||||
Conditions: []federation.ClusterCondition{
|
||||
{
|
||||
Status: api.ConditionTrue,
|
||||
Type: federation.ClusterReady,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "cluster2",
|
||||
CreationTimestamp: metav1.Time{Time: time.Now().Add(1.9e9)},
|
||||
Labels: map[string]string{"label1": "", "label2": "cluster2"},
|
||||
},
|
||||
Status: federation.ClusterStatus{
|
||||
Conditions: []federation.ClusterCondition{
|
||||
{
|
||||
Status: api.ConditionTrue,
|
||||
Type: federation.ClusterReady,
|
||||
},
|
||||
{
|
||||
Status: api.ConditionFalse,
|
||||
Type: federation.ClusterOffline,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
[]metav1alpha1.TableRow{{Cells: []interface{}{"cluster1", "Ready", "0s", "<none>"}},
|
||||
{Cells: []interface{}{"cluster2", "Ready,NotOffline", "0s", "label1=,label2=cluster2"}}},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
rows, err := printClusterList(&test.clusters, printers.PrintOptions{ShowLabels: test.showLabels})
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := range rows {
|
||||
rows[i].Object.Object = nil
|
||||
}
|
||||
if !reflect.DeepEqual(test.expect, rows) {
|
||||
t.Errorf("mismatch: %s", diff.ObjectReflectDiff(test.expect, rows))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue