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

Added --broker-config flag to broker create command #1700

Merged
merged 4 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions docs/cmd/kn_broker_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kn broker create NAME
```
--backoff-delay string The delay before retrying.
--backoff-policy string The retry backoff policy (linear, exponential).
--broker-config string Broker config object like ConfigMap or RabbitMQ
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow this statement is missing something "Broker config object like ConfigMap or RabbitMQ" can we be more descriptive. Also is "or" the right conjunctive here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the flag description in my latest commit. Please let me know if it can be improved further

--class string Broker class like 'MTChannelBasedBroker' or 'Kafka' (if available).
--dl-sink string The sink receiving event that could not be sent to a destination.
-h, --help help for create
Expand Down
7 changes: 7 additions & 0 deletions pkg/eventing/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ func (b *BrokerBuilder) RetryAfterMax(max *string) *BrokerBuilder {

}

// Config for the broker builder
func (b *BrokerBuilder) Config(config *duckv1.KReference) *BrokerBuilder {
b.broker.Spec.Config = config
return b

}

// Build to return an instance of broker object
func (b *BrokerBuilder) Build() *eventingv1.Broker {
return b.broker
Expand Down
115 changes: 115 additions & 0 deletions pkg/kn/commands/broker/config_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright © 2022 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 broker

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"knative.dev/client/pkg/util"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

type ConfigType int
vyasgun marked this conversation as resolved.
Show resolved Hide resolved

// Known config types for broker
const (
ConfigMapType ConfigType = iota
SecretType
RabbitMqType
)

var (
KReferenceMapping = map[ConfigType]*duckv1.KReference{
vyasgun marked this conversation as resolved.
Show resolved Hide resolved
ConfigMapType: {Kind: "ConfigMap", APIVersion: "v1"},
SecretType: {Kind: "Secret", APIVersion: "v1"},
RabbitMqType: {Kind: "RabbitmqCluster", APIVersion: "rabbitmq.com/v1beta1"},
}
)

type ConfigFlags struct {
vyasgun marked this conversation as resolved.
Show resolved Hide resolved
BrokerConfig string
}

func (c *ConfigFlags) Add(cmd *cobra.Command) {
vyasgun marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().StringVar(&c.BrokerConfig, "broker-config", "", "Broker config object like ConfigMap or RabbitMQ")
}

func (c *ConfigFlags) GetBrokerConfigReference() (*duckv1.KReference, error) {
vyasgun marked this conversation as resolved.
Show resolved Hide resolved
config := c.BrokerConfig
slices := strings.SplitN(config, ":", 3)
if len(slices) == 1 {
return &duckv1.KReference{
Kind: "ConfigMap",
Name: slices[0],
APIVersion: "v1",
}, nil
} else if len(slices) == 2 {
kind := slices[0]
name := slices[1]
kRef := getDefaultKReference(kind)
if kRef.APIVersion == "" {
return nil, fmt.Errorf("kind %q is unknown and APIVersion could not be determined", kind)
}
kRef.Name = name
return kRef, nil
} else {
kind := slices[0]
name := slices[1]
kRef := getDefaultKReference(kind)
kRef.Name = name

params := strings.Split(slices[2], ",")
if len(params) == 1 && !strings.Contains(params[0], ",") {
kRef.Namespace = params[0]
return kRef, nil
}
mappedOptions, err := util.MapFromArray(params, "=")
if err != nil {
return nil, err
}
for k, v := range mappedOptions {
switch strings.ToLower(k) {
case "namespace":
kRef.Namespace = v
case "group":
kRef.Group = v
case "apiversion":
kRef.APIVersion = v
default:
return nil, fmt.Errorf("incorrect field %q. Please specify any of the following: Namespace, Group, APIVersion", k)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since field test is on lower case names perhaps the error message should use lower case in message too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument is being converted to lower case before the check so the user can provide it in any case. I think the error message looks clearer with capitalisation.

}
}
if kRef.APIVersion == "" {
return nil, fmt.Errorf("kind %q is unknown and APIVersion could not be determined", kind)
}
return kRef, nil
}
}

func getDefaultKReference(kind string) *duckv1.KReference {
k := strings.ToLower(kind)
switch k {
case "cm", "configmap":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we make that switch more dynamic ? I.e. already add the list of shortcut prefixes to KReferenceMapping and then either iterate of the list to find the entry or build up a map before with the shortcut prefix as key, pointing to the KReferenceMapping entry (i.e. I would create an own Struct for the mapping with a KReference and a string array as member for KRerferenceMapping.

The advantage would be that all the relevant parts (reference + prefixes) are defined in a single place (i.e. where KReferenceMapping is defined)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sounds good. I'll do that.

return KReferenceMapping[ConfigMapType]
case "sc", "secret":
return KReferenceMapping[SecretType]
case "rmq", "rabbitmq", "rabbitmqcluster":
return KReferenceMapping[RabbitMqType]
default:
return &duckv1.KReference{Kind: kind}
}
}
19 changes: 18 additions & 1 deletion pkg/kn/commands/broker/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/spf13/cobra"
v1 "knative.dev/eventing/pkg/apis/duck/v1"
duckv1 "knative.dev/pkg/apis/duck/v1"

clientv1beta1 "knative.dev/client/pkg/eventing/v1"
"knative.dev/client/pkg/kn/commands"
Expand All @@ -41,6 +42,7 @@ func NewBrokerCreateCommand(p *commands.KnParams) *cobra.Command {
var className string

var deliveryFlags DeliveryOptionFlags
var configFlags ConfigFlags
cmd := &cobra.Command{
Use: "create NAME",
Short: "Create a broker",
Expand Down Expand Up @@ -73,6 +75,19 @@ func NewBrokerCreateCommand(p *commands.KnParams) *cobra.Command {

backoffPolicy := v1.BackoffPolicyType(deliveryFlags.BackoffPolicy)

var configReference *duckv1.KReference

if cmd.Flags().Changed("broker-config") {
if !cmd.Flags().Changed("class") {
return fmt.Errorf("cannot set broker-config without setting class")
}

configReference, err = configFlags.GetBrokerConfigReference()
if err != nil {
return err
}
}

brokerBuilder := clientv1beta1.
NewBrokerBuilder(name).
Namespace(namespace).
Expand All @@ -82,7 +97,8 @@ func NewBrokerCreateCommand(p *commands.KnParams) *cobra.Command {
Timeout(&deliveryFlags.Timeout).
BackoffPolicy(&backoffPolicy).
BackoffDelay(&deliveryFlags.BackoffDelay).
RetryAfterMax(&deliveryFlags.RetryAfterMax)
RetryAfterMax(&deliveryFlags.RetryAfterMax).
Config(configReference)

err = eventingClient.CreateBroker(cmd.Context(), brokerBuilder.Build())
if err != nil {
Expand All @@ -96,6 +112,7 @@ func NewBrokerCreateCommand(p *commands.KnParams) *cobra.Command {
}
commands.AddNamespaceFlags(cmd.Flags(), false)
cmd.Flags().StringVar(&className, "class", "", "Broker class like 'MTChannelBasedBroker' or 'Kafka' (if available).")
configFlags.Add(cmd)
deliveryFlags.Add(cmd)
return cmd
}