Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when output is set to name #775

Merged
merged 3 commits into from
Apr 14, 2020

Conversation

daisy-ycguo
Copy link
Member

Describe

Fix the error when calling kn service list -o name. Because the default name printer only supports unstructured.UnstructuredList, we have to convert servingv1.ServiceList to unstructured.UnstructuredList when output flag is set.

Changes

  • Convert servingv1.ServiceList to unstructured.UnstructuredList when output flag is set in kn service list -o name

Reference

Fixes #754

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Apr 2, 2020
@knative-prow-robot knative-prow-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 2, 2020

}

func toUnstructured(obj runtime.Object) (*unstructured.Unstructured, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@daisy-ycguo daisy-ycguo Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information.
I checked pkg/apis/duck, and noticed it only accepts an interface duck.OneOfOurs. Unfortunately, neither servingv1.ServiceList nor servingv1.Service , runtime.Object cannot satisfy that interface. So I cannot leverage it.

@knative-prow-robot knative-prow-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 3, 2020
@knative-prow-robot knative-prow-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 3, 2020
Copy link
Contributor

@maximilien maximilien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See feedback

if err != nil {
return err
if serviceListFlags.GenericPrintFlags.OutputFlagSpecified() {
unstructedList, err := util.ToUnstructuredList(serviceList)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/unstructedList/unstructuredList/

Copy link
Member Author

@daisy-ycguo daisy-ycguo Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in the new version.


// ToUnstructuredList is to converts an object to unstructured.UnstructuredList.
// If the object is not a list type, it will convert to a single item UnstructuredList.
func ToUnstructuredList(obj runtime.Object) (*unstructured.UnstructuredList, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the unit test for this ToUnstructuredList exported function?

Copy link
Member Author

@daisy-ycguo daisy-ycguo Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see TestToUnstructuredList in pkg/util/unstructured_test.go

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 3, 2020
Copy link
Collaborator

@navidshaikh navidshaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can solve this and also improve the list printing for all command groups.
If we pull the following check earlier in pkg/kn/commands/flags/listprint.go

			if serviceListFlags.GenericPrintFlags.OutputFlagSpecified() {
				unstructedList, err := util.ToUnstructuredList(serviceList)

and add another method Print to ListPrintFlags taking care of finding correct printer and above check+conversion,
we solve this for other list commands too.
I tried doing ^^ on your PR and replaced the print call for revision list (can be done in similar way for route.. etc)

➜  client git:(f5a4863b) ✗ git --no-pager diff .
diff --git a/pkg/kn/commands/flags/listprint.go b/pkg/kn/commands/flags/listprint.go
index a19cca49..2ec1c81d 100644
--- a/pkg/kn/commands/flags/listprint.go
+++ b/pkg/kn/commands/flags/listprint.go
@@ -15,11 +15,15 @@
 package flags
 
 import (
+	"io"
+
 	"github.com/spf13/cobra"
+	"k8s.io/apimachinery/pkg/runtime"
 	"k8s.io/cli-runtime/pkg/genericclioptions"
 
 	"knative.dev/client/pkg/kn/commands"
 	hprinters "knative.dev/client/pkg/printers"
+	"knative.dev/client/pkg/util"
 )
 
 // ListFlags composes common printer flag structs
@@ -56,6 +60,23 @@ func (f *ListPrintFlags) ToPrinter() (hprinters.ResourcePrinter, error) {
 	return p, nil
 }
 
+func (f *ListPrintFlags) Print(obj runtime.Object, w io.Writer) error {
+	printer, err := f.ToPrinter()
+	if err != nil {
+		return err
+	}
+
+	if f.GenericPrintFlags.OutputFlagSpecified() {
+		unstructedList, err := util.ToUnstructuredList(obj)
+		if err != nil {
+			return err
+		}
+		return printer.PrintObj(unstructedList, w)
+	}
+
+	return printer.PrintObj(obj, w)
+}
+
 // AddFlags receives a *cobra.Command reference and binds
 // flags related to humanreadable and template printing.
 func (f *ListPrintFlags) AddFlags(cmd *cobra.Command) {
diff --git a/pkg/kn/commands/revision/list.go b/pkg/kn/commands/revision/list.go
index 88f733f5..3821d76e 100644
--- a/pkg/kn/commands/revision/list.go
+++ b/pkg/kn/commands/revision/list.go
@@ -102,13 +102,7 @@ func NewRevisionListCommand(p *commands.KnParams) *cobra.Command {
 			// Sort revisions by namespace, service, generation (in this order)
 			sortRevisions(revisionList)
 
-			// Print out infos via printer framework
-			printer, err := revisionListFlags.ToPrinter()
-			if err != nil {
-				return err
-			}
-
-			return printer.PrintObj(revisionList, cmd.OutOrStdout())
+			return revisionListFlags.Print(revisionList, cmd.OutOrStdout())
 		},
 	}
 	commands.AddNamespaceFlags(revisionListCommand.Flags(), true)
diff --git a/pkg/kn/commands/service/list.go b/pkg/kn/commands/service/list.go
index 974db353..10a3308c 100644
--- a/pkg/kn/commands/service/list.go
+++ b/pkg/kn/commands/service/list.go
@@ -24,7 +24,6 @@ import (
 	"knative.dev/client/pkg/kn/commands"
 	"knative.dev/client/pkg/kn/commands/flags"
 	clientservingv1 "knative.dev/client/pkg/serving/v1"
-	"knative.dev/client/pkg/util"
 )
 
 // NewServiceListCommand represents 'kn service list' command
@@ -66,11 +65,6 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
 				serviceListFlags.EnsureWithNamespace()
 			}
 
-			printer, err := serviceListFlags.ToPrinter()
-			if err != nil {
-				return err
-			}
-
 			// Sort serviceList by namespace and name (in this order)
 			sort.SliceStable(serviceList.Items, func(i, j int) bool {
 				a := serviceList.Items[i]
@@ -82,15 +76,7 @@ func NewServiceListCommand(p *commands.KnParams) *cobra.Command {
 				return a.ObjectMeta.Name < b.ObjectMeta.Name
 			})
 
-			if serviceListFlags.GenericPrintFlags.OutputFlagSpecified() {
-				unstructedList, err := util.ToUnstructuredList(serviceList)
-				if err != nil {
-					return err
-				}
-				return printer.PrintObj(unstructedList, cmd.OutOrStdout())
-			}
-
-			return printer.PrintObj(serviceList, cmd.OutOrStdout())
+			return serviceListFlags.Print(serviceList, cmd.OutOrStdout())
 		},
 	}
 	commands.AddNamespaceFlags(serviceListCommand.Flags(), true)

here is the output:

➜  client git:(f5a4863b) ✗ ./kn revision list -oname
revision.serving.knative.dev/svc-tgzvv-1
➜  client git:(f5a4863b) ✗ ./kn service list -oname 
service.serving.knative.dev/svc

@navidshaikh
Copy link
Collaborator

/retest

Copy link
Collaborator

@navidshaikh navidshaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daisy-ycguo needs rebase and changes in e2e as e2e refactor PR is in now

test/e2e/basic_workflow_test.go:52:19: not enough arguments in call to serviceListOutput
	have (*test.KnRunResultCollector, string)
	want (*test.KnRunResultCollector, *test.KnRunResultCollector, string)

@knative-metrics-robot
Copy link

The following is the coverage report on the affected files.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/kn/commands/flags/listprint.go 62.5% 56.0% -6.5
pkg/kn/commands/service/list.go 89.5% 93.8% 4.3
pkg/util/unstructured.go Do not exist 78.3%

@daisy-ycguo
Copy link
Member Author

@navidshaikh This PR is ready for review.

Copy link
Collaborator

@navidshaikh navidshaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

Thanks Daisy!

unstructuredList.Items = append(unstructuredList.Items, *ud)
}
return unstructuredList, nil

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -0,0 +1,86 @@
// Copyright © 2019 The Knative Authors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright © 2019 The Knative Authors
// Copyright © 2020 The Knative Authors

"metadata": map[string]interface{}{
"namespace": "default",
"name": name,
"creationTimestamp": nil,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it require to set creationTimeStamp field ?

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Apr 14, 2020
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: daisy-ycguo, maximilien, navidshaikh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [maximilien,navidshaikh]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot merged commit d725ef5 into knative:master Apr 14, 2020
@navidshaikh navidshaikh added the backport/candidate Consider this PR to be backported to the release branch label Apr 15, 2020
rhuss pushed a commit to rhuss/knative-client that referenced this pull request Apr 15, 2020
* fix error when output is set to name

* add e2e test

* change to flags/listprint.go

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	test/e2e/basic_workflow_test.go
knative-prow-robot pushed a commit that referenced this pull request Apr 15, 2020
* (refactor) address the e2e extract / refactor of issue #763 (#765)

* (refactor) address the e2e extract / refactor of issue #763

* various updates to address reviewers feedback

* renamed lib/test/integration to lib/test and package to test

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	CHANGELOG.adoc
#	test/e2e/service_export_import_apply_test.go
#	test/e2e/trigger_test.go

* fix(plugin): Fix plugin lookup with file ext on Windows (#774)

* fix(plugin): Fix plugin lookup with file ext on Windows

* chore: Update changelog

* fix: Reflect review feedback

* fix: Reflect review feedback and add future todo

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	CHANGELOG.adoc

* fix(issue #762): correct error message when updating service (#778)

* fix(issue #762): correct error message when updating service

* correct message when updating service and passing many names
* fix issue with TestServiceUpdateWithMultipleImages running create vs update

* * added TestServiceDescribeWithMultipleNames
* added TestServiceCreateWithMultipleNames
* fix error message for service delete since many names can be passed

* Use vendored deps while running e2e locally (#783)

Also set GO111MODULE=on unconditionally

* Update sink binding create usage string (#785)

* Add "--target-utilization" to manage "autoscaling.knative.dev/targetUtilizationPercentage" annotation (#788)

* Support setting "autoscaling.knative.dev/targetUtilizationPercentage" annotation.

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	test/e2e/service_options_test.go

* Remove the delete propagation flag (#770)

* Remove the delete propagation flag

In it's current state it now takes me about 25 seconds for the `kn delete`
to complete. Before #682 it used to be
almost immediate. This is because we now pass in the
`DeletePropagationBackground` flag. I believe this is a mistake, not only
because of the 20+ seconds of additional time to delete things, but IMO
the CLI should talk to the server in the same way regardless of the --wait
flag. That flag should just be a CLI thing to indicate if the user wants the CLI
to wait for the server to complete but not HOW the server should do the delete.

Signed-off-by: Doug Davis <[email protected]>

* try just tweaking the --no-wait flag

Signed-off-by: Doug Davis <[email protected]>

* Fix error when output is set to name (#775)

* fix error when output is set to name

* add e2e test

* change to flags/listprint.go

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	test/e2e/basic_workflow_test.go

* Show all revisions when run `service describe -v` (#790)

* The `kn service describe -v` command shows repetitive revisions, because
  the revision would be covered by next one.

* Fix resource listing with -oname flag (#799)

* Fix resource listing with -oname flag

* add e2e tests

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	test/e2e/ping_test.go
#	test/e2e/revision_test.go
#	test/e2e/route_test.go
#	test/e2e/source_apiserver_test.go
#	test/e2e/source_binding_test.go
#	test/e2e/trigger_test.go

* Make wait, no-wait and async flags per bool var CLI convention (#802)

* Make wait, no-wait and async flags per bool var CLI convention

 Fixes #800

 - Deprecated bool vars can be supported for CLI convention
 - Bind --async flag value to --no-wait
 - Only one flag among [wait, no-wait, async] can be provided, else raise an error

* Simplify conditionals

* Add unit tests for deprecated flag async

* Fix a typo

* e2e: Foreground delete for revisions and services in e2e (#794)

* e2e: Foreground delete for revisions and services in e2e

 to avoid any race conditions and flakes

* Use --wait instead of --no-wait=false

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	test/e2e/basic_workflow_test.go
#	test/e2e/revision_test.go

* e2e: Run tekton e2e against pipeline v0.11.1 (#803)

* Use buildah task from master branch and paramterize FORMAT

* Configure pipeline v0.11.1

* DNM: Run tekton e2e in this PR

* Revert "DNM: Run tekton e2e in this PR"

This reverts commit 903f5be.

* Update CHANGELOG for v0.13.2 (#804)

* Pin serving to v0.13.2 and update version command (#797)

* Pin serving v0.13.2 dep to v0.13.2

* Update version command

 now points to serving v0.13.2 and eventing v0.13.6

* Copy go.sum as generated in CI

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	go.mod
#	go.sum
#	vendor/modules.txt

* add missing vendored files

* fixed error reporting for traffics tests

* Updated test

* fix formatting

* e2e for service export (#739)

* e2e for service export

* e2e for service export

* e2e for service export

* e2e for service export

* e2e for service export

Signed-off-by: Roland Huß <[email protected]>
# Conflicts:
#	test/e2e/service_export_import_apply_test.go

Co-authored-by: dr.max <[email protected]>
Co-authored-by: David Simansky <[email protected]>
Co-authored-by: Navid Shaikh <[email protected]>
Co-authored-by: Lv Jiawei <[email protected]>
Co-authored-by: Doug Davis <[email protected]>
Co-authored-by: Ying Chun Guo <[email protected]>
Co-authored-by: Murugappan Chetty <[email protected]>
@navidshaikh navidshaikh added backport/pr A backport PR which is target to a release branch. and removed backport/candidate Consider this PR to be backported to the release branch labels Apr 20, 2020
@rhuss rhuss added backported-to/0.13 and removed backport/pr A backport PR which is target to a release branch. labels Apr 20, 2020
mgencur pushed a commit to openshift-knative/client that referenced this pull request Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kn service list -o name doesn't seem to work
7 participants