This is an automated email from the ASF dual-hosted git repository. pcongiusti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 00b830d7cf9ad49232700a2ae9fcb9da224d53bd Author: Pasquale Congiusti <[email protected]> AuthorDate: Thu Aug 19 12:34:15 2021 +0200 feat(cmd/bind): error-handler setting --- pkg/cmd/bind.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/bind.go b/pkg/cmd/bind.go index 352551d..de66d85 100644 --- a/pkg/cmd/bind.go +++ b/pkg/cmd/bind.go @@ -57,23 +57,26 @@ func newCmdBind(rootCmdOptions *RootCmdOptions) (*cobra.Command, *bindCmdOptions }, } + cmd.Flags().String("error-handler", "", `Add error handler (none|log|dlc:<endpoint>|bean:<type>|ref:<registry-ref>). DLC endpoints are expected in the format "[[apigroup/]version:]kind:[namespace/]name", plain Camel URIs or Kamelet name.`) cmd.Flags().String("name", "", "Name for the binding") cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml") - cmd.Flags().StringArrayP("property", "p", nil, `Add a binding property in the form of "source.<key>=<value>", "sink.<key>=<value>" or "step-<n>.<key>=<value>"`) + cmd.Flags().StringArrayP("property", "p", nil, `Add a binding property in the form of "source.<key>=<value>", "sink.<key>=<value>", "error-handler.<key>=<value>" or "step-<n>.<key>=<value>"`) cmd.Flags().Bool("skip-checks", false, "Do not verify the binding for compliance with Kamelets and other Kubernetes resources") - cmd.Flags().StringArray("step", nil, `Add binding steps as Kubernetes resources, such as Kamelets. Endpoints are expected in the format "[[apigroup/]version:]kind:[namespace/]name" or plain Camel URIs.`) + cmd.Flags().StringArray("step", nil, `Add binding steps as Kubernetes resources. Endpoints are expected in the format "[[apigroup/]version:]kind:[namespace/]name", plain Camel URIs or Kamelet name.`) return &cmd, &options } const ( - sourceKey = "source" - sinkKey = "sink" - stepKeyPrefix = "step-" + sourceKey = "source" + sinkKey = "sink" + stepKeyPrefix = "step-" + errorHandlerKey = "error-handler" ) type bindCmdOptions struct { *RootCmdOptions + ErrorHandler string `mapstructure:"error-handler" yaml:",omitempty"` Name string `mapstructure:"name" yaml:",omitempty"` OutputFormat string `mapstructure:"output" yaml:",omitempty"` Properties []string `mapstructure:"properties" yaml:",omitempty"` @@ -150,6 +153,14 @@ func (o *bindCmdOptions) run(cmd *cobra.Command, args []string) error { }, } + if o.ErrorHandler != "" { + if errorHandler, err := o.parseErrorHandler(); err == nil { + binding.Spec.ErrorHandler = errorHandler + } else { + return err + } + } + if len(o.Steps) > 0 { binding.Spec.Steps = make([]v1alpha1.Endpoint, 0) for idx, stepDesc := range o.Steps { @@ -197,6 +208,54 @@ func showOutput(cmd *cobra.Command, binding *v1alpha1.KameletBinding, outputForm return printer.PrintObj(binding, cmd.OutOrStdout()) } +func (o *bindCmdOptions) parseErrorHandler() (*v1alpha1.ErrorHandlerSpec, error) { + var errHandlMap = make(map[string]interface{}) + errHandlType, errHandlValue, err := parseErrorHandlerByType(o.ErrorHandler) + if err != nil { + return nil, err + } + switch errHandlType { + case "none": + errHandlMap["none"] = nil + case "log": + errHandlMap["log"] = nil + case "dlc": + dlcSpec, err := o.decode(errHandlValue, errorHandlerKey) + if err != nil { + return nil, err + } + errHandlMap["dead-letter-channel"] = map[string]interface{}{ + "endpoint": dlcSpec, + } + case "bean": + errHandlMap["bean"] = map[string]interface{}{ + "type": errHandlValue, + } + case "ref": + errHandlMap["ref"] = errHandlValue + default: + return nil, fmt.Errorf("invalid error handler type %s", o.ErrorHandler) + } + errHandlMarshalled, err := json.Marshal(&errHandlMap) + if err != nil { + return nil, err + } + return &v1alpha1.ErrorHandlerSpec{RawMessage: errHandlMarshalled}, nil +} + +func parseErrorHandlerByType(value string) (string, string, error) { + errHandlSplit := strings.SplitN(value, ":", 2) + if (errHandlSplit[0] == "dlc" || errHandlSplit[0] == "bean" || errHandlSplit[0] == "ref") && + len(errHandlSplit) != 2 { + return "", "", fmt.Errorf("invalid error handler syntax. Type %s needs a configuration (ie %s:value)", + errHandlSplit[0], errHandlSplit[0]) + } + if len(errHandlSplit) > 1 { + return errHandlSplit[0], errHandlSplit[1], nil + } + return errHandlSplit[0], "", nil +} + func (o *bindCmdOptions) decode(res string, key string) (v1alpha1.Endpoint, error) { refConverter := reference.NewConverter(reference.KameletPrefix) endpoint := v1alpha1.Endpoint{} @@ -292,17 +351,18 @@ func (o *bindCmdOptions) getProperties(refType string) map[string]string { func (o *bindCmdOptions) parseProperty(prop string) (string, string, string, error) { parts := strings.SplitN(prop, "=", 2) if len(parts) != 2 { - return "", "", "", fmt.Errorf(`property %q does not follow format "[source|sink|step-<n>].<key>=<value>"`, prop) + return "", "", "", fmt.Errorf(`property %q does not follow format "[source|sink|error-handler|step-<n>].<key>=<value>"`, prop) } keyParts := strings.SplitN(parts[0], ".", 2) if len(keyParts) != 2 { - return "", "", "", fmt.Errorf(`property key %q does not follow format "[source|sink|step-<n>].<key>"`, parts[0]) + return "", "", "", fmt.Errorf(`property key %q does not follow format "[source|sink|error-handler|step-<n>].<key>"`, parts[0]) } isSource := keyParts[0] == sourceKey isSink := keyParts[0] == sinkKey + isErrorHandler := keyParts[0] == errorHandlerKey isStep := strings.HasPrefix(keyParts[0], stepKeyPrefix) - if !isSource && !isSink && !isStep { - return "", "", "", fmt.Errorf(`property key %q does not start with "source.", "sink." or "step-<n>."`, parts[0]) + if !isSource && !isSink && !isStep && !isErrorHandler { + return "", "", "", fmt.Errorf(`property key %q does not start with "source.", "sink.", "error-handler." or "step-<n>."`, parts[0]) } return keyParts[0], keyParts[1], parts[1], nil }
