Skip to content
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

Fix #1747: propagate icon on kameletbinding and fix integration owner… #1749

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions pkg/controller/kameletbinding/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}