update cluster printer to enable --show-labels

pull/6/head
Di Xu 2017-10-12 13:29:11 +08:00
parent cea1af38e2
commit 5a9313e10b
3 changed files with 128 additions and 0 deletions

View File

@ -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",

View File

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

View File

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