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

Adds more columns in service get command output #56

Closed
wants to merge 15 commits into from

Conversation

navidshaikh
Copy link
Collaborator

@navidshaikh navidshaikh commented Apr 4, 2019

This changeset adds basic details about service in
service get command output.
text/tabwriter is used to print columns on stdout.

Fixes ##40

@knative-prow-robot knative-prow-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 4, 2019
@knative-prow-robot knative-prow-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 4, 2019
@navidshaikh navidshaikh changed the title WIP: Adds more columns in service list command output Adds more columns in service list command output Apr 5, 2019
@knative-prow-robot knative-prow-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 5, 2019
@cppforlife
Copy link

/ok-to-test

@knative-prow-robot knative-prow-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 8, 2019
@navidshaikh
Copy link
Collaborator Author

/retest

Copy link
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Looks good to me, but I'm not sure whether tabwriter.Writer is the proper approach as it assumes always equally sized columns (although with a minimum width). I don't think it is optimal for distributing screen real estate.

For example, typically the NAME column is much larger than a status column. E.g. for plain pods its not uncommon to have name of 20 chars, but the READY column will be only around 3-5 chars. Having this equally sized with 20 chars is waste of space and doesnt look good anyway.

I recommend a more customized format. Not sure what Go package is a good fit (maybe fromhttps://github.com/kubernetes/cli-runtime). If it would be Perl, I would use perl formats.

tl;dr - I think we already think about flexible, non equally sized column widths.

@navidshaikh
Copy link
Collaborator Author

@rhuss : Thanks for your review, each column width is re-adjusted based on the cell's max width (of that column). Hard-coding the padding and column width wont scale IMO.
For printers.Flush(), lets use defer to make sure it's called before returning. Sending an update to the PR.

@rhuss
Copy link
Contributor

rhuss commented Apr 15, 2019

@rhuss : Thanks for your review, each column width is re-adjusted based on the cell's max width (of that column). Hard-coding the padding and column width wont scale IMO.

Yeah right. My fault, I've misread the documentation here. I think you are right, we should stay with this approach.

@sixolet
Copy link
Contributor

sixolet commented Apr 15, 2019

/hold
The previous approach for getting a printer enabled a lot of output flags shared with kubectl. This one doesn't seem to, if I read it correctly?

I think we should not implement our own table-writing code.

@knative-prow-robot knative-prow-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 15, 2019
@rhuss
Copy link
Contributor

rhuss commented Apr 16, 2019

The previous approach for getting a printer enabled a lot of output flags shared with kubectl. This one doesn't seem to, if I read it correctly?

I agree, that we should reuse the formatting machinery provided by cli-runtime for creating various output formats when specific options are provided.

I think we should not implement our own table-writing code.

Sorry for my ignorance (just ramping up with the k8s codebase), but doesn't kubectl itself uses custom code for human readable output ? I.e. in https://github.com/kubernetes/kubernetes/blob/c082ace102e217846833baace01d80966369ecd8/pkg/kubectl/cmd/get/get.go#L513 if o.IsHumanReadablePrinter == true then a tabWriter is used like in this PR.

I think we should combine both, but I'm afraid we have to write our own formatting code for the default console output, not covered by CLI options.

@rhuss
Copy link
Contributor

rhuss commented Apr 16, 2019

in kubectl, the tab-writer is used when this combination of options is true:

// human readable printers have special conversion rules, so we determine if we're using one.
	if (len(*o.PrintFlags.OutputFormat) == 0 && len(templateArg) == 0) || *o.PrintFlags.OutputFormat == "wide" {
		o.IsHumanReadablePrinter = true
	}

@rhuss
Copy link
Contributor

rhuss commented Apr 16, 2019

I'm a bit confused: Both kubernetes itself and cli-runtime have a PrintFlags abstraction for obtaining a suitable Printer depending on context. But only Kubernetes itself has a HumanReadableFlags field for obtaining the printer for printing columnwise on the console (as known to kubectl get). kubectl doesn't use cli-runtime for the flags.

I'm pretty sure that the supported printers (JSON, YAML, Name, Template) are not suitable for direct console output.

@sixolet Any suggestion how to proceed? We could either implement a HumanReadableWriter on our own or just use a tabbed writer for the beginning (if no other options have been selected).

@navidshaikh
Copy link
Collaborator Author

navidshaikh commented Apr 16, 2019

@rhuss : Thanks for the feedback on human readable output format and enabling the generic output format flags, I have updated the PR respectively.

Now the command supports printing with generic output format flags. Excerpts

./kn service list --help
List available services.

Usage:
  kn service list [flags]

Flags:
      --all-namespaces                If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
      --allow-missing-template-keys   If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
  -h, --help                          help for list
  -n, --namespace string              List the requested object(s) in given namespace.
  -o, --output string                 Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file.
      --template string               Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
[..]

and the service list

./kn service list
NAME   DOMAIN                   LATESTCREATED   LATESTREADY   AGE
s1     s1.default.example.com   s1-t4mbr        s1-t4mbr      5h
s2     s2.default.example.com   s2-8xrvv        s2-8xrvv      36m

and the service list with jsonpath

./kn service list -o "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}"
s1
s2

@knative-metrics-robot
Copy link

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

File Old Coverage New Coverage Delta
pkg/kn/commands/service_list.go 77.3% 79.5% 2.2

@navidshaikh
Copy link
Collaborator Author

@sixolet : The previous approach for getting printer from cli-runtime is now restored. If no output flags are specified, the tabwriter is used to print the human-readable output.
PTAL
cc @cppforlife

@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: navidshaikh
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: evankanderson

If they are not already assigned, you can assign the PR to them by writing /assign @evankanderson in a comment when ready.

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:

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 added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 24, 2019
@navidshaikh navidshaikh changed the title Adds more columns in service list command output Adds more columns in service get command output Apr 24, 2019
@navidshaikh
Copy link
Collaborator Author

Based on the recent discussions, renamed service list command to service get. service get with an argument should be a separate PR.

@navidshaikh
Copy link
Collaborator Author

I think we should not implement our own table-writing code.

@sixolet @cppforlife What are other recommendations ?

I was checking https://github.com/kubernetes/kubernetes/tree/master/pkg/printers to see if we can re-use it, however this would add dependency on kube-versioned packages. I think we don't want that ?

Copy link
Contributor

@sixolet sixolet left a comment

Choose a reason for hiding this comment

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

So, I still have some trouble with the approach here. I'd like to see something that enables us to reuse some of the resource printing capabilities of k8s.io genericclioptions, and not have to reimplmeent that from scratch. That means using some of their interfaces, like before. I've linked them below in other comments.

// See the License for the specific language governing permissions and
// limitations under the License.

package printers
Copy link
Contributor

Choose a reason for hiding this comment

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

pkg/util or pkg/util/printers?

@@ -0,0 +1,30 @@
// Copyright © 2018 The Knative Authors
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not in love with "util" as a name of things. it seems like the thing people name a bin where they stick things they don't know where else to put.

@@ -0,0 +1,34 @@
// Copyright © 2018 The Knative Authors
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd love pkg/output or pkg/printing or something.

return nil
}
// if no output format flag is set, lets print the human readable output
printer := printers.NewTabWriter(cmd.OutOrStdout())
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a solved problem for kubectl and the human-friendly printer implementation is in place already https://github.com/kubernetes/kubernetes/blob/master/pkg/printers/tableprinter.go#L49 ,
however its not importable (or we don't want k8s dep) as genericclioptions.
A bunch of utilities can be re-used though from pkg/printers (re-use = borrow the code?), and we'll just need to add the column-definitions and their printing logic as done here
https://github.com/kubernetes/kubernetes/blob/master/pkg/printers/internalversion/printers.go#L79 for resources operable via kn.

WDYT?
Cc: @bbrowning @rhuss

Copy link
Contributor

Choose a reason for hiding this comment

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

@navidshaikh If there are no efforts underway to port that part to cli-runtime anyway, I would try to move that tableprinter stuff to kn in an isolated way and if this is successful, create a PR for cli-runtime so that we could use it from there eventually. It depends on the size of the code to move around and how easy it is to isolate it, but I think this could be a strategy from which everyone could benefit.

We could do it the other way round (first move it to cli-runtime), but this is (a) doesn't help for kn in the short term and (b) if we fail at cli-runtime, the effort is wasted. That's not the case with the first approach where the only danger is, that we would have to maintain the ported code on our own.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@rhuss : makes sense, lets move ahead with first approach as you mentioned.

 This commit adds basic details about service in
 service list command output.
 text/tabwriter is used to print columns on stdout.

 Fixes #knative#40
 This commit adds AGE column representing the resources' age
 in human readable format calculated from CreationTimestamp
 field of service.
 Now if list is empty, we print "No resources found." string.
 This commit updates the respective unit test.
 Doesn't check the argument length after `service get` to leave
 room for filtering based on argument.
@navidshaikh
Copy link
Collaborator Author

Closing in favor of #90

@navidshaikh navidshaikh closed this May 9, 2019
coryrc pushed a commit to coryrc/client that referenced this pull request May 14, 2020
The remote caching jobs are not configured, and anyway Knative is not using bazel to the point that remote caching is a benefit.

Disable it to avoid spurious error messages in the test logs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants