You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2021/04/29 13:12:22 UTC

[camel-k] 08/30: refactor(trait): integration error handler spec

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

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

commit 34c82df0eea0b75e4dece804604732eec13d50ef
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Wed Apr 21 10:04:02 2021 +0200

    refactor(trait): integration error handler spec
---
 pkg/apis/camel/v1/common_types.go       | 12 +++++++
 pkg/apis/camel/v1/integration_types.go  |  9 ++---
 pkg/controller/kameletbinding/common.go | 14 ++++----
 pkg/trait/error_handler.go              | 59 +++++----------------------------
 pkg/trait/kamelets.go                   | 19 +++++------
 5 files changed, 39 insertions(+), 74 deletions(-)

diff --git a/pkg/apis/camel/v1/common_types.go b/pkg/apis/camel/v1/common_types.go
index e5b327b..e1b27e1 100644
--- a/pkg/apis/camel/v1/common_types.go
+++ b/pkg/apis/camel/v1/common_types.go
@@ -162,6 +162,18 @@ type Flow struct {
 	RawMessage `json:",inline"`
 }
 
+// ErrorHandlerSpec respresents an integration error handler to be used as default at runtime
+type ErrorHandlerSpec struct {
+	Type          string                     `json:"type,omitempty"`
+	URI           string                     `json:"uri,omitempty"`
+	Configuration *ErrorHandlerConfiguration `json:"configuration,omitempty"`
+}
+
+// ErrorHandlerConfiguration --
+type ErrorHandlerConfiguration struct {
+	RawMessage `json:",inline"`
+}
+
 // RuntimeProvider --
 type RuntimeProvider string
 
diff --git a/pkg/apis/camel/v1/integration_types.go b/pkg/apis/camel/v1/integration_types.go
index 6ba7e10..500291c 100644
--- a/pkg/apis/camel/v1/integration_types.go
+++ b/pkg/apis/camel/v1/integration_types.go
@@ -27,10 +27,11 @@ import (
 
 // IntegrationSpec defines the desired state of Integration
 type IntegrationSpec struct {
-	Replicas  *int32         `json:"replicas,omitempty"`
-	Sources   []SourceSpec   `json:"sources,omitempty"`
-	Flows     []Flow         `json:"flows,omitempty"`
-	Resources []ResourceSpec `json:"resources,omitempty"`
+	Replicas     *int32           `json:"replicas,omitempty"`
+	Sources      []SourceSpec     `json:"sources,omitempty"`
+	ErrorHandler ErrorHandlerSpec `json:"errorHandler,omitempty"`
+	Flows        []Flow           `json:"flows,omitempty"`
+	Resources    []ResourceSpec   `json:"resources,omitempty"`
 	// Deprecated: use the IntegrationKit field
 	Kit                string                  `json:"kit,omitempty"`
 	IntegrationKit     *corev1.ObjectReference `json:"integrationKit,omitempty"`
diff --git a/pkg/controller/kameletbinding/common.go b/pkg/controller/kameletbinding/common.go
index a63780f..13e13fe 100644
--- a/pkg/controller/kameletbinding/common.go
+++ b/pkg/controller/kameletbinding/common.go
@@ -86,9 +86,9 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
 			return nil, errors.Wrap(err, "could not determine error handler URI")
 		}
 
-		err = setErrorHandlerKamelet(errorHandler, kameletbinding.Spec.ErrorHandler)
+		err = setIntegrationErrorHandler(&it.Spec, errorHandler, kameletbinding.Spec.ErrorHandler)
 		if err != nil {
-			return nil, errors.Wrap(err, "could not set error handler")
+			return nil, errors.Wrap(err, "could not set integration error handler")
 		}
 	}
 
@@ -159,14 +159,12 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
 	return &it, nil
 }
 
-func setErrorHandlerKamelet(errorHandler *bindings.Binding, errorHandlerSpec v1alpha1.ErrorHandler) error {
-	if errorHandler.ApplicationProperties == nil {
-		errorHandler.ApplicationProperties = make(map[string]string)
+func setIntegrationErrorHandler(it *v1.IntegrationSpec, errorHandler *bindings.Binding, errorHandlerSpec v1alpha1.ErrorHandler) error {
+	it.ErrorHandler = v1.ErrorHandlerSpec{
+		URI:  errorHandler.URI,
+		Type: string(errorHandlerSpec.Type),
 	}
 
-	errorHandler.ApplicationProperties["camel.k.default-error-handler.uri"] = errorHandler.URI
-	errorHandler.ApplicationProperties["camel.k.default-error-handler.type"] = string(errorHandlerSpec.Type)
-
 	return nil
 }
 
diff --git a/pkg/trait/error_handler.go b/pkg/trait/error_handler.go
index 7101d9e..228ed12 100644
--- a/pkg/trait/error_handler.go
+++ b/pkg/trait/error_handler.go
@@ -19,10 +19,8 @@ package trait
 
 import (
 	"fmt"
-	"strings"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
-	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 )
 
 // The error handler is a platform trait used to inject Error Handler source into the integration runtime.
@@ -30,12 +28,7 @@ import (
 // +camel-k:trait=error-handler
 type errorHandlerTrait struct {
 	BaseTrait `property:",squash"`
-	// Automatically inject all referenced Kamelets and their default configuration (enabled by default)
-	Auto *bool `property:"auto"`
-
-	// TODO move into a struct
-	ErrorHandlerURI  string
-	ErrorHandlerType string
+	Auto      *bool `property:"auto"`
 }
 
 func newErrorHandlerTrait() Trait {
@@ -58,50 +51,14 @@ func (t *errorHandlerTrait) Configure(e *Environment) (bool, error) {
 		return false, nil
 	}
 
-	if t.Auto == nil || *t.Auto {
-		if t.ErrorHandlerType == "" {
-			t.ErrorHandlerType = maybeErrorHandler(e.Integration.Configurations())
-			if t.ErrorHandlerType != "" && v1alpha1.ErrorHandlerType(t.ErrorHandlerType) == v1alpha1.ErrorHandlerTypeDeadLetterChannel {
-				t.ErrorHandlerURI = maybeKameletAsDefaultErrorHandler(e.Integration.Configurations())
-			}
-		}
-	}
-
-	return t.ErrorHandlerType != "", nil
-}
-
-func maybeErrorHandler(properties []v1.ConfigurationSpec) string {
-	for _, property := range properties {
-		if strings.HasPrefix(property.Value, "camel.k.default-error-handler.type=") {
-			split := strings.Split(property.Value, "=")
-			if len(split) > 0 {
-				return split[1]
-			}
-		}
-	}
-
-	return ""
-}
-
-func maybeKameletAsDefaultErrorHandler(properties []v1.ConfigurationSpec) string {
-	for _, property := range properties {
-		if strings.HasPrefix(property.Value, "camel.k.default-error-handler.uri=") {
-			split := strings.Split(property.Value, "=")
-			if len(split) > 0 {
-				return split[1]
-			}
-		}
-	}
-
-	return ""
+	return e.Integration.Spec.ErrorHandler.Type != "", nil
 }
 
 func (t *errorHandlerTrait) Apply(e *Environment) error {
-
 	if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
-		if t.ErrorHandlerType != "" {
+		if e.Integration.Spec.ErrorHandler.Type != "" {
 			// Possible error handler
-			err := addErrorHandlerAsSource(e, t.ErrorHandlerURI, t.ErrorHandlerType)
+			err := addErrorHandlerAsSource(e)
 			if err != nil {
 				return err
 			}
@@ -110,8 +67,8 @@ func (t *errorHandlerTrait) Apply(e *Environment) error {
 	return nil
 }
 
-func addErrorHandlerAsSource(e *Environment, errorHandlerURI string, errorHandlerType string) error {
-	errorHandlerStatement, err := parseErrorHandler(errorHandlerURI, errorHandlerType)
+func addErrorHandlerAsSource(e *Environment) error {
+	errorHandlerStatement, err := parseErrorHandler(e.Integration.Spec.ErrorHandler.URI, e.Integration.Spec.ErrorHandler.Type)
 	if err != nil {
 		return err
 	}
@@ -148,14 +105,14 @@ func addErrorHandlerAsSource(e *Environment, errorHandlerURI string, errorHandle
 	return nil
 }
 
-func parseErrorHandler(errHandlUri string, errHandlType string) (string, error) {
+func parseErrorHandler(errHandlURI string, errHandlType string) (string, error) {
 	switch errHandlType {
 	case "no":
 		return `errorHandler(noErrorHandler());`, nil
 	case "default":
 		return `errorHandler(defaultErrorHandler());`, nil
 	case "dead-letter-channel":
-		return fmt.Sprintf(`errorHandler(deadLetterChannel("%v"));`, errHandlUri), nil
+		return fmt.Sprintf(`errorHandler(deadLetterChannel("%v"));`, errHandlURI), nil
 	}
 
 	return "", fmt.Errorf("Cannot recognize any error handler of type %s", errHandlType)
diff --git a/pkg/trait/kamelets.go b/pkg/trait/kamelets.go
index dd4c3bb..190c17e 100644
--- a/pkg/trait/kamelets.go
+++ b/pkg/trait/kamelets.go
@@ -47,10 +47,6 @@ type kameletsTrait struct {
 	Auto *bool `property:"auto"`
 	// Comma separated list of Kamelet names to load into the current integration
 	List string `property:"list"`
-
-	// TODO move into a struct
-	ErrorHandlerURI  string
-	ErrorHandlerType string
 }
 
 type configurationKey struct {
@@ -111,13 +107,10 @@ func (t *kameletsTrait) Configure(e *Environment) (bool, error) {
 			})
 			sort.Strings(kamelets)
 		}
-		if t.ErrorHandlerType == "" {
-			t.ErrorHandlerType = maybeErrorHandler(e.Integration.Configurations())
-			if t.ErrorHandlerType != "" && v1alpha1.ErrorHandlerType(t.ErrorHandlerType) == v1alpha1.ErrorHandlerTypeDeadLetterChannel {
-				t.ErrorHandlerURI = maybeKameletAsDefaultErrorHandler(e.Integration.Configurations())
-				if strings.HasPrefix(t.ErrorHandlerURI, "kamelet:") {
-					kamelets = append(kamelets, extractKamelet(t.ErrorHandlerURI))
-				}
+		// Check if a Kamelet is configured as default error handler
+		if e.Integration.Spec.ErrorHandler.URI != "" {
+			if strings.HasPrefix(e.Integration.Spec.ErrorHandler.URI, "kamelet:") {
+				kamelets = append(kamelets, extractKamelet(e.Integration.Spec.ErrorHandler.URI))
 			}
 		}
 
@@ -132,8 +125,12 @@ func (t *kameletsTrait) declaredKamelets() bool {
 }
 
 func (t *kameletsTrait) Apply(e *Environment) error {
+<<<<<<< HEAD
 
 	if e.IntegrationInPhase(v1.IntegrationPhaseInitialization, v1.IntegrationPhaseRunning) {
+=======
+	if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
+>>>>>>> refactor(trait): integration error handler spec
 		if err := t.addKamelets(e); err != nil {
 			return err
 		}