Adds revision get command (#97)

* Renames test names for service get command

 also removes unnecessary commented imports

* Adds revision get command

 renames `revision list` ==> `revision get`

* Adds tests for revision get command

* Updates the vendors/modules.txt

* Removes revision list command
This commit is contained in:
Navid Shaikh 2019-05-14 01:51:39 +05:30 committed by Knative Prow Robot
parent bd7d57fa09
commit 39034a73f9
7 changed files with 256 additions and 33 deletions

View File

@ -36,8 +36,10 @@ func (f *HumanPrintFlags) AllowedFormats() []string {
// handling human-readable output. // handling human-readable output.
func (f *HumanPrintFlags) ToPrinter() (hprinters.ResourcePrinter, error) { func (f *HumanPrintFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
p := hprinters.NewTablePrinter(hprinters.PrintOptions{}) p := hprinters.NewTablePrinter(hprinters.PrintOptions{})
// Add the column definitions and respective functions // Add the column definitions and respective printing functions for service get command
ServiceGetHandlers(p) ServiceGetHandlers(p)
// Add the column definitions and respective printing functions for revision get command
RevisionGetHandlers(p)
return p, nil return p, nil
} }

View File

@ -23,7 +23,7 @@ func NewRevisionCommand(p *KnParams) *cobra.Command {
Use: "revision", Use: "revision",
Short: "Revision command group", Short: "Revision command group",
} }
revisionCmd.AddCommand(NewRevisionListCommand(p)) revisionCmd.AddCommand(NewRevisionGetCommand(p))
revisionCmd.AddCommand(NewRevisionDescribeCommand(p)) revisionCmd.AddCommand(NewRevisionDescribeCommand(p))
return revisionCmd return revisionCmd
} }

View File

@ -1,4 +1,4 @@
// Copyright © 2018 The Knative Authors // Copyright © 2019 The Knative Authors
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -15,21 +15,20 @@
package commands package commands
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
) )
var revisionListPrintFlags *genericclioptions.PrintFlags // NewRevisionGetCommand represents 'kn revision get' command
func NewRevisionGetCommand(p *KnParams) *cobra.Command {
revisionGetFlags := NewRevisionGetFlags()
// listCmd represents the list command revisionGetCommand := &cobra.Command{
func NewRevisionListCommand(p *KnParams) *cobra.Command { Use: "get",
revisionListPrintFlags = genericclioptions.NewPrintFlags("").WithDefaultOutput( Short: "Get available revisions.",
"jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}")
revisionListCmd := &cobra.Command{
Use: "list",
Short: "List available revisions.",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
client, err := p.ServingFactory() client, err := p.ServingFactory()
if err != nil { if err != nil {
@ -43,15 +42,18 @@ func NewRevisionListCommand(p *KnParams) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
if len(revision.Items) == 0 {
printer, err := revisionListPrintFlags.ToPrinter() fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
if err != nil { return nil
return err
} }
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev", Group: "knative.dev",
Version: "v1alpha1", Version: "v1alpha1",
Kind: "Revision"}) Kind: "revision"})
printer, err := revisionGetFlags.ToPrinter()
if err != nil {
return err
}
err = printer.PrintObj(revision, cmd.OutOrStdout()) err = printer.PrintObj(revision, cmd.OutOrStdout())
if err != nil { if err != nil {
return err return err
@ -59,7 +61,7 @@ func NewRevisionListCommand(p *KnParams) *cobra.Command {
return nil return nil
}, },
} }
AddNamespaceFlags(revisionListCmd.Flags(), true) AddNamespaceFlags(revisionGetCommand.Flags(), true)
revisionListPrintFlags.AddFlags(revisionListCmd) revisionGetFlags.AddFlags(revisionGetCommand)
return revisionListCmd return revisionGetCommand
} }

View File

@ -0,0 +1,122 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
hprinters "github.com/knative/client/pkg/printers"
serving "github.com/knative/serving/pkg/apis/serving"
servingv1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/spf13/cobra"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
// RevisionGetFlags composes common printer flag structs
// used in the Get command.
type RevisionGetFlags struct {
GenericPrintFlags *genericclioptions.PrintFlags
HumanReadableFlags *HumanPrintFlags
}
// AllowedFormats is the list of formats in which data can be displayed
func (f *RevisionGetFlags) AllowedFormats() []string {
formats := f.GenericPrintFlags.AllowedFormats()
formats = append(formats, f.HumanReadableFlags.AllowedFormats()...)
return formats
}
// ToPrinter attempts to find a composed set of RevisionGetFlags suitable for
// returning a printer based on current flag values.
func (f *RevisionGetFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
// if there are flags specified for generic printing
if f.GenericPrintFlags.OutputFlagSpecified() {
p, err := f.GenericPrintFlags.ToPrinter()
if err != nil {
return nil, err
}
return p, nil
}
// if no flags specified, use the table printing
p, err := f.HumanReadableFlags.ToPrinter()
if err != nil {
return nil, err
}
return p, nil
}
// AddFlags receives a *cobra.Command reference and binds
// flags related to humanreadable and template printing.
func (f *RevisionGetFlags) AddFlags(cmd *cobra.Command) {
f.GenericPrintFlags.AddFlags(cmd)
f.HumanReadableFlags.AddFlags(cmd)
}
// NewGetPrintFlags returns flags associated with humanreadable,
// template, and "name" printing, with default values set.
func NewRevisionGetFlags() *RevisionGetFlags {
return &RevisionGetFlags{
GenericPrintFlags: genericclioptions.NewPrintFlags(""),
HumanReadableFlags: NewHumanPrintFlags(),
}
}
// RevisionGetHandlers adds print handlers for revision get command
func RevisionGetHandlers(h hprinters.PrintHandler) {
RevisionColumnDefinitions := []metav1beta1.TableColumnDefinition{
{Name: "Service", Type: "string", Description: "Name of the knative service."},
{Name: "Name", Type: "string", Description: "Name of the revision."},
{Name: "Age", Type: "string", Description: "Age of the revision."},
{Name: "Conditions", Type: "string", Description: "Conditions describing statuses of revision."},
{Name: "Ready", Type: "string", Description: "Ready condition status of the revision."},
{Name: "Reason", Type: "string", Description: "Reason for non-ready condition of the revision."},
}
h.TableHandler(RevisionColumnDefinitions, printRevision)
h.TableHandler(RevisionColumnDefinitions, printRevisionList)
}
// printRevisionList populates the knative revision list table rows
func printRevisionList(revisionList *servingv1alpha1.RevisionList, options hprinters.PrintOptions) ([]metav1beta1.TableRow, error) {
rows := make([]metav1beta1.TableRow, 0, len(revisionList.Items))
for _, rev := range revisionList.Items {
r, err := printRevision(&rev, options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}
// printRevision populates the knative revision table rows
func printRevision(revision *servingv1alpha1.Revision, options hprinters.PrintOptions) ([]metav1beta1.TableRow, error) {
service := revision.Labels[serving.ConfigurationLabelKey]
name := revision.Name
age := translateTimestampSince(revision.CreationTimestamp)
conditions := conditionsValue(revision.Status.Conditions)
ready := readyCondition(revision.Status.Conditions)
reason := nonReadyConditionReason(revision.Status.Conditions)
row := metav1beta1.TableRow{
Object: runtime.RawExtension{Object: revision},
}
row.Cells = append(row.Cells,
service,
name,
age,
conditions,
ready,
reason)
return []metav1beta1.TableRow{row}, nil
}

View File

@ -0,0 +1,98 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"bytes"
"strings"
"testing"
serving "github.com/knative/serving/pkg/apis/serving"
v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
servingclient "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing"
)
func fakeRevisionGet(args []string, response *v1alpha1.RevisionList) (action client_testing.Action, output []string, err error) {
buf := new(bytes.Buffer)
fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}}
cmd := NewKnCommand(KnParams{
Output: buf,
ServingFactory: func() (servingclient.ServingV1alpha1Interface, error) { return fakeServing, nil },
})
fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) {
action = a
return true, response, nil
})
cmd.SetArgs(args)
err = cmd.Execute()
if err != nil {
return
}
output = strings.Split(buf.String(), "\n")
return
}
func TestRevisionGetEmpty(t *testing.T) {
action, output, err := fakeRevisionGet([]string{"revision", "get"}, &v1alpha1.RevisionList{})
if err != nil {
t.Error(err)
return
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "revisions") {
t.Errorf("Bad action %v", action)
} else if output[0] != "No resources found." {
t.Errorf("Bad output %s", output[0])
}
}
func TestRevisionGetDefaultOutput(t *testing.T) {
revision1 := createMockRevisionWithParams("foo-abcd", "foo")
revision2 := createMockRevisionWithParams("bar-wxyz", "bar")
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision1, *revision2}}
action, output, err := fakeRevisionGet([]string{"revision", "get"}, RevisionList)
if err != nil {
t.Fatal(err)
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("list", "revisions") {
t.Errorf("Bad action %v", action)
}
testContains(t, output[0], []string{"SERVICE", "NAME", "AGE", "CONDITIONS", "READY", "REASON"}, "column header")
testContains(t, output[1], []string{"foo", "foo-abcd"}, "value")
testContains(t, output[2], []string{"bar", "bar-wxyz"}, "value")
}
func createMockRevisionWithParams(name, svcName string) *v1alpha1.Revision {
revision := &v1alpha1.Revision{
TypeMeta: metav1.TypeMeta{
Kind: "Revision",
APIVersion: "knative.dev/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
Labels: map[string]string{serving.ConfigurationLabelKey: svcName},
},
}
return revision
}

View File

@ -19,22 +19,21 @@ import (
"strings" "strings"
"testing" "testing"
//servinglib "github.com/knative/client/pkg/serving"
duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1"
"github.com/knative/serving/pkg/apis/serving/v1alpha1" v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1" servingclient "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake" "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing" client_testing "k8s.io/client-go/testing"
) )
func fakeGet(args []string, response *v1alpha1.ServiceList) (action client_testing.Action, output []string, err error) { func fakeServiceGet(args []string, response *v1alpha1.ServiceList) (action client_testing.Action, output []string, err error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}} fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}}
cmd := NewKnCommand(KnParams{ cmd := NewKnCommand(KnParams{
Output: buf, Output: buf,
ServingFactory: func() (serving.ServingV1alpha1Interface, error) { return fakeServing, nil }, ServingFactory: func() (servingclient.ServingV1alpha1Interface, error) { return fakeServing, nil },
}) })
fakeServing.AddReactor("*", "*", fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) { func(a client_testing.Action) (bool, runtime.Object, error) {
@ -51,7 +50,7 @@ func fakeGet(args []string, response *v1alpha1.ServiceList) (action client_testi
} }
func TestGetEmpty(t *testing.T) { func TestGetEmpty(t *testing.T) {
action, output, err := fakeGet([]string{"service", "get"}, &v1alpha1.ServiceList{}) action, output, err := fakeServiceGet([]string{"service", "get"}, &v1alpha1.ServiceList{})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -65,11 +64,11 @@ func TestGetEmpty(t *testing.T) {
} }
} }
func TestListDefaultOutput(t *testing.T) { func TestServiceGetDefaultOutput(t *testing.T) {
service1 := createMockServiceWithParams(t, "foo", "foo.default.example.com", 1) service1 := createMockServiceWithParams("foo", "foo.default.example.com", 1)
service2 := createMockServiceWithParams(t, "bar", "bar.default.example.com", 2) service2 := createMockServiceWithParams("bar", "bar.default.example.com", 2)
serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service1, *service2}} serviceList := &v1alpha1.ServiceList{Items: []v1alpha1.Service{*service1, *service2}}
action, output, err := fakeGet([]string{"service", "get"}, serviceList) action, output, err := fakeServiceGet([]string{"service", "get"}, serviceList)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -91,7 +90,7 @@ func testContains(t *testing.T, output string, sub []string, element string) {
} }
} }
func createMockServiceWithParams(t *testing.T, name, domain string, generation int64) *v1alpha1.Service { func createMockServiceWithParams(name, domain string, generation int64) *v1alpha1.Service {
service := &v1alpha1.Service{ service := &v1alpha1.Service{
TypeMeta: metav1.TypeMeta{ TypeMeta: metav1.TypeMeta{
Kind: "Service", Kind: "Service",

2
vendor/modules.txt vendored
View File

@ -70,13 +70,13 @@ github.com/knative/pkg/kmeta
github.com/knative/pkg/kmp github.com/knative/pkg/kmp
github.com/knative/pkg/configmap github.com/knative/pkg/configmap
# github.com/knative/serving v0.5.2 # github.com/knative/serving v0.5.2
github.com/knative/serving/pkg/apis/serving
github.com/knative/serving/pkg/apis/serving/v1alpha1 github.com/knative/serving/pkg/apis/serving/v1alpha1
github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1 github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1
github.com/knative/serving/pkg/apis/autoscaling github.com/knative/serving/pkg/apis/autoscaling
github.com/knative/serving/pkg/apis/config github.com/knative/serving/pkg/apis/config
github.com/knative/serving/pkg/apis/networking github.com/knative/serving/pkg/apis/networking
github.com/knative/serving/pkg/apis/networking/v1alpha1 github.com/knative/serving/pkg/apis/networking/v1alpha1
github.com/knative/serving/pkg/apis/serving
github.com/knative/serving/pkg/client/clientset/versioned/scheme github.com/knative/serving/pkg/client/clientset/versioned/scheme
github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake
github.com/knative/serving/pkg/apis/autoscaling/v1alpha1 github.com/knative/serving/pkg/apis/autoscaling/v1alpha1