Skip to content

Commit

Permalink
add url output for broker and channel
Browse files Browse the repository at this point in the history
Signed-off-by: Arghya Sadhu <[email protected]>
  • Loading branch information
arghya88 committed Nov 13, 2020
1 parent 6508fed commit a6673e5
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
| 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]
|===

### v0.19.0 (2020-11-11)
[cols="1,10,3", options="header", width="100%"]
|===
Expand Down
10 changes: 8 additions & 2 deletions docs/cmd/kn_broker_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ kn broker describe NAME
# Describe broker 'mybroker' in the 'myproject' namespace
kn broker describe mybroker --namespace myproject
# Print only broker URL
kn broker describe mybroker -o url
```

### Options

```
-h, --help help for describe
-n, --namespace string Specify the namespace to operate in.
--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|url.
--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].
```

### Options inherited from parent commands
Expand Down
5 changes: 4 additions & 1 deletion docs/cmd/kn_channel_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ kn channel describe NAME
# Describe a channel 'pipe'
kn channel describe pipe
# Print only channel URL
kn channel describe pipe -o url
```

### Options
Expand All @@ -24,7 +27,7 @@ kn channel describe NAME
--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.
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-file|url.
--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.
```
Expand Down
36 changes: 33 additions & 3 deletions pkg/kn/commands/broker/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ package broker

import (
"errors"
"fmt"
"io"
"strings"

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

"github.com/spf13/cobra"

Expand All @@ -33,11 +37,17 @@ var describeExample = `
kn broker describe mybroker
# Describe broker 'mybroker' in the 'myproject' namespace
kn broker describe mybroker --namespace myproject`
kn broker describe mybroker --namespace myproject
# Print only broker URL
kn broker describe mybroker -o url`

// NewBrokerDescribeCommand represents command to describe details of broker instance
func NewBrokerDescribeCommand(p *commands.KnParams) *cobra.Command {

// For machine readable output
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

cmd := &cobra.Command{
Use: "describe NAME",
Short: "Describe broker",
Expand All @@ -62,10 +72,26 @@ func NewBrokerDescribeCommand(p *commands.KnParams) *cobra.Command {
if err != nil {
return err
}
return describeBroker(cmd.OutOrStdout(), broker, false)

out := cmd.OutOrStdout()

if machineReadablePrintFlags.OutputFlagSpecified() {
if strings.ToLower(*machineReadablePrintFlags.OutputFormat) == "url" {
fmt.Fprintf(out, "%s\n", extractURL(broker))
return nil
}
printer, err := machineReadablePrintFlags.ToPrinter()
if err != nil {
return err
}
return printer.PrintObj(broker, out)
}
return describeBroker(out, broker, false)
},
}
commands.AddNamespaceFlags(cmd.Flags(), false)
machineReadablePrintFlags.AddFlags(cmd)
cmd.Flag("output").Usage = fmt.Sprintf("Output format. One of: %s.", strings.Join(append(machineReadablePrintFlags.AllowedFormats(), "url"), "|"))
return cmd
}

Expand All @@ -74,11 +100,15 @@ func describeBroker(out io.Writer, broker *v1beta1.Broker, printDetails bool) er
dw := printers.NewPrefixWriter(out)
commands.WriteMetadata(dw, &broker.ObjectMeta, printDetails)
dw.WriteLine()
dw.WriteAttribute("Address", "").WriteAttribute("URL", broker.Status.Address.URL.String())
dw.WriteAttribute("Address", "").WriteAttribute("URL", extractURL(broker))
dw.WriteLine()
commands.WriteConditions(dw, broker.Status.Conditions, printDetails)
if err := dw.Flush(); err != nil {
return err
}
return nil
}

func extractURL(broker *v1beta1.Broker) string {
return broker.Status.Address.URL.String()
}
13 changes: 13 additions & 0 deletions pkg/kn/commands/broker/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ func TestDescribeError(t *testing.T) {
recorder.Validate()
}

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

recorder := client.Recorder()
recorder.GetBroker("foo", getBroker(), nil)

out, err := executeBrokerCommand(client, "describe", "foo", "-o", "url")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(out, "http://foo-broker.test"))

recorder.Validate()
}

func getBroker() *v1beta1.Broker {
return &v1beta1.Broker{
TypeMeta: v1.TypeMeta{},
Expand Down
18 changes: 18 additions & 0 deletions pkg/kn/commands/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ package channel
import (
"bytes"

"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/clientcmd"
"knative.dev/eventing/pkg/apis/messaging/v1beta1"

"knative.dev/client/pkg/kn/commands"
clientv1beta1 "knative.dev/client/pkg/messaging/v1beta1"
eventingduck "knative.dev/eventing/pkg/apis/duck/v1beta1"
)

// Helper methods
Expand Down Expand Up @@ -79,3 +83,17 @@ func cleanupChannelMockClient() {
func createChannel(name, namespace string, gvk *schema.GroupVersionKind) *v1beta1.Channel {
return clientv1beta1.NewChannelBuilder(name, namespace).Type(gvk).Build()
}

func createChannelWithStatus(name string, gvk *schema.GroupVersionKind) *v1beta1.Channel {
channel := clientv1beta1.NewChannelBuilder(name).Type(gvk).Build()
channel.Status = v1beta1.ChannelStatus{
ChannelableStatus: eventingduck.ChannelableStatus{
AddressStatus: duckv1.AddressStatus{
Address: &duckv1.Addressable{
URL: &apis.URL{Scheme: "http", Host: "pipe-channel.test"},
},
},
},
}
return channel
}
27 changes: 21 additions & 6 deletions pkg/kn/commands/channel/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package channel
import (
"errors"
"fmt"
"strings"

"github.com/spf13/cobra"

Expand All @@ -28,18 +29,23 @@ import (
"knative.dev/client/pkg/printers"
)

var describeExample = `
# Describe a channel 'pipe'
kn channel describe pipe
# Print only channel URL
kn channel describe pipe -o url`

// NewChannelDescribeCommand returns a new command for describe a channel object
func NewChannelDescribeCommand(p *commands.KnParams) *cobra.Command {

// For machine readable output
machineReadablePrintFlags := genericclioptions.NewPrintFlags("")

cmd := &cobra.Command{
Use: "describe NAME",
Short: "Show details of a channel",
Example: `
# Describe a channel 'pipe'
kn channel describe pipe`,
Use: "describe NAME",
Short: "Show details of a channel",
Example: describeExample,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("'kn channel describe' requires the channel name given as single argument")
Expand All @@ -59,6 +65,10 @@ func NewChannelDescribeCommand(p *commands.KnParams) *cobra.Command {
out := cmd.OutOrStdout()

if machineReadablePrintFlags.OutputFlagSpecified() {
if strings.ToLower(*machineReadablePrintFlags.OutputFormat) == "url" {
fmt.Fprintf(out, "%s\n", extractURL(channel))
return nil
}
printer, err := machineReadablePrintFlags.ToPrinter()
if err != nil {
return err
Expand Down Expand Up @@ -92,6 +102,7 @@ func NewChannelDescribeCommand(p *commands.KnParams) *cobra.Command {
commands.AddNamespaceFlags(flags, false)
flags.BoolP("verbose", "v", false, "More output.")
machineReadablePrintFlags.AddFlags(cmd)
cmd.Flag("output").Usage = fmt.Sprintf("Output format. One of: %s.", strings.Join(append(machineReadablePrintFlags.AllowedFormats(), "url"), "|"))
return cmd
}

Expand All @@ -100,6 +111,10 @@ func writeChannel(dw printers.PrefixWriter, channel *messagingv1beta1.Channel, p
ctype := fmt.Sprintf("%s (%s)", channel.Spec.ChannelTemplate.Kind, channel.Spec.ChannelTemplate.APIVersion)
dw.WriteAttribute("Type", ctype)
if channel.Status.Address != nil {
dw.WriteAttribute("URL", channel.Status.Address.URL.String())
dw.WriteAttribute("URL", extractURL(channel))
}
}

func extractURL(channel *messagingv1beta1.Channel) string {
return channel.Status.Address.URL.String()
}
10 changes: 10 additions & 0 deletions pkg/kn/commands/channel/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ func TestDescribeChannel(t *testing.T) {
assert.Assert(t, util.ContainsAll(out, "messaging.knative.dev", "v1beta1", "InMemoryChannel", "pipe"))
cRecorder.Validate()
}

func TestDescribeChannelURL(t *testing.T) {
cClient := v1beta1.NewMockKnChannelsClient(t)
cRecorder := cClient.Recorder()
cRecorder.GetChannel("pipe", createChannelWithStatus("pipe", &schema.GroupVersionKind{Group: "messaging.knative.dev", Version: "v1beta1", Kind: "InMemoryChannel"}), nil)
out, err := executeChannelCommand(cClient, "describe", "pipe", "-o", "url")
assert.NilError(t, err, "channel should be described with url as output")
assert.Assert(t, util.ContainsAll(out, "pipe-channel.test"))
cRecorder.Validate()
}

0 comments on commit a6673e5

Please sign in to comment.