Skip to content

Commit

Permalink
Support specifying the type of channel to create using --type
Browse files Browse the repository at this point in the history
 Default is to use messaging layer configuration for channels.
  • Loading branch information
navidshaikh committed Aug 4, 2020
1 parent b99e5f9 commit f119bed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
11 changes: 9 additions & 2 deletions docs/cmd/kn_channel_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ kn channel create NAME

```
# Create a channel 'pipe' in namespace 'flow'
kn channel create pipe -n flow
# Create a channel 'pipe' with default setting for channel configuration
kn channel create pipe
# Create a channel 'imc1' of type InMemoryChannel
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 Type of the channel to create in the format 'Group:Version:Kind'. For example: '--type messaging.knative.dev:v1alpha1:KafkaChannel'. Default is to use messaging layer setting for default channel configuration.
```

### Options inherited from parent commands
Expand Down
34 changes: 31 additions & 3 deletions pkg/kn/commands/channel/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package channel
import (
"errors"
"fmt"
"strings"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime/schema"

knerrors "knative.dev/client/pkg/errors"
"knative.dev/client/pkg/kn/commands"
Expand All @@ -31,8 +33,15 @@ func NewChannelCreateCommand(p *commands.KnParams) *cobra.Command {
Use: "create NAME",
Short: "Create an event channel",
Example: `
# Create a channel 'pipe' in namespace 'flow'
kn channel create pipe -n flow`,
# Create a channel 'pipe' with default setting for channel configuration
kn channel create pipe
# Create a channel 'imc1' of type InMemoryChannel
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`,

RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'kn channel create' requires the channel name given as single argument")
Expand All @@ -49,7 +58,17 @@ func NewChannelCreateCommand(p *commands.KnParams) *cobra.Command {
return err
}

err = client.CreateChannel(knmessagingv1beta1.NewChannelBuilder(name).Build())
cb := knmessagingv1beta1.NewChannelBuilder(name)

if cmd.Flag("type").Changed {
gvk, err := processType(cmd.Flag("type").Value.String())
if err != nil {
return err
}
cb.Type(*gvk)
}

err = client.CreateChannel(cb.Build())
if err != nil {
return knerrors.GetError(err)
}
Expand All @@ -59,5 +78,14 @@ func NewChannelCreateCommand(p *commands.KnParams) *cobra.Command {
},
}
commands.AddNamespaceFlags(cmd.Flags(), false)
cmd.Flags().String("type", "", "Type of the channel to create in the format 'Group:Version:Kind'. For example: '--type messaging.knative.dev:v1alpha1:KafkaChannel'. Default is to use messaging layer setting for default channel configuration.")
return cmd
}

func processType(t string) (*schema.GroupVersionKind, error) {
parts := strings.Split(t, ":")
if len(parts) != 3 || parts[0] == "" || parts[1] == "" || parts[2] == "" {
return nil, errors.New(fmt.Sprintf("Error: incorrect value '%s' for '--type', must be in the format 'Group:Version:Kind'", t))
}
return &schema.GroupVersionKind{Group: parts[0], Version: parts[1], Kind: parts[2]}, nil
}
10 changes: 10 additions & 0 deletions pkg/messaging/v1beta1/messaging_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v1beta1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/eventing/pkg/apis/messaging/v1beta1"
clientv1beta1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/messaging/v1beta1"

Expand Down Expand Up @@ -136,6 +137,15 @@ func NewChannelBuilder(name string) *ChannelBuilder {
}}
}

// Type sets the type of the channel to create
func (c *ChannelBuilder) Type(gvk schema.GroupVersionKind) *v1beta1.Channel {
spec := &v1beta1.ChannelTemplateSpec{}
spec.Kind = gvk.Kind
spec.APIVersion = gvk.GroupVersion().String()
c.channel.Spec.ChannelTemplate = spec
return c.channel
}

// Build returns the Channel object from the builder
func (c *ChannelBuilder) Build() *v1beta1.Channel {
return c.channel
Expand Down

0 comments on commit f119bed

Please sign in to comment.