Skip to content

Commit

Permalink
Refine route list output
Browse files Browse the repository at this point in the history
- Only show NAME/URL/READY

[issue 350](#350)
  • Loading branch information
Gong Zhang committed Oct 22, 2019
1 parent df04573 commit b8f5e69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
26 changes: 3 additions & 23 deletions pkg/kn/commands/route/human_readable_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package route

import (
"fmt"

metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"knative.dev/client/pkg/kn/commands"
Expand All @@ -29,9 +27,7 @@ func RouteListHandlers(h hprinters.PrintHandler) {
kRouteColumnDefinitions := []metav1beta1.TableColumnDefinition{
{Name: "Name", Type: "string", Description: "Name of the Knative route.", Priority: 1},
{Name: "URL", Type: "string", Description: "URL of the Knative route.", Priority: 1},
{Name: "Age", Type: "string", Description: "Age of the Knative route.", Priority: 1},
{Name: "Conditions", Type: "string", Description: "Conditions describing statuses of route components.", Priority: 1},
{Name: "Traffic", Type: "integer", Description: "Traffic configured for route.", Priority: 1},
{Name: "READY", Type: "string", Description: "Ready condition status of the Knative route.", Priority: 1},
}
h.TableHandler(kRouteColumnDefinitions, printRoute)
h.TableHandler(kRouteColumnDefinitions, printKRouteList)
Expand All @@ -54,29 +50,13 @@ func printKRouteList(kRouteList *servingv1alpha1.RouteList, options hprinters.Pr
func printRoute(route *servingv1alpha1.Route, options hprinters.PrintOptions) ([]metav1beta1.TableRow, error) {
name := route.Name
url := route.Status.URL
age := commands.TranslateTimestampSince(route.CreationTimestamp)
conditions := commands.ConditionsValue(route.Status.Conditions)
traffic := calculateTraffic(route.Status.Traffic)
ready := commands.ReadyCondition(route.Status.Conditions)
row := metav1beta1.TableRow{
Object: runtime.RawExtension{Object: route},
}
row.Cells = append(row.Cells,
name,
url,
age,
conditions,
traffic)
ready)
return []metav1beta1.TableRow{row}, nil
}

func calculateTraffic(targets []servingv1alpha1.TrafficTarget) string {
var traffic string
for _, target := range targets {
if len(traffic) > 0 {
traffic = fmt.Sprintf("%s, %d%% -> %s", traffic, target.Percent, target.RevisionName)
} else {
traffic = fmt.Sprintf("%d%% -> %s", target.Percent, target.RevisionName)
}
}
return traffic
}
16 changes: 8 additions & 8 deletions pkg/kn/commands/route/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func TestRouteListDefaultOutput(t *testing.T) {
} else if !action.Matches("list", "routes") {
t.Errorf("Bad action %v", action)
}
assert.Check(t, util.ContainsAll(output[0], "NAME", "URL", "AGE", "CONDITIONS", "TRAFFIC"))
assert.Check(t, util.ContainsAll(output[1], "foo", "100% -> foo-01234"))
assert.Check(t, util.ContainsAll(output[2], "bar", "100% -> bar-98765"))
assert.Check(t, util.ContainsAll(output[0], "NAME", "URL", "READY"))
assert.Check(t, util.ContainsAll(output[1], "foo"))
assert.Check(t, util.ContainsAll(output[2], "bar"))
}

func TestRouteListDefaultOutputNoHeaders(t *testing.T) {
Expand All @@ -84,9 +84,9 @@ func TestRouteListDefaultOutputNoHeaders(t *testing.T) {
t.Errorf("Bad action %v", action)
}

assert.Check(t, util.ContainsNone(output[0], "NAME", "URL", "GENERATION", "AGE", "CONDITIONS", "READY", "REASON"))
assert.Check(t, util.ContainsAll(output[0], "foo", "100% -> foo-01234"))
assert.Check(t, util.ContainsAll(output[1], "bar", "100% -> bar-98765"))
assert.Check(t, util.ContainsNone(output[0], "NAME", "URL", "READY"))
assert.Check(t, util.ContainsAll(output[0], "foo"))
assert.Check(t, util.ContainsAll(output[1], "bar"))

}

Expand All @@ -100,8 +100,8 @@ func TestRouteListWithTwoTargetsOutput(t *testing.T) {
} else if !action.Matches("list", "routes") {
t.Errorf("Bad action %v", action)
}
assert.Check(t, util.ContainsAll(output[0], "NAME", "URL", "AGE", "CONDITIONS", "TRAFFIC"))
assert.Check(t, util.ContainsAll(output[1], "foo", "20% -> foo-01234, 80% -> foo-98765"))
assert.Check(t, util.ContainsAll(output[0], "NAME", "URL", "READY"))
assert.Check(t, util.ContainsAll(output[1], "foo"))
}

func createMockRouteMeta(name string) *v1alpha1.Route {
Expand Down

0 comments on commit b8f5e69

Please sign in to comment.