You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2021/08/26 14:30:03 UTC

[camel-k] branch main updated (45bcecb -> d039af5)

This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git.


    from 45bcecb  fix(jolokia): exclude com.sun:tools for broken jolokia-jvm pom
     new 00b830d  feat(cmd/bind): error-handler setting
     new d039af5  feat(cmd/bind): error-handler unit test

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pkg/cmd/bind.go      | 78 ++++++++++++++++++++++++++++++++++++++++++++++------
 pkg/cmd/bind_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 145 insertions(+), 9 deletions(-)

[camel-k] 02/02: feat(cmd/bind): error-handler unit test

Posted by pc...@apache.org.
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 d039af51396859e1fd97e15545b762ce4bd8ab24
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Mon Aug 23 15:40:07 2021 +0200

    feat(cmd/bind): error-handler unit test
    
    Closes #2494
---
 pkg/cmd/bind_test.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/pkg/cmd/bind_test.go b/pkg/cmd/bind_test.go
index 8467d31..d5682b4 100644
--- a/pkg/cmd/bind_test.go
+++ b/pkg/cmd/bind_test.go
@@ -82,3 +82,79 @@ func TestBindOutputUnknownFormat(t *testing.T) {
 
 	assert.Equal(t, "invalid output format option 'fail', should be one of: yaml|json\n", output)
 }
+
+func TestBindErrorHandlerDLCKamelet(t *testing.T) {
+	buildCmdOptions, bindCmd, _ := initializeBindCmdOptions(t)
+	output, err := test.ExecuteCommand(bindCmd, cmdBind, "my:src", "my:dst", "-o", "yaml",
+		"--error-handler", "dlc:my-kamelet", "-p", "error-handler.my-prop=value")
+	assert.Equal(t, "yaml", buildCmdOptions.OutputFormat)
+
+	assert.Nil(t, err)
+	assert.Equal(t, `apiVersion: camel.apache.org/v1alpha1
+kind: KameletBinding
+metadata:
+  creationTimestamp: null
+  name: my-to-my
+spec:
+  errorHandler:
+    dead-letter-channel:
+      endpoint:
+        properties:
+          my-prop: value
+        ref:
+          apiVersion: camel.apache.org/v1alpha1
+          kind: Kamelet
+          name: my-kamelet
+  sink:
+    uri: my:dst
+  source:
+    uri: my:src
+status: {}
+`, output)
+}
+
+func TestBindErrorHandlerNone(t *testing.T) {
+	buildCmdOptions, bindCmd, _ := initializeBindCmdOptions(t)
+	output, err := test.ExecuteCommand(bindCmd, cmdBind, "my:src", "my:dst", "-o", "yaml",
+		"--error-handler", "none")
+	assert.Equal(t, "yaml", buildCmdOptions.OutputFormat)
+
+	assert.Nil(t, err)
+	assert.Equal(t, `apiVersion: camel.apache.org/v1alpha1
+kind: KameletBinding
+metadata:
+  creationTimestamp: null
+  name: my-to-my
+spec:
+  errorHandler:
+    none: null
+  sink:
+    uri: my:dst
+  source:
+    uri: my:src
+status: {}
+`, output)
+}
+
+func TestBindErrorHandlerRef(t *testing.T) {
+	buildCmdOptions, bindCmd, _ := initializeBindCmdOptions(t)
+	output, err := test.ExecuteCommand(bindCmd, cmdBind, "my:src", "my:dst", "-o", "yaml",
+		"--error-handler", "ref:my-registry-reference")
+	assert.Equal(t, "yaml", buildCmdOptions.OutputFormat)
+
+	assert.Nil(t, err)
+	assert.Equal(t, `apiVersion: camel.apache.org/v1alpha1
+kind: KameletBinding
+metadata:
+  creationTimestamp: null
+  name: my-to-my
+spec:
+  errorHandler:
+    ref: my-registry-reference
+  sink:
+    uri: my:dst
+  source:
+    uri: my:src
+status: {}
+`, output)
+}

[camel-k] 01/02: feat(cmd/bind): error-handler setting

Posted by pc...@apache.org.
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 <pa...@gmail.com>
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
 }