Skip to content

Commit

Permalink
feat(channel): Manage knative eventing channels (#967)
Browse files Browse the repository at this point in the history
* feat(channel): Manage knative eventing channels

- Support specifying the type of channel to create using --type.
 - Default is to use messaging layer configuration for channels
- Channel type aliases via kn config and alias for InMemoryChannel
 - User can now configure channel type aliases in kn config and specify
   alias to GVK mappings for easy reference on CLI and refer with `--type` flag
 - User can also use inbuilt alias 'imc' for InMemoryChannel

* Let channel reconcile, sleep for 5 secs after creation

* Add imcv1 and imcv1beta1 aliases

* Rename imcv1 alias to imc
  • Loading branch information
navidshaikh authored Aug 25, 2020
1 parent a01861b commit 5ed353d
Show file tree
Hide file tree
Showing 42 changed files with 2,523 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
| ✨
| `kn source list` output now has client custom GVK set as `{Group: client.knative.dev, Version: v1alpha1, Kind: SourceList}`
| https://github.com/knative/client/pull/980[#980]

| 🎁
| Manage knative eventing channels
| https://github.com/knative/client/pull/967[#967]
|===

## v0.16.0 (2020-07-14)
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ kn is the command line interface for managing Knative Serving and Eventing resou
### SEE ALSO

* [kn broker](kn_broker.md) - Manage message broker
* [kn channel](kn_channel.md) - Manage event channels
* [kn completion](kn_completion.md) - Output shell completion code
* [kn options](kn_options.md) - Print the list of flags inherited by all commands
* [kn plugin](kn_plugin.md) - Manage kn plugins
Expand Down
34 changes: 34 additions & 0 deletions docs/cmd/kn_channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## kn channel

Manage event channels

### Synopsis

Manage event channels

```
kn channel COMMAND
```

### Options

```
-h, --help help for channel
```

### Options inherited from parent commands

```
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn](kn.md) - kn manages Knative Serving and Eventing resources
* [kn channel create](kn_channel_create.md) - Create an event channel
* [kn channel delete](kn_channel_delete.md) - Delete a channel
* [kn channel describe](kn_channel_describe.md) - Show details of a channel
* [kn channel list](kn_channel_list.md) - List channels

48 changes: 48 additions & 0 deletions docs/cmd/kn_channel_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## kn channel create

Create an event channel

### Synopsis

Create an event channel

```
kn channel create NAME
```

### Examples

```
# Create a channel 'pipe' with default setting for channel configuration
kn channel create pipe
# Create a channel 'imc1' of type InMemoryChannel using inbuilt alias 'imc'
kn channel create imc1 --type imc
# same as above without using inbuilt alias but providing explicit GVK
kn channel create imc1 --type messaging.knative.dev:v1beta1:InMemoryChannel
# Create a channel 'k1' of type KafkaChannel
kn channel create k1 --type messaging.knative.dev:v1alpha1:KafkaChannel
```

### Options

```
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
--type string Override channel type to create, in the format '--type Group:Version:Kind'. If flag is not specified, it uses default messaging layer settings for channel type, cluster wide or specific namespace. You can configure aliases for channel types in kn config and refer the aliases with this flag. You can also refer inbuilt channel type InMemoryChannel using an alias 'imc' like '--type imc'. Examples: '--type messaging.knative.dev:v1alpha1:KafkaChannel' for specifying explicit Group:Version:Kind.
```

### Options inherited from parent commands

```
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn channel](kn_channel.md) - Manage event channels

39 changes: 39 additions & 0 deletions docs/cmd/kn_channel_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## kn channel delete

Delete a channel

### Synopsis

Delete a channel

```
kn channel delete NAME
```

### Examples

```
# Delete a channel 'pipe'
kn channel delete pipe
```

### Options

```
-h, --help help for delete
-n, --namespace string Specify the namespace to operate in.
```

### Options inherited from parent commands

```
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn channel](kn_channel.md) - Manage event channels

43 changes: 43 additions & 0 deletions docs/cmd/kn_channel_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## kn channel describe

Show details of a channel

### Synopsis

Show details of a channel

```
kn channel describe NAME
```

### Examples

```
# Describe a channel 'pipe'
kn channel describe pipe
```

### Options

```
--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

```
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn channel](kn_channel.md) - Manage event channels

47 changes: 47 additions & 0 deletions docs/cmd/kn_channel_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## kn channel list

List channels

### Synopsis

List channels

```
kn channel list
```

### Examples

```
# List all channels
kn channel list
# List channels in YAML format
kn channel ping list -o yaml
```

### Options

```
-A, --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 Specify the namespace to operate in.
--no-headers When using the default output format, don't print headers (default: print headers).
-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].
```

### Options inherited from parent commands

```
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn channel](kn_channel.md) - Manage event channels

55 changes: 55 additions & 0 deletions lib/test/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 The Knative Authors

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or im
// See the License for the specific language governing permissions and
// limitations under the License.

package test

import (
"time"

"gotest.tools/assert"

"knative.dev/client/pkg/util"
)

func ChannelCreate(r *KnRunResultCollector, cname string, args ...string) {
cmd := []string{"channel", "create", cname}
cmd = append(cmd, args...)
out := r.KnTest().Kn().Run(cmd...)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "channel", cname, "created"))
// let channel reconcile TODO: fix the wait for channel to become ready
time.Sleep(5 * time.Second)
}

func ChannelList(r *KnRunResultCollector, args ...string) string {
cmd := []string{"channel", "list"}
cmd = append(cmd, args...)
out := r.KnTest().Kn().Run(cmd...)
r.AssertNoError(out)
return out.Stdout
}

func ChannelDescribe(r *KnRunResultCollector, cname string, args ...string) string {
cmd := []string{"channel", "describe", cname}
cmd = append(cmd, args...)
out := r.KnTest().Kn().Run(cmd...)
r.AssertNoError(out)
return out.Stdout
}

func ChannelDelete(r *KnRunResultCollector, cname string) {
out := r.KnTest().Kn().Run("channel", "delete", cname)
r.AssertNoError(out)
assert.Check(r.T(), util.ContainsAllIgnoreCase(out.Stdout, "channel", cname, "deleted"))
}
4 changes: 4 additions & 0 deletions pkg/errors/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func isEmptyConfigError(err error) bool {
//Retrieves a custom error struct based on the original error APIStatus struct
//Returns the original error struct in case it can't identify the kind of APIStatus error
func GetError(err error) error {
if err == nil {
return nil
}

switch {
case isEmptyConfigError(err):
return newNoKubeConfig(err.Error())
Expand Down
4 changes: 4 additions & 0 deletions pkg/errors/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,7 @@ func TestIsForbiddenError(t *testing.T) {
})
}
}

func TestNilError(t *testing.T) {
assert.NilError(t, GetError(nil), nil)
}
67 changes: 67 additions & 0 deletions pkg/kn/commands/channel/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2020 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package channel

import (
"github.com/spf13/cobra"

"k8s.io/client-go/tools/clientcmd"
clientv1beta1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/messaging/v1beta1"

"knative.dev/client/pkg/kn/commands"
messagingv1beta1 "knative.dev/client/pkg/messaging/v1beta1"
)

// NewChannelCommand to manage event channels
func NewChannelCommand(p *commands.KnParams) *cobra.Command {
channelCmd := &cobra.Command{
Use: "channel COMMAND",
Short: "Manage event channels",
}
channelCmd.AddCommand(NewChannelCreateCommand(p))
channelCmd.AddCommand(NewChannelListCommand(p))
channelCmd.AddCommand(NewChannelDeleteCommand(p))
channelCmd.AddCommand(NewChannelDescribeCommand(p))
return channelCmd
}

var channelClientFactory func(config clientcmd.ClientConfig, namespace string) (messagingv1beta1.KnChannelsClient, error)

func newChannelClient(p *commands.KnParams, cmd *cobra.Command) (messagingv1beta1.KnChannelsClient, error) {
namespace, err := p.GetNamespace(cmd)
if err != nil {
return nil, err
}

if channelClientFactory != nil {
config, err := p.GetClientConfig()
if err != nil {
return nil, err
}
return channelClientFactory(config, namespace)
}

clientConfig, err := p.RestConfig()
if err != nil {
return nil, err
}

client, err := clientv1beta1.NewForConfig(clientConfig)
if err != nil {
return nil, err
}

return messagingv1beta1.NewKnMessagingClient(client, namespace).ChannelsClient(), nil
}
Loading

0 comments on commit 5ed353d

Please sign in to comment.