mirror of https://github.com/knative/client.git
This commit is contained in:
parent
b8d571678f
commit
ac2170c1e2
|
|
@ -23,11 +23,18 @@ 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{
|
||||
{Name: "Name", Type: "string", Description: "Name of the revision.", Priority: 1},
|
||||
{Name: "Service", Type: "string", Description: "Name of the Knative service.", 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: "Generation", Type: "string", Description: "Generation of the revision", Priority: 1},
|
||||
{Name: "Age", Type: "string", Description: "Age of the revision.", Priority: 1},
|
||||
{Name: "Conditions", Type: "string", Description: "Conditions describing statuses of the revision.", Priority: 1},
|
||||
|
|
@ -57,6 +64,8 @@ func printRevisionList(revisionList *servingv1alpha1.RevisionList, options hprin
|
|||
func printRevision(revision *servingv1alpha1.Revision, options hprinters.PrintOptions) ([]metav1beta1.TableRow, error) {
|
||||
name := revision.Name
|
||||
service := revision.Labels[serving.ServiceLabelKey]
|
||||
traffic := revision.Annotations[RevisionTrafficAnnotation]
|
||||
tags := revision.Annotations[RevisionTagsAnnotation]
|
||||
generation := revision.Labels[serving.ConfigurationGenerationLabelKey]
|
||||
age := commands.TranslateTimestampSince(revision.CreationTimestamp)
|
||||
conditions := commands.ConditionsValue(revision.Status.Conditions)
|
||||
|
|
@ -68,6 +77,8 @@ func printRevision(revision *servingv1alpha1.Revision, options hprinters.PrintOp
|
|||
row.Cells = append(row.Cells,
|
||||
name,
|
||||
service,
|
||||
traffic,
|
||||
tags,
|
||||
generation,
|
||||
age,
|
||||
conditions,
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import (
|
|||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"knative.dev/serving/pkg/apis/serving"
|
||||
|
||||
|
|
@ -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]
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -70,6 +70,7 @@ func NewRevisionListFlags() *RevisionListFlags {
|
|||
return &RevisionListFlags{
|
||||
GenericPrintFlags: genericclioptions.NewPrintFlags(""),
|
||||
HumanReadableFlags: commands.NewHumanPrintFlags(),
|
||||
ServiceRefFlags: &ServiceReferenceFlags{},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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", "TRAFFIC", "TAGS", "GENERATION", "AGE", "CONDITIONS", "READY", "REASON"}
|
||||
|
||||
func fakeRevisionList(args []string, response *v1alpha1.RevisionList) (action client_testing.Action, output []string, err error) {
|
||||
knParams := &commands.KnParams{}
|
||||
|
|
@ -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}}
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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",
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue