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

Add machine readable output for trigger #1121

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
| 🐛
| Embed the namespace in request body while creating channels
| https://github.com/knative/client/pull/1117[#1117]
|===

| 🎁
| Add "url" output format to return broker url in broker describe and channel url in channel describe
| https://github.com/knative/client/pull/1118[#1118]
|===

| 🎁
| Add machine readable output (-o flag) to kn broker describe
| https://github.com/knative/client/pull/1124[#1124]

| 🎁
| Add machine readable output (-o flag) to kn trigger describe
| https://github.com/knative/client/pull/1121[#1121]
|===

### v0.19.0 (2020-11-11)
Expand Down
12 changes: 9 additions & 3 deletions docs/cmd/kn_trigger_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ kn trigger describe NAME

# Describe a trigger with name 'my-trigger'
kn trigger describe my-trigger

# Describe a trigger 'my-trigger' in YAML format
kn trigger describe my-trigger -o yaml
```

### Options

```
-h, --help help for describe
-n, --namespace string Specify the namespace to operate in.
-v, --verbose More output.
--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.
--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
4 changes: 4 additions & 0 deletions pkg/eventing/v1beta1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (c *knEventingClient) GetTrigger(name string) (*v1beta1.Trigger, error) {
if err != nil {
return nil, kn_errors.GetError(err)
}
err = updateEventingGVK(trigger)
if err != nil {
return nil, err
}
return trigger, nil
}

Expand Down
41 changes: 32 additions & 9 deletions pkg/kn/commands/trigger/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ package trigger

import (
"errors"
"fmt"
"strings"

"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/spf13/cobra"

Expand All @@ -26,15 +30,23 @@ import (
"knative.dev/client/pkg/printers"
)

var describeExample = `
# Describe a trigger with name 'my-trigger'
kn trigger describe my-trigger

# Describe a trigger 'my-trigger' in YAML format
kn trigger describe my-trigger -o yaml`

// NewTriggerDescribeCommand returns a new command for describe a trigger
func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command {

triggerDescribe := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a trigger",
Example: `
# Describe a trigger with name 'my-trigger'
kn trigger describe my-trigger`,
// For machine readable output
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

command := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a trigger",
Example: describeExample,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn trigger describe' requires name of the trigger as single argument")
Expand All @@ -59,6 +71,16 @@ func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command {
}

out := cmd.OutOrStdout()

// Print out machine readable output if requested
if machineReadablePrintFlags.OutputFlagSpecified() {
printer, err := machineReadablePrintFlags.ToPrinter()
if err != nil {
return err
}
return printer.PrintObj(trigger, out)
}

dw := printers.NewPrefixWriter(out)

printDetails, err := cmd.Flags().GetBool("verbose")
Expand Down Expand Up @@ -88,11 +110,12 @@ func NewTriggerDescribeCommand(p *commands.KnParams) *cobra.Command {
return nil
},
}
flags := triggerDescribe.Flags()
flags := command.Flags()
commands.AddNamespaceFlags(flags, false)
flags.BoolP("verbose", "v", false, "More output.")

return triggerDescribe
machineReadablePrintFlags.AddFlags(command)
command.Flag("output").Usage = fmt.Sprintf("Output format. One of: %s.", strings.Join(machineReadablePrintFlags.AllowedFormats(), "|"))
return command
}

func writeTrigger(dw printers.PrefixWriter, trigger *v1beta1.Trigger, printDetails bool) {
Expand Down
24 changes: 22 additions & 2 deletions pkg/kn/commands/trigger/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,26 @@ func TestDescribeTriggerWithSinkURI(t *testing.T) {
recorder.Validate()
}

func TestDescribeTriggerMachineReadable(t *testing.T) {
client := clientv1beta1.NewMockKnEventingClient(t, "mynamespace")

recorder := client.Recorder()
recorder.GetTrigger("testtrigger", getTriggerSinkRef(), nil)

output, err := executeTriggerCommand(client, nil, "describe", "testtrigger", "-o", "yaml")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "kind: Trigger", "spec:", "status:", "metadata:"))

// Validate that all recorded API methods have been called
recorder.Validate()
}

func getTriggerSinkRef() *v1beta1.Trigger {
return &v1beta1.Trigger{
TypeMeta: v1.TypeMeta{},
TypeMeta: v1.TypeMeta{
Kind: "Trigger",
APIVersion: "eventing.knative.dev/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testtrigger",
Namespace: "default",
Expand All @@ -110,7 +127,10 @@ func getTriggerSinkRef() *v1beta1.Trigger {

func getTriggerSinkURI() *v1beta1.Trigger {
return &v1beta1.Trigger{
TypeMeta: v1.TypeMeta{},
TypeMeta: v1.TypeMeta{
Kind: "Trigger",
APIVersion: "eventing.knative.dev/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testtrigger",
Namespace: "default",
Expand Down