Skip to content

Commit

Permalink
feature(#580): adds traffic and tags information to revision list
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilien committed Dec 20, 2019
1 parent b640219 commit df045ee
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
11 changes: 11 additions & 0 deletions pkg/kn/commands/revision/human_readable_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
servingv1alpha1 "knative.dev/serving/pkg/apis/serving/v1alpha1"
)

const (
RevisionTrafficAnnotation = "client.knative.dev/traffic"
RevisionTagsAnnotation = "client.knative.dev/tags"
)

// RevisionListHandlers adds print handlers for revision list command
func RevisionListHandlers(h hprinters.PrintHandler) {
RevisionColumnDefinitions := []metav1beta1.TableColumnDefinition{
Expand All @@ -32,6 +37,8 @@ func RevisionListHandlers(h hprinters.PrintHandler) {
{Name: "Age", Type: "string", Description: "Age of the revision.", Priority: 1},
{Name: "Conditions", Type: "string", Description: "Conditions describing statuses of the revision.", Priority: 1},
{Name: "Ready", Type: "string", Description: "Ready condition status of the revision.", Priority: 1},
{Name: "Traffic", Type: "string", Description: "Percentage of traffic assigned to this revision.", Priority: 1},
{Name: "Tags", Type: "string", Description: "Set of tags assigned to this revision.", Priority: 1},
{Name: "Reason", Type: "string", Description: "Reason for non-ready condition of the revision.", Priority: 1},
}
h.TableHandler(RevisionColumnDefinitions, printRevision)
Expand Down Expand Up @@ -61,6 +68,8 @@ func printRevision(revision *servingv1alpha1.Revision, options hprinters.PrintOp
age := commands.TranslateTimestampSince(revision.CreationTimestamp)
conditions := commands.ConditionsValue(revision.Status.Conditions)
ready := commands.ReadyCondition(revision.Status.Conditions)
traffic := revision.Annotations[RevisionTrafficAnnotation]
tags := revision.Annotations[RevisionTagsAnnotation]
reason := commands.NonReadyConditionReason(revision.Status.Conditions)
row := metav1beta1.TableRow{
Object: runtime.RawExtension{Object: revision},
Expand All @@ -72,6 +81,8 @@ func printRevision(revision *servingv1alpha1.Revision, options hprinters.PrintOp
age,
conditions,
ready,
traffic,
tags,
reason)
return []metav1beta1.TableRow{row}, nil
}
19 changes: 19 additions & 0 deletions pkg/kn/commands/revision/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"sort"
"strconv"
"strings"

"knative.dev/serving/pkg/apis/serving"

Expand Down Expand Up @@ -85,6 +86,23 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
}
}

// Add revision/service info as annotations: traffic and tags
var service *v1alpha1.Service
var serviceName string
for _, revision := range revisionList.Items {
if serviceName == "" || revision.Labels[serving.ServiceLabelKey] != serviceName {
serviceName = revision.Labels[serving.ServiceLabelKey]
service, err = client.GetService(serviceName)
if err != nil {
return err
}
}

traffic, tags := trafficForRevision(revision.Name, service)
revision.Annotations[RevisionTrafficAnnotation] = fmt.Sprintf("%d%%", traffic)
revision.Annotations[RevisionTagsAnnotation] = strings.Join(tags, ",")
}

// sort revisionList by configuration generation key
sort.SliceStable(revisionList.Items, func(i, j int) bool {
a := revisionList.Items[i]
Expand All @@ -110,6 +128,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}

err = printer.PrintObj(revisionList, cmd.OutOrStdout())
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/kn/commands/revision/list_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
type RevisionListFlags struct {
GenericPrintFlags *genericclioptions.PrintFlags
HumanReadableFlags *commands.HumanPrintFlags
ServiceRefFlags ServiceReferenceFlags
ServiceRefFlags *ServiceReferenceFlags
}

// AllowedFormats is the list of formats in which data can be displayed
Expand Down Expand Up @@ -70,6 +70,7 @@ func NewRevisionListFlags() *RevisionListFlags {
return &RevisionListFlags{
GenericPrintFlags: genericclioptions.NewPrintFlags(""),
HumanReadableFlags: commands.NewHumanPrintFlags(),
ServiceRefFlags: &ServiceReferenceFlags{},
}
}

Expand Down
34 changes: 19 additions & 15 deletions pkg/kn/commands/revision/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"knative.dev/client/pkg/util"
)

var revisionListHeader = []string{"NAME", "SERVICE", "GENERATION", "AGE", "CONDITIONS", "READY", "REASON"}
var revisionListHeader = []string{"NAME", "SERVICE", "GENERATION", "AGE", "CONDITIONS", "READY", "TRAFFIC", "TAGS", "REASON"}

func fakeRevisionList(args []string, response *v1alpha1.RevisionList) (action client_testing.Action, output []string, err error) {
knParams := &commands.KnParams{}
Expand Down Expand Up @@ -71,13 +71,13 @@ func TestRevisionListEmptyByName(t *testing.T) {
}

func TestRevisionListDefaultOutput(t *testing.T) {
revision1 := createMockRevisionWithParams("foo-abcd", "foo", "1")
revision2 := createMockRevisionWithParams("bar-abcd", "bar", "1")
revision3 := createMockRevisionWithParams("foo-wxyz", "foo", "2")
revision4 := createMockRevisionWithParams("bar-wxyz", "bar", "2")
revision1 := createMockRevisionWithParams("foo-abcd", "foo", "1", "100", "")
revision2 := createMockRevisionWithParams("bar-abcd", "bar", "1", "100", "")
revision3 := createMockRevisionWithParams("foo-wxyz", "foo", "2", "50", "tag10")
revision4 := createMockRevisionWithParams("bar-wxyz", "bar", "2", "0", "tag2")
// Validate edge case for catching the sorting issue caused by string comparison
revision5 := createMockRevisionWithParams("foo-wxyz", "foo", "10")
revision6 := createMockRevisionWithParams("bar-wxyz", "bar", "10")
revision5 := createMockRevisionWithParams("foo-wxyz", "foo", "10", "tag1", "tagx")
revision6 := createMockRevisionWithParams("bar-wxyz", "bar", "10", "50", "")

RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{
*revision1, *revision2, *revision3, *revision4, *revision5, *revision6}}
Expand All @@ -98,8 +98,8 @@ func TestRevisionListDefaultOutput(t *testing.T) {
}

func TestRevisionListDefaultOutputNoHeaders(t *testing.T) {
revision1 := createMockRevisionWithParams("foo-abcd", "foo", "2")
revision2 := createMockRevisionWithParams("bar-wxyz", "bar", "1")
revision1 := createMockRevisionWithParams("foo-abcd", "foo", "2", "100", "")
revision2 := createMockRevisionWithParams("bar-wxyz", "bar", "1", "100", "")
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision1, *revision2}}
action, output, err := fakeRevisionList([]string{"revision", "list", "--no-headers"}, RevisionList)
assert.NilError(t, err)
Expand All @@ -116,10 +116,10 @@ func TestRevisionListDefaultOutputNoHeaders(t *testing.T) {
}

func TestRevisionListForService(t *testing.T) {
revision1 := createMockRevisionWithParams("foo-abcd", "svc1", "1")
revision2 := createMockRevisionWithParams("bar-wxyz", "svc1", "2")
revision3 := createMockRevisionWithParams("foo-abcd", "svc2", "1")
revision4 := createMockRevisionWithParams("bar-wxyz", "svc2", "2")
revision1 := createMockRevisionWithParams("foo-abcd", "svc1", "1", "50", "")
revision2 := createMockRevisionWithParams("bar-wxyz", "svc1", "2", "50", "")
revision3 := createMockRevisionWithParams("foo-abcd", "svc2", "1", "0", "")
revision4 := createMockRevisionWithParams("bar-wxyz", "svc2", "2", "100", "")
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision1, *revision2, *revision3, *revision4}}
action, output, err := fakeRevisionList([]string{"revision", "list", "-s", "svc1"}, RevisionList)
assert.NilError(t, err)
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestRevisionListForService(t *testing.T) {
}

func TestRevisionListOneOutput(t *testing.T) {
revision := createMockRevisionWithParams("foo-abcd", "foo", "1")
revision := createMockRevisionWithParams("foo-abcd", "foo", "1", "100", "")
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision}}
action, output, err := fakeRevisionList([]string{"revision", "list", "foo-abcd"}, RevisionList)
assert.NilError(t, err)
Expand All @@ -176,7 +176,7 @@ func TestRevisionListOutputWithTwoRevName(t *testing.T) {
assert.ErrorContains(t, err, "'kn revision list' accepts maximum 1 argument")
}

func createMockRevisionWithParams(name, svcName, generation string) *v1alpha1.Revision {
func createMockRevisionWithParams(name, svcName, generation, traffic, tags string) *v1alpha1.Revision {
revision := &v1alpha1.Revision{
TypeMeta: metav1.TypeMeta{
Kind: "Revision",
Expand All @@ -189,6 +189,10 @@ func createMockRevisionWithParams(name, svcName, generation string) *v1alpha1.Re
serving.ServiceLabelKey: svcName,
serving.ConfigurationGenerationLabelKey: generation,
},
Annotations: map[string]string{
"client.knative.dev/traffic": traffic,
"client.knative.dev/tags": tags,
},
},
}
return revision
Expand Down

0 comments on commit df045ee

Please sign in to comment.