forked from knative/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds revision get command (knative#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
- Loading branch information
1 parent
d699bac
commit ad94a95
Showing
7 changed files
with
256 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters