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 2020/10/07 07:54:37 UTC

[camel-k] branch master updated: Fix #1747: propagate icon on kameletbinding and fix integration owner references

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

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


The following commit(s) were added to refs/heads/master by this push:
     new cd22ebf  Fix #1747: propagate icon on kameletbinding and fix integration owner references
cd22ebf is described below

commit cd22ebfb77e3b9c64ad6907fd4f43384b00f8f52
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Tue Oct 6 16:20:59 2020 +0200

    Fix #1747: propagate icon on kameletbinding and fix integration owner references
---
 pkg/controller/kameletbinding/initialize.go | 74 +++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

diff --git a/pkg/controller/kameletbinding/initialize.go b/pkg/controller/kameletbinding/initialize.go
index 2afefcc..d3874d6 100644
--- a/pkg/controller/kameletbinding/initialize.go
+++ b/pkg/controller/kameletbinding/initialize.go
@@ -20,13 +20,18 @@ package kameletbinding
 import (
 	"context"
 	"encoding/json"
+	"strings"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/util/bindings"
 	"github.com/apache/camel-k/pkg/util/kubernetes"
+	"github.com/apache/camel-k/pkg/util/patch"
 	"github.com/pkg/errors"
+	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/types"
+	"sigs.k8s.io/controller-runtime/pkg/client"
 )
 
 // NewInitializeAction returns a action that initializes the kamelet binding configuration when not provided by the user
@@ -47,10 +52,22 @@ func (action *initializeAction) CanHandle(kameletbinding *v1alpha1.KameletBindin
 }
 
 func (action *initializeAction) Handle(ctx context.Context, kameletbinding *v1alpha1.KameletBinding) (*v1alpha1.KameletBinding, error) {
+	controller := true
+	blockOwnerDeletion := true
 	it := v1.Integration{
 		ObjectMeta: metav1.ObjectMeta{
 			Namespace: kameletbinding.Namespace,
 			Name:      kameletbinding.Name,
+			OwnerReferences: []metav1.OwnerReference{
+				{
+					APIVersion:         kameletbinding.APIVersion,
+					Kind:               kameletbinding.Kind,
+					Name:               kameletbinding.Name,
+					UID:                kameletbinding.UID,
+					Controller:         &controller,
+					BlockOwnerDeletion: &blockOwnerDeletion,
+				},
+			},
 		},
 	}
 	// start from the integration spec defined in the binding
@@ -105,7 +122,64 @@ func (action *initializeAction) Handle(ctx context.Context, kameletbinding *v1al
 		return nil, errors.Wrap(err, "could not create integration for kamelet binding")
 	}
 
+	// propagate Kamelet icon (best effort)
+	action.propagateIcon(ctx, kameletbinding)
+
 	target := kameletbinding.DeepCopy()
 	target.Status.Phase = v1alpha1.KameletBindingPhaseCreating
 	return target, nil
 }
+
+func (action *initializeAction) propagateIcon(ctx context.Context, binding *v1alpha1.KameletBinding) {
+	icon, err := action.findIcon(ctx, binding)
+	if err != nil {
+		action.L.Errorf(err, "cannot find icon for kamelet binding %q", binding.Name)
+		return
+	}
+	if icon == "" {
+		return
+	}
+	// compute patch
+	clone := binding.DeepCopy()
+	clone.Annotations = make(map[string]string)
+	for k, v := range binding.Annotations {
+		clone.Annotations[k] = v
+	}
+	if _, ok := clone.Annotations[v1alpha1.AnnotationIcon]; !ok {
+		clone.Annotations[v1alpha1.AnnotationIcon] = icon
+	}
+	p, err := patch.PositiveMergePatch(binding, clone)
+	if err != nil {
+		action.L.Errorf(err, "cannot compute patch to update icon for kamelet binding %q", binding.Name)
+		return
+	}
+	if len(p) > 0 {
+		if err := action.client.Patch(ctx, clone, client.RawPatch(types.MergePatchType, p)); err != nil {
+			action.L.Errorf(err, "cannot apply merge patch to update icon for kamelet binding %q", binding.Name)
+			return
+		}
+	}
+}
+
+func (action *initializeAction) findIcon(ctx context.Context, binding *v1alpha1.KameletBinding) (string, error) {
+	var kameletRef *corev1.ObjectReference
+	if binding.Spec.Source.Ref != nil && binding.Spec.Source.Ref.Kind == "Kamelet" && strings.HasPrefix(binding.Spec.Source.Ref.APIVersion, "camel.apache.org/") {
+		kameletRef = binding.Spec.Source.Ref
+	} else if binding.Spec.Sink.Ref != nil && binding.Spec.Sink.Ref.Kind == "Kamelet" && strings.HasPrefix(binding.Spec.Sink.Ref.APIVersion, "camel.apache.org/") {
+		kameletRef = binding.Spec.Sink.Ref
+	}
+
+	if kameletRef == nil {
+		return "", nil
+	}
+
+	key := client.ObjectKey{
+		Namespace: binding.Namespace,
+		Name:      kameletRef.Name,
+	}
+	var kamelet v1alpha1.Kamelet
+	if err := action.client.Get(ctx, key, &kamelet); err != nil {
+		return "", err
+	}
+	return kamelet.Annotations[v1alpha1.AnnotationIcon], nil
+}