Skip to content

Commit

Permalink
get service and revision based on name parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture committed Jun 10, 2019
1 parent 1b471d5 commit dcf4cfa
Show file tree
Hide file tree
Showing 21 changed files with 4,212 additions and 63 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.3.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/net v0.0.0-20190514140710-3ec191127204 // indirect
golang.org/x/oauth2 v0.0.0-20190517181255-950ef44c6e07 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoH
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand Down
4 changes: 2 additions & 2 deletions hack/generate-docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func main() {
if len(os.Args) > 1 {
dir = os.Args[1]
}
err := doc.GenMarkdownTree(rootCmd, dir + "/docs/cmd/")
err := doc.GenMarkdownTree(rootCmd, dir+"/docs/cmd/")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
69 changes: 57 additions & 12 deletions pkg/kn/commands/revision/revision_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"fmt"

"github.com/knative/client/pkg/kn/commands"

serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -28,8 +31,20 @@ func NewRevisionGetCommand(p *commands.KnParams) *cobra.Command {
revisionGetFlags := NewRevisionGetFlags()

revisionGetCommand := &cobra.Command{
Use: "get",
Use: "get NAME",
Short: "Get available revisions.",
Example: `
# List a all revisions.
kn revision get
# List a all revisions in JSON output format.
kn revision get -o json
# List a single revisions with specified NAME.
kn revision get web
# List a single revisions in YAML output format.
kn revision get web -o yaml`,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := p.ServingFactory()
if err != nil {
Expand All @@ -39,23 +54,18 @@ func NewRevisionGetCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
revision, err := client.Revisions(namespace).List(v1.ListOptions{})
if err != nil {

revResp, err := getRevisionInfo(args, namespace, client, cmd)
value, ok := revResp.(runtime.Object)
if err != nil || !ok {
return err
}
if len(revision.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
return nil
}
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "revision"})

printer, err := revisionGetFlags.ToPrinter()
if err != nil {
return err
}
err = printer.PrintObj(revision, cmd.OutOrStdout())
err = printer.PrintObj(value, cmd.OutOrStdout())
if err != nil {
return err
}
Expand All @@ -66,3 +76,38 @@ func NewRevisionGetCommand(p *commands.KnParams) *cobra.Command {
revisionGetFlags.AddFlags(revisionGetCommand)
return revisionGetCommand
}

func getRevisionInfo(args []string, namespace string, client serving.ServingV1alpha1Interface, cmd *cobra.Command) (interface{}, error) {

switch len(args) {
case 0:
revision, err := client.Revisions(namespace).List(v1.ListOptions{})
if err != nil {
return nil, err
}
if len(revision.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
return nil, nil
}
revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "revision"})
return revision, nil

case 1:
revision, err := client.Revisions(namespace).Get(args[0], v1.GetOptions{})
if err != nil {
return nil, err
}

revision.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "serving.knative.dev",
Version: "v1alpha1",
Kind: "revision"})

return revision, nil
default:
return nil, fmt.Errorf("'revision get' only accepts no arguments or a single revision name, not %d arguments as given", len(args))
}
}
112 changes: 93 additions & 19 deletions pkg/kn/commands/revision/revision_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,31 @@ import (
"testing"

"github.com/knative/client/pkg/kn/commands"
serving "github.com/knative/serving/pkg/apis/serving"
v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"

"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/stretchr/testify/assert"
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) {
func fakeGet(args []string, response interface{}) (action client_testing.Action, output []string, err error) {
var (
resp runtime.Object
ok bool
)

if resp, ok = response.(runtime.Object); !ok {
return
}

knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewRevisionCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) {
action = a
return true, response, nil
return true, resp, nil
})
cmd.SetArgs(args)
err = cmd.Execute()
Expand All @@ -43,8 +54,8 @@ func fakeRevisionGet(args []string, response *v1alpha1.RevisionList) (action cli
return
}

func TestRevisionGetEmpty(t *testing.T) {
action, output, err := fakeRevisionGet([]string{"revision", "get"}, &v1alpha1.RevisionList{})
func TestRevisionListEmpty(t *testing.T) {
action, output, err := fakeGet([]string{"revision", "get"}, &v1alpha1.RevisionList{})
if err != nil {
t.Error(err)
return
Expand All @@ -58,11 +69,24 @@ func TestRevisionGetEmpty(t *testing.T) {
}
}

func TestRevisionGetEmpty(t *testing.T) {
action, _, err := fakeGet([]string{"revision", "get", "name"}, &v1alpha1.Revision{})
if err != nil {
t.Error(err)
return
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("get", "revisions") {
t.Errorf("Bad action %v", action)
}
}

func TestRevisionGetDefaultOutput(t *testing.T) {
revision1 := createMockRevisionWithParams("foo-abcd", "foo")
revision2 := createMockRevisionWithParams("bar-wxyz", "bar")
revision1 := createMockRevisionWithParams("foo-abcd", "foo", false)
revision2 := createMockRevisionWithParams("bar-wxyz", "bar", false)
RevisionList := &v1alpha1.RevisionList{Items: []v1alpha1.Revision{*revision1, *revision2}}
action, output, err := fakeRevisionGet([]string{"revision", "get"}, RevisionList)
action, output, err := fakeGet([]string{"revision", "get"}, RevisionList)
if err != nil {
t.Fatal(err)
}
Expand All @@ -71,24 +95,74 @@ func TestRevisionGetDefaultOutput(t *testing.T) {
} 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")
commands.TestContains(t, output[0], []string{"SERVICE", "NAME", "AGE", "CONDITIONS", "READY", "REASON"}, "column header")
commands.TestContains(t, output[1], []string{"foo", "foo-abcd"}, "value")
commands.TestContains(t, output[2], []string{"bar", "bar-wxyz"}, "value")
}

func TestRevisionGetOneOutput(t *testing.T) {
revision := createMockRevisionWithParams("foo-abcd", "foo", true)
action, output, err := fakeGet([]string{"revision", "get", "foo-abcd"}, revision)
if err != nil {
t.Fatal(err)
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("get", "revisions") {
t.Errorf("Bad action %v", action)
}
commands.TestContains(t, output[0], []string{"SERVICE", "NAME", "AGE", "CONDITIONS", "READY", "REASON"}, "column header")
commands.TestContains(t, output[1], []string{"foo", "foo-abcd"}, "value")
}

func testContains(t *testing.T, output string, sub []string, element string) {
for _, each := range sub {
if !strings.Contains(output, each) {
t.Errorf("Missing %s: %s", element, each)
}
func TestRevisionGetOutputWithTwoRevName(t *testing.T) {
t.Log("create a first revision name foo")
revision := createMockRevisionWithParams("foo-abcd", "foo", true)
action, output, err := fakeGet([]string{"revision", "get", "foo-abcd"}, revision)
if err != nil {
t.Fatal(err)
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("get", "revisions") {
t.Errorf("Bad action %v", action)
}
commands.TestContains(t, output[0], []string{"SERVICE", "NAME", "AGE", "CONDITIONS", "READY", "REASON"}, "column header")
commands.TestContains(t, output[1], []string{"foo", "foo-abcd"}, "value")

t.Log("create a second revision name bar")
revision = createMockRevisionWithParams("bar-abcd", "bar", true)
action, output, err = fakeGet([]string{"revision", "get", "bar-abcd"}, revision)
if err != nil {
t.Fatal(err)
}
if action == nil {
t.Errorf("No action")
} else if !action.Matches("get", "revisions") {
t.Errorf("Bad action %v", action)
}
commands.TestContains(t, output[0], []string{"SERVICE", "NAME", "AGE", "CONDITIONS", "READY", "REASON"}, "column header")
commands.TestContains(t, output[1], []string{"bar", "bar-abcd"}, "value")

t.Log("verify by passing two revision name to get command")
_, output, err = fakeGet([]string{"revision", "get", "foo-abcd", "bar-abcd"}, revision)
assert.NotNil(t, err)
assert.Empty(t, output)

}

func createMockRevisionWithParams(name, svcName string) *v1alpha1.Revision {
func createMockRevisionWithParams(name, svcName string, version bool) *v1alpha1.Revision {
var apiVersion string
if version {
apiVersion = "serving.knative.dev/v1alpha1"
} else {
apiVersion = "knative.dev/v1alpha1"
}

revision := &v1alpha1.Revision{
TypeMeta: metav1.TypeMeta{
Kind: "Revision",
APIVersion: "knative.dev/v1alpha1",
APIVersion: apiVersion,
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
68 changes: 56 additions & 12 deletions pkg/kn/commands/service/service_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"fmt"

"github.com/knative/client/pkg/kn/commands"

serving "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -28,8 +31,20 @@ func NewServiceGetCommand(p *commands.KnParams) *cobra.Command {
serviceGetFlags := NewServiceGetFlags()

serviceGetCommand := &cobra.Command{
Use: "get",
Use: "get NAME",
Short: "Get available services.",
Example: `
# List a all services.
kn service get
# List a all services in JSON output format.
kn service get -o json
# List a single service with specified NAME.
kn service get web
# List a single service in YAML output format.
kn service get web -o yaml`,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := p.ServingFactory()
if err != nil {
Expand All @@ -39,25 +54,19 @@ func NewServiceGetCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
service, err := client.Services(namespace).List(v1.ListOptions{})
if err != nil {

srvResp, err := getServiceInfo(args, namespace, client, cmd)
value, ok := srvResp.(runtime.Object)
if err != nil || !ok {
return err
}
if len(service.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
return nil
}
service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Service"})

printer, err := serviceGetFlags.ToPrinter()
if err != nil {
return err
}

err = printer.PrintObj(service, cmd.OutOrStdout())
err = printer.PrintObj(value, cmd.OutOrStdout())
if err != nil {
return err
}
Expand All @@ -68,3 +77,38 @@ func NewServiceGetCommand(p *commands.KnParams) *cobra.Command {
serviceGetFlags.AddFlags(serviceGetCommand)
return serviceGetCommand
}

func getServiceInfo(args []string, namespace string, client serving.ServingV1alpha1Interface, cmd *cobra.Command) (interface{}, error) {

switch len(args) {
case 0:
service, err := client.Services(namespace).List(v1.ListOptions{})
if err != nil {
return nil, err
}
if len(service.Items) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No resources found.\n")
return nil, nil
}
service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "knative.dev",
Version: "v1alpha1",
Kind: "Service"})

return service, nil
case 1:
service, err := client.Services(namespace).Get(args[0], v1.GetOptions{})
if err != nil {
return nil, err
}

service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: "serving.knative.dev",
Version: "v1alpha1",
Kind: "Service"})

return service, nil
default:
return nil, fmt.Errorf("'service get' only accepts no arguments or a single service name, not %d arguments as given", len(args))
}
}
Loading

0 comments on commit dcf4cfa

Please sign in to comment.