This is an automated email from the ASF dual-hosted git repository. astefanutti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push: new 1eb8dd5 fix: Fix cross-namespace creator resource events 1eb8dd5 is described below commit 1eb8dd56dd45126391b8bc71e83a0ea062b68037 Author: Antonin Stefanutti <anto...@stefanutti.fr> AuthorDate: Wed Aug 4 17:22:11 2021 +0200 fix: Fix cross-namespace creator resource events --- pkg/controller/integration/build_kit.go | 14 +++++++------- pkg/event/manager.go | 28 +++++++++++++++------------- pkg/trait/container.go | 11 ++++++----- pkg/util/kubernetes/camel.go | 22 ++++++++++++++-------- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/pkg/controller/integration/build_kit.go b/pkg/controller/integration/build_kit.go index 30a629f..005ed45 100644 --- a/pkg/controller/integration/build_kit.go +++ b/pkg/controller/integration/build_kit.go @@ -122,13 +122,13 @@ func (action *buildKitAction) Handle(ctx context.Context, integration *v1.Integr // Add some information for post-processing, this may need to be refactored // to a proper data structure platformKit.Labels = map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, - "camel.apache.org/created.by.kind": v1.IntegrationKind, - "camel.apache.org/created.by.name": integration.Name, - "camel.apache.org/created.by.namespace": integration.Namespace, - "camel.apache.org/created.by.version": integration.ResourceVersion, - "camel.apache.org/runtime.version": integration.Status.RuntimeVersion, - "camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider), + "camel.apache.org/kit.type": v1.IntegrationKitTypePlatform, + "camel.apache.org/runtime.version": integration.Status.RuntimeVersion, + "camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider), + kubernetes.CamelCreatorLabelKind: v1.IntegrationKind, + kubernetes.CamelCreatorLabelName: integration.Name, + kubernetes.CamelCreatorLabelNamespace: integration.Namespace, + kubernetes.CamelCreatorLabelVersion: integration.ResourceVersion, } // Set the kit to have the same characteristics as the integrations diff --git a/pkg/event/manager.go b/pkg/event/manager.go index 6278ba8..c8362d2 100644 --- a/pkg/event/manager.go +++ b/pkg/event/manager.go @@ -21,15 +21,17 @@ import ( "context" "fmt" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" + + ctrl "sigs.k8s.io/controller-runtime/pkg/client" + 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/client" "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/apache/camel-k/pkg/util/log" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/tools/record" - runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" ) const ( @@ -260,17 +262,21 @@ func NotifyBuildError(ctx context.Context, c client.Client, recorder record.Even } // nolint:lll -func notifyIfPhaseUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, new runtime.Object, oldPhase, newPhase string, resourceType, name, reason, info string) { +func notifyIfPhaseUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, new ctrl.Object, oldPhase, newPhase string, resourceType, name, reason, info string) { // Update information about phase changes if oldPhase != newPhase { phase := newPhase if phase == "" { phase = "[none]" } - recorder.Eventf(new, corev1.EventTypeNormal, reason, "%s %s in phase %q%s", resourceType, name, phase, info) + recorder.Eventf(new, corev1.EventTypeNormal, reason, "%s %q in phase %q%s", resourceType, name, phase, info) if creatorRef, creator := getCreatorObject(ctx, c, new); creatorRef != nil && creator != nil { - recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s %s subresource %s (%s) changed phase to %q%s", creatorRef.Kind, creatorRef.Name, name, resourceType, phase, info) + if namespace := new.GetNamespace(); namespace == creatorRef.Namespace { + recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s %q, created by %s %q, changed phase to %q%s", resourceType, name, creatorRef.Kind, creatorRef.Name, phase, info) + } else { + recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s \"%s/%s\", created by %s %q, changed phase to %q%s", resourceType, namespace, name, creatorRef.Kind, creatorRef.Name, phase, info) + } } } } @@ -305,12 +311,8 @@ func getCreatorObject(ctx context.Context, c client.Client, obj runtime.Object) if ref := kubernetes.GetCamelCreator(obj); ref != nil { if ref.Kind == "Integration" { it := v1.NewIntegration(ref.Namespace, ref.Name) - key := runtimeclient.ObjectKey{ - Namespace: ref.Namespace, - Name: ref.Name, - } - if err := c.Get(ctx, key, &it); err != nil { - log.Infof("Cannot get information about the Integration creating resource %v: %v", ref, err) + if err := c.Get(ctx, ctrl.ObjectKeyFromObject(&it), &it); err != nil { + log.Infof("Cannot get information about the creator Integration %v: %v", ref, err) return nil, nil } return ref, &it diff --git a/pkg/trait/container.go b/pkg/trait/container.go index c06cb2e..c8df0fe 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -22,6 +22,7 @@ import ( "path" "sort" + "github.com/apache/camel-k/pkg/util/kubernetes" appsv1 "k8s.io/api/apps/v1" "k8s.io/api/batch/v1beta1" corev1 "k8s.io/api/core/v1" @@ -181,11 +182,11 @@ func (t *containerTrait) configureDependencies(e *Environment) error { // Add some information for post-processing, this may need to be refactored // to a proper data structure kit.Labels = map[string]string{ - "camel.apache.org/kit.type": v1.IntegrationKitTypeExternal, - "camel.apache.org/created.by.kind": v1.IntegrationKind, - "camel.apache.org/created.by.name": e.Integration.Name, - "camel.apache.org/created.by.namespace": e.Integration.Namespace, - "camel.apache.org/created.by.version": e.Integration.ResourceVersion, + "camel.apache.org/kit.type": v1.IntegrationKitTypeExternal, + kubernetes.CamelCreatorLabelKind: v1.IntegrationKind, + kubernetes.CamelCreatorLabelName: e.Integration.Name, + kubernetes.CamelCreatorLabelNamespace: e.Integration.Namespace, + kubernetes.CamelCreatorLabelVersion: e.Integration.ResourceVersion, } t.L.Infof("image %s", kit.Spec.Image) diff --git a/pkg/util/kubernetes/camel.go b/pkg/util/kubernetes/camel.go index ca487b5..5cc7e1e 100644 --- a/pkg/util/kubernetes/camel.go +++ b/pkg/util/kubernetes/camel.go @@ -20,18 +20,20 @@ package kubernetes import ( "strings" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" ) const ( CamelCreatorLabelPrefix = "camel.apache.org/created.by" - CamelCreatorLabelKind = CamelCreatorLabelPrefix + ".kind" - CamelCreatorLabelName = CamelCreatorLabelPrefix + ".name" + CamelCreatorLabelKind = CamelCreatorLabelPrefix + ".kind" + CamelCreatorLabelName = CamelCreatorLabelPrefix + ".name" + CamelCreatorLabelNamespace = CamelCreatorLabelPrefix + ".namespace" + CamelCreatorLabelVersion = CamelCreatorLabelPrefix + ".version" ) // FilterCamelCreatorLabels is used to inherit the creator information among resources @@ -57,16 +59,20 @@ func MergeCamelCreatorLabels(source map[string]string, target map[string]string) } // GetCamelCreator returns the Camel creator object referenced by this runtime object, if present -func GetCamelCreator(obj runtime.Object) *v1.ObjectReference { +func GetCamelCreator(obj runtime.Object) *corev1.ObjectReference { if m, ok := obj.(metav1.Object); ok { kind := m.GetLabels()[CamelCreatorLabelKind] name := m.GetLabels()[CamelCreatorLabelName] + namespace, ok := m.GetLabels()[CamelCreatorLabelNamespace] + if !ok { + namespace = m.GetNamespace() + } if kind != "" && name != "" { - return &v1.ObjectReference{ + return &corev1.ObjectReference{ Kind: kind, - Namespace: m.GetNamespace(), + Namespace: namespace, Name: name, - APIVersion: camelv1.SchemeGroupVersion.String(), + APIVersion: v1.SchemeGroupVersion.String(), } } }