Skip to content

Commit

Permalink
Implement human readable output for kn route describe (#643)
Browse files Browse the repository at this point in the history
* Implement human readable output for kn route describe

 - Keeps the machine readable output
 - Align the command description text, check for single argument, reported error messages and unit tests in service, revision, route, source binding describe commands

* Dont print separate section for owner references

* Print Service heading irrespective of owner.Kind == Service

* Align desc, err msg for trigger, apiserver and cronjob source
  • Loading branch information
navidshaikh authored Feb 10, 2020
1 parent 50ae82b commit 15dec58
Show file tree
Hide file tree
Showing 27 changed files with 162 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
|===
| | Description | PR

| 🎁
| Add human readable `kn route describe`
| https://github.com/knative/client/pull/643[#643]

| 🐛
| Show envFrom when running describe service or revision
| https://github.com/knative/client/pull/630[#630]
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_revision.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ kn revision [flags]

* [kn](kn.md) - Knative client
* [kn revision delete](kn_revision_delete.md) - Delete a revision.
* [kn revision describe](kn_revision_describe.md) - Describe revisions.
* [kn revision describe](kn_revision_describe.md) - Show details of a revision
* [kn revision list](kn_revision_list.md) - List available revisions.

4 changes: 2 additions & 2 deletions docs/cmd/kn_revision_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn revision describe

Describe revisions.
Show details of a revision

### Synopsis

Describe revisions.
Show details of a revision

```
kn revision describe NAME [flags]
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ kn route [flags]
### SEE ALSO

* [kn](kn.md) - Knative client
* [kn route describe](kn_route_describe.md) - Describe available route.
* [kn route describe](kn_route_describe.md) - Show details of a route
* [kn route list](kn_route_list.md) - List available routes.

7 changes: 4 additions & 3 deletions docs/cmd/kn_route_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn route describe

Describe available route.
Show details of a route

### Synopsis

Describe available route.
Show details of a route

```
kn route describe NAME [flags]
Expand All @@ -16,8 +16,9 @@ kn route describe NAME [flags]
--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 describe
-n, --namespace string Specify the namespace to operate in.
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file. (default "yaml")
-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].
-v, --verbose More output.
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kn service [flags]
* [kn](kn.md) - Knative client
* [kn service create](kn_service_create.md) - Create a service.
* [kn service delete](kn_service_delete.md) - Delete a service.
* [kn service describe](kn_service_describe.md) - Show details for a given service
* [kn service describe](kn_service_describe.md) - Show details of a service
* [kn service list](kn_service_list.md) - List available services.
* [kn service update](kn_service_update.md) - Update a service.

4 changes: 2 additions & 2 deletions docs/cmd/kn_service_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn service describe

Show details for a given service
Show details of a service

### Synopsis

Show details for a given service
Show details of a service

```
kn service describe NAME [flags]
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_apiserver.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kn source apiserver [flags]
* [kn source](kn_source.md) - Event source command group
* [kn source apiserver create](kn_source_apiserver_create.md) - Create an ApiServer source.
* [kn source apiserver delete](kn_source_apiserver_delete.md) - Delete an ApiServer source.
* [kn source apiserver describe](kn_source_apiserver_describe.md) - Describe an ApiServer source.
* [kn source apiserver describe](kn_source_apiserver_describe.md) - Show details of an ApiServer source
* [kn source apiserver list](kn_source_apiserver_list.md) - List ApiServer sources.
* [kn source apiserver update](kn_source_apiserver_update.md) - Update an ApiServer source.

4 changes: 2 additions & 2 deletions docs/cmd/kn_source_apiserver_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn source apiserver describe

Describe an ApiServer source.
Show details of an ApiServer source

### Synopsis

Describe an ApiServer source.
Show details of an ApiServer source

```
kn source apiserver describe NAME [flags]
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kn source binding [flags]
* [kn source](kn_source.md) - Event source command group
* [kn source binding create](kn_source_binding_create.md) - Create a sink binding.
* [kn source binding delete](kn_source_binding_delete.md) - Delete a sink binding.
* [kn source binding describe](kn_source_binding_describe.md) - Describe a sink binding.
* [kn source binding describe](kn_source_binding_describe.md) - Show details of a sink binding
* [kn source binding list](kn_source_binding_list.md) - List sink bindings.
* [kn source binding update](kn_source_binding_update.md) - Update a sink binding.

4 changes: 2 additions & 2 deletions docs/cmd/kn_source_binding_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn source binding describe

Describe a sink binding.
Show details of a sink binding

### Synopsis

Describe a sink binding.
Show details of a sink binding

```
kn source binding describe NAME [flags]
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_source_cronjob.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kn source cronjob [flags]
* [kn source](kn_source.md) - Event source command group
* [kn source cronjob create](kn_source_cronjob_create.md) - Create a CronJob source.
* [kn source cronjob delete](kn_source_cronjob_delete.md) - Delete a CronJob source.
* [kn source cronjob describe](kn_source_cronjob_describe.md) - Describe a CronJob source.
* [kn source cronjob describe](kn_source_cronjob_describe.md) - Show details of a CronJob source
* [kn source cronjob list](kn_source_cronjob_list.md) - List CronJob sources.
* [kn source cronjob update](kn_source_cronjob_update.md) - Update a CronJob source.

4 changes: 2 additions & 2 deletions docs/cmd/kn_source_cronjob_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn source cronjob describe

Describe a CronJob source.
Show details of a CronJob source

### Synopsis

Describe a CronJob source.
Show details of a CronJob source

```
kn source cronjob describe NAME [flags]
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_trigger.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ kn trigger [flags]
* [kn](kn.md) - Knative client
* [kn trigger create](kn_trigger_create.md) - Create a trigger
* [kn trigger delete](kn_trigger_delete.md) - Delete a trigger.
* [kn trigger describe](kn_trigger_describe.md) - Describe a trigger.
* [kn trigger describe](kn_trigger_describe.md) - Show details of a trigger
* [kn trigger list](kn_trigger_list.md) - List available triggers.
* [kn trigger update](kn_trigger_update.md) - Update a trigger

4 changes: 2 additions & 2 deletions docs/cmd/kn_trigger_describe.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kn trigger describe

Describe a trigger.
Show details of a trigger

### Synopsis

Describe a trigger.
Show details of a trigger

```
kn trigger describe NAME [flags]
Expand Down
7 changes: 3 additions & 4 deletions pkg/kn/commands/revision/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ func NewRevisionDescribeCommand(p *commands.KnParams) *cobra.Command {

command := &cobra.Command{
Use: "describe NAME",
Short: "Describe revisions.",
Short: "Show details of a revision",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires the revision name.")
if len(args) != 1 {
return errors.New("'kn revision describe' requires name of the revision as single argument")
}

namespace, err := p.GetNamespace(cmd)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions pkg/kn/commands/revision/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func fakeRevision(args []string, response *servingv1.Revision) (action clienttes

func TestDescribeRevisionWithNoName(t *testing.T) {
_, _, err := fakeRevision([]string{"revision", "describe"}, &servingv1.Revision{})
expectedError := "requires the revision name."
if err == nil || err.Error() != expectedError {
t.Fatal("expect to fail with missing revision name")
}
assert.ErrorContains(t, err, "requires", "name", "revision", "single", "argument")
}

func TestDescribeRevisionYaml(t *testing.T) {
Expand Down
89 changes: 75 additions & 14 deletions pkg/kn/commands/route/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ package route

import (
"errors"
"fmt"
"io"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/printers"
)

// NewRouteDescribeCommand represents 'kn route describe' command
func NewRouteDescribeCommand(p *commands.KnParams) *cobra.Command {
routeDescribePrintFlags := genericclioptions.NewPrintFlags("").WithDefaultOutput("yaml")
routeDescribeCommand := &cobra.Command{
// For machine readable output
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")
command := &cobra.Command{
Use: "describe NAME",
Short: "Describe available route.",
Short: "Show details of a route",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires the route name.")
if len(args) != 1 {
return errors.New("'kn route describe' requires name of the route as single argument")
}
namespace, err := p.GetNamespace(cmd)
if err != nil {
Expand All @@ -43,23 +48,79 @@ func NewRouteDescribeCommand(p *commands.KnParams) *cobra.Command {
return err
}

describeRoute, err := client.GetRoute(args[0])
route, err := client.GetRoute(args[0])
if err != nil {
return err
}

printer, err := routeDescribePrintFlags.ToPrinter()
if err != nil {
return err
if machineReadablePrintFlags.OutputFlagSpecified() {
printer, err := machineReadablePrintFlags.ToPrinter()
if err != nil {
return err
}
return printer.PrintObj(route, cmd.OutOrStdout())
}
err = printer.PrintObj(describeRoute, cmd.OutOrStdout())
printDetails, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
}
return nil
return describe(cmd.OutOrStdout(), route, printDetails)
},
}
commands.AddNamespaceFlags(routeDescribeCommand.Flags(), false)
routeDescribePrintFlags.AddFlags(routeDescribeCommand)
return routeDescribeCommand
flags := command.Flags()
commands.AddNamespaceFlags(flags, false)
machineReadablePrintFlags.AddFlags(command)
flags.BoolP("verbose", "v", false, "More output.")
return command
}

func describe(w io.Writer, route *servingv1.Route, printDetails bool) error {
dw := printers.NewPrefixWriter(w)
commands.WriteMetadata(dw, &route.ObjectMeta, printDetails)
dw.WriteAttribute("URL", route.Status.URL.String())
writeService(dw, route, printDetails)
dw.WriteLine()
writeTraffic(dw, route)
dw.WriteLine()
commands.WriteConditions(dw, route.Status.Conditions, printDetails)
if err := dw.Flush(); err != nil {
return err
}
return nil
}

func writeService(dw printers.PrefixWriter, route *servingv1.Route, printDetails bool) {
svcName := ""
for _, owner := range route.ObjectMeta.OwnerReferences {
if owner.Kind == "Service" {
svcName = owner.Name
if printDetails {
svcName = fmt.Sprintf("%s (%s)", svcName, owner.APIVersion)
}
}

dw.WriteAttribute("Service", svcName)
}
}

func writeTraffic(dw printers.PrefixWriter, route *servingv1.Route) {
trafficSection := dw.WriteAttribute("Traffic Targets", "")
dw.Flush()
for _, target := range route.Status.Traffic {
section := trafficSection.WriteColsLn(fmt.Sprintf("%3d%%", *target.Percent), formatTarget(target))
if target.Tag != "" {
section.WriteAttribute("URL", target.URL.String())
}
}
}

func formatTarget(target servingv1.TrafficTarget) string {
targetHeader := target.RevisionName
if target.LatestRevision != nil && *target.LatestRevision {
targetHeader = fmt.Sprintf("@latest (%s)", target.RevisionName)
}
if target.Tag != "" {
targetHeader = fmt.Sprintf("%s #%s", targetHeader, target.Tag)
}
return targetHeader
}
Loading

0 comments on commit 15dec58

Please sign in to comment.