-
Notifications
You must be signed in to change notification settings - Fork 261
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 The advantage would be that all the relevant parts (reference + prefixes) are defined in a single place (i.e. where KReferenceMapping is defined) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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