This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit e110c429afd948b84040d8cc0bb02b4a8c35b11e Author: Nicola Ferraro <ni.ferr...@gmail.com> AuthorDate: Mon Jan 27 16:31:27 2020 +0100 #1199: multicasting events of dependent resources to integration --- deploy/platform-integration-kit-groovy.yaml | 4 +- deploy/platform-integration-kit-java.yaml | 4 +- deploy/platform-integration-kit-js.yaml | 4 +- deploy/platform-integration-kit-knative.yaml | 4 +- deploy/platform-integration-kit-kotlin.yaml | 4 +- deploy/platform-integration-kit-main.yaml | 4 +- deploy/platform-integration-kit-xml.yaml | 4 +- deploy/platform-integration-kit-yaml.yaml | 4 +- pkg/builder/s2i/publisher.go | 4 ++ pkg/controller/build/build_controller.go | 6 +- pkg/controller/build/schedule_pod.go | 3 + pkg/controller/integration/build_kit.go | 8 +-- .../integration/integration_controller.go | 6 +- pkg/controller/integrationkit/build.go | 1 + .../integrationkit/integrationkit_controller.go | 6 +- .../integrationplatform_controller.go | 6 +- pkg/events/manager.go | 56 ++++++++++++---- pkg/util/kubernetes/camel_labels.go | 74 ++++++++++++++++++++++ 18 files changed, 157 insertions(+), 45 deletions(-) diff --git a/deploy/platform-integration-kit-groovy.yaml b/deploy/platform-integration-kit-groovy.yaml index f8e40f3..b525c58 100644 --- a/deploy/platform-integration-kit-groovy.yaml +++ b/deploy/platform-integration-kit-groovy.yaml @@ -21,8 +21,8 @@ metadata: name: groovy labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-java.yaml b/deploy/platform-integration-kit-java.yaml index c9f9752..511ce4f 100644 --- a/deploy/platform-integration-kit-java.yaml +++ b/deploy/platform-integration-kit-java.yaml @@ -21,8 +21,8 @@ metadata: name: java labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-js.yaml b/deploy/platform-integration-kit-js.yaml index e8dbef2..6187905 100644 --- a/deploy/platform-integration-kit-js.yaml +++ b/deploy/platform-integration-kit-js.yaml @@ -21,8 +21,8 @@ metadata: name: js labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-knative.yaml b/deploy/platform-integration-kit-knative.yaml index 972fc8f..74fc176 100644 --- a/deploy/platform-integration-kit-knative.yaml +++ b/deploy/platform-integration-kit-knative.yaml @@ -21,8 +21,8 @@ metadata: name: knative labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-kotlin.yaml b/deploy/platform-integration-kit-kotlin.yaml index 1be76e4..44f5819 100644 --- a/deploy/platform-integration-kit-kotlin.yaml +++ b/deploy/platform-integration-kit-kotlin.yaml @@ -21,8 +21,8 @@ metadata: name: kotlin labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-main.yaml b/deploy/platform-integration-kit-main.yaml index 7a3a6cf..0b5cfb9 100644 --- a/deploy/platform-integration-kit-main.yaml +++ b/deploy/platform-integration-kit-main.yaml @@ -21,8 +21,8 @@ metadata: name: main labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-xml.yaml b/deploy/platform-integration-kit-xml.yaml index 7e31773..ab1de23 100644 --- a/deploy/platform-integration-kit-xml.yaml +++ b/deploy/platform-integration-kit-xml.yaml @@ -21,8 +21,8 @@ metadata: name: xml labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/deploy/platform-integration-kit-yaml.yaml b/deploy/platform-integration-kit-yaml.yaml index bf223a9..1b4b0f4 100644 --- a/deploy/platform-integration-kit-yaml.yaml +++ b/deploy/platform-integration-kit-yaml.yaml @@ -21,8 +21,8 @@ metadata: name: yaml labels: app: "camel-k" - camel.apache.org/kit.created.by.kind: Operator - camel.apache.org/kit.created.by.name: camel-k-operator + camel.apache.org/created.by.kind: Operator + camel.apache.org/created.by.name: camel-k-operator camel.apache.org/kit.type: platform spec: dependencies: diff --git a/pkg/builder/s2i/publisher.go b/pkg/builder/s2i/publisher.go index ae86de8..3165587 100644 --- a/pkg/builder/s2i/publisher.go +++ b/pkg/builder/s2i/publisher.go @@ -71,6 +71,8 @@ func publisher(ctx *builder.Context) error { }, } + bc.Labels = kubernetes.MergeCamelCreatorLabels(ctx.Build.Meta.Labels, bc.Labels) + err := ctx.Client.Delete(ctx.C, &bc) if err != nil && !apierrors.IsNotFound(err) { return errors.Wrap(err, "cannot delete build config") @@ -100,6 +102,8 @@ func publisher(ctx *builder.Context) error { }, } + is.Labels = kubernetes.MergeCamelCreatorLabels(ctx.Build.Meta.Labels, is.Labels) + err = ctx.Client.Delete(ctx.C, &is) if err != nil && !apierrors.IsNotFound(err) { return errors.Wrap(err, "cannot delete image stream") diff --git a/pkg/controller/build/build_controller.go b/pkg/controller/build/build_controller.go index ddf5da7..ecb2995 100644 --- a/pkg/controller/build/build_controller.go +++ b/pkg/controller/build/build_controller.go @@ -208,13 +208,13 @@ func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result, newTarget, err := a.Handle(ctx, target) if err != nil { - events.NotifyBuildError(r.recorder, &instance, newTarget, err) + events.NotifyBuildError(ctx, r.client, r.recorder, &instance, newTarget, err) return reconcile.Result{}, err } if newTarget != nil { if res, err := r.update(ctx, &instance, newTarget); err != nil { - events.NotifyBuildError(r.recorder, &instance, newTarget, err) + events.NotifyBuildError(ctx, r.client, r.recorder, &instance, newTarget, err) return res, err } @@ -231,7 +231,7 @@ func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result, // handle one action at time so the resource // is always at its latest state - events.NotifyBuildUpdated(r.recorder, &instance, newTarget) + events.NotifyBuildUpdated(ctx, r.client, r.recorder, &instance, newTarget) break } } diff --git a/pkg/controller/build/schedule_pod.go b/pkg/controller/build/schedule_pod.go index 08b773a..f26f92e 100644 --- a/pkg/controller/build/schedule_pod.go +++ b/pkg/controller/build/schedule_pod.go @@ -21,6 +21,7 @@ import ( "context" "sync" + "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" @@ -129,6 +130,8 @@ func (action *schedulePodAction) newBuildPod(ctx context.Context, build *v1.Buil }, } + pod.Labels = kubernetes.MergeCamelCreatorLabels(build.Labels, pod.Labels) + for _, task := range build.Spec.Tasks { if task.Builder != nil { // TODO: Move the retrieval of the operator image into the controller diff --git a/pkg/controller/integration/build_kit.go b/pkg/controller/integration/build_kit.go index e561c9f..61c8c3f 100644 --- a/pkg/controller/integration/build_kit.go +++ b/pkg/controller/integration/build_kit.go @@ -107,10 +107,10 @@ 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/kit.created.by.kind": v1.IntegrationKind, - "camel.apache.org/kit.created.by.name": integration.Name, - "camel.apache.org/kit.created.by.version": integration.ResourceVersion, + "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.version": integration.ResourceVersion, "camel.apache.org/runtime.version": integration.Status.RuntimeVersion, "camel.apache.org/runtime.provider": string(integration.Status.RuntimeProvider), } diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go index 80e1911..205004b 100644 --- a/pkg/controller/integration/integration_controller.go +++ b/pkg/controller/integration/integration_controller.go @@ -260,13 +260,13 @@ func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.R newTarget, err := a.Handle(ctx, target) if err != nil { - events.NotifyIntegrationError(r.recorder, &instance, newTarget, err) + events.NotifyIntegrationError(ctx, r.client, r.recorder, &instance, newTarget, err) return reconcile.Result{}, err } if newTarget != nil { if res, err := r.update(ctx, &instance, newTarget); err != nil { - events.NotifyIntegrationError(r.recorder, &instance, newTarget, err) + events.NotifyIntegrationError(ctx, r.client, r.recorder, &instance, newTarget, err) return res, err } @@ -281,7 +281,7 @@ func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.R // handle one action at time so the resource // is always at its latest state - events.NotifyIntegrationUpdated(r.recorder, &instance, newTarget) + events.NotifyIntegrationUpdated(ctx, r.client, r.recorder, &instance, newTarget) break } } diff --git a/pkg/controller/integrationkit/build.go b/pkg/controller/integrationkit/build.go index aa1f61d..bed746f 100644 --- a/pkg/controller/integrationkit/build.go +++ b/pkg/controller/integrationkit/build.go @@ -90,6 +90,7 @@ func (action *buildAction) handleBuildSubmitted(ctx context.Context, kit *v1.Int ObjectMeta: metav1.ObjectMeta{ Namespace: kit.Namespace, Name: kit.Name, + Labels: kubernetes.FilterCamelCreatorLabels(kit.Labels), }, Spec: v1.BuildSpec{ Tasks: env.BuildTasks, diff --git a/pkg/controller/integrationkit/integrationkit_controller.go b/pkg/controller/integrationkit/integrationkit_controller.go index fc87130..c86947a 100644 --- a/pkg/controller/integrationkit/integrationkit_controller.go +++ b/pkg/controller/integrationkit/integrationkit_controller.go @@ -227,13 +227,13 @@ func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcil newTarget, err := a.Handle(ctx, target) if err != nil { - events.NotifyIntegrationKitError(r.recorder, &instance, newTarget, err) + events.NotifyIntegrationKitError(ctx, r.client, r.recorder, &instance, newTarget, err) return reconcile.Result{}, err } if newTarget != nil { if res, err := r.update(ctx, &instance, newTarget); err != nil { - events.NotifyIntegrationKitError(r.recorder, &instance, newTarget, err) + events.NotifyIntegrationKitError(ctx, r.client, r.recorder, &instance, newTarget, err) return res, err } @@ -248,7 +248,7 @@ func (r *ReconcileIntegrationKit) Reconcile(request reconcile.Request) (reconcil // handle one action at time so the resource // is always at its latest state - events.NotifyIntegrationKitUpdated(r.recorder, &instance, newTarget) + events.NotifyIntegrationKitUpdated(ctx, r.client, r.recorder, &instance, newTarget) break } } diff --git a/pkg/controller/integrationplatform/integrationplatform_controller.go b/pkg/controller/integrationplatform/integrationplatform_controller.go index 37fd592..1ad9a60 100644 --- a/pkg/controller/integrationplatform/integrationplatform_controller.go +++ b/pkg/controller/integrationplatform/integrationplatform_controller.go @@ -151,13 +151,13 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec target, err = a.Handle(ctx, target) if err != nil { - events.NotifyIntegrationPlatformError(r.recorder, &instance, target, err) + events.NotifyIntegrationPlatformError(ctx, r.client, r.recorder, &instance, target, err) return reconcile.Result{}, err } if target != nil { if err := r.client.Status().Patch(ctx, target, k8sclient.MergeFrom(&instance)); err != nil { - events.NotifyIntegrationPlatformError(r.recorder, &instance, target, err) + events.NotifyIntegrationPlatformError(ctx, r.client, r.recorder, &instance, target, err) return reconcile.Result{}, err } @@ -174,7 +174,7 @@ func (r *ReconcileIntegrationPlatform) Reconcile(request reconcile.Request) (rec // handle one action at time so the resource // is always at its latest state - events.NotifyIntegrationPlatformUpdated(r.recorder, &instance, target) + events.NotifyIntegrationPlatformUpdated(ctx, r.client, r.recorder, &instance, target) break } } diff --git a/pkg/events/manager.go b/pkg/events/manager.go index 2a792eb..5622d27 100644 --- a/pkg/events/manager.go +++ b/pkg/events/manager.go @@ -18,12 +18,17 @@ limitations under the License. package events import ( + "context" "fmt" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "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 ( @@ -54,10 +59,13 @@ const ( ReasonBuildConditionChanged = "BuildConditionChanged" // ReasonBuildError -- ReasonBuildError = "BuildError" + + // ReasonRelatedObjectChanged -- + ReasonRelatedObjectChanged = "ReasonRelatedObjectChanged" ) // NotifyIntegrationError automatically generates error events when the integration reconcile cycle phase has an error -func NotifyIntegrationError(recorder record.EventRecorder, old, new *v1.Integration, err error) { +func NotifyIntegrationError(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.Integration, err error) { it := old if new != nil { it = new @@ -69,7 +77,7 @@ func NotifyIntegrationError(recorder record.EventRecorder, old, new *v1.Integrat } // NotifyIntegrationUpdated automatically generates events when the integration changes -func NotifyIntegrationUpdated(recorder record.EventRecorder, old, new *v1.Integration) { +func NotifyIntegrationUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.Integration) { if new == nil { return } @@ -79,14 +87,14 @@ func NotifyIntegrationUpdated(recorder record.EventRecorder, old, new *v1.Integr oldPhase = string(old.Status.Phase) oldConditions = old.Status.GetConditions() } - notifyIfPhaseUpdated(recorder, new, oldPhase, string(new.Status.Phase), "Integration", new.Name, ReasonIntegrationPhaseUpdated) + notifyIfPhaseUpdated(ctx, c, recorder, new, oldPhase, string(new.Status.Phase), "Integration", new.Name, ReasonIntegrationPhaseUpdated) if new.Status.Phase != v1.IntegrationPhaseNone { notifyIfConditionUpdated(recorder, new, oldConditions, new.Status.GetConditions(), "Integration", new.Name, ReasonIntegrationConditionChanged) } } // NotifyIntegrationKitUpdated automatically generates events when an integration kit changes -func NotifyIntegrationKitUpdated(recorder record.EventRecorder, old, new *v1.IntegrationKit) { +func NotifyIntegrationKitUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.IntegrationKit) { if new == nil { return } @@ -96,14 +104,14 @@ func NotifyIntegrationKitUpdated(recorder record.EventRecorder, old, new *v1.Int oldPhase = string(old.Status.Phase) oldConditions = old.Status.GetConditions() } - notifyIfPhaseUpdated(recorder, new, oldPhase, string(new.Status.Phase), "Integration Kit", new.Name, ReasonIntegrationKitPhaseUpdated) + notifyIfPhaseUpdated(ctx, c, recorder, new, oldPhase, string(new.Status.Phase), "Integration Kit", new.Name, ReasonIntegrationKitPhaseUpdated) if new.Status.Phase != v1.IntegrationKitPhaseNone { notifyIfConditionUpdated(recorder, new, oldConditions, new.Status.GetConditions(), "Integration Kit", new.Name, ReasonIntegrationKitConditionChanged) } } // NotifyIntegrationKitError automatically generates error events when the integration kit reconcile cycle phase has an error -func NotifyIntegrationKitError(recorder record.EventRecorder, old, new *v1.IntegrationKit, err error) { +func NotifyIntegrationKitError(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.IntegrationKit, err error) { kit := old if new != nil { kit = new @@ -115,7 +123,7 @@ func NotifyIntegrationKitError(recorder record.EventRecorder, old, new *v1.Integ } // NotifyIntegrationPlatformUpdated automatically generates events when an integration platform changes -func NotifyIntegrationPlatformUpdated(recorder record.EventRecorder, old, new *v1.IntegrationPlatform) { +func NotifyIntegrationPlatformUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.IntegrationPlatform) { if new == nil { return } @@ -125,14 +133,14 @@ func NotifyIntegrationPlatformUpdated(recorder record.EventRecorder, old, new *v oldPhase = string(old.Status.Phase) oldConditions = old.Status.GetConditions() } - notifyIfPhaseUpdated(recorder, new, oldPhase, string(new.Status.Phase), "Integration Platform", new.Name, ReasonIntegrationPlatformPhaseUpdated) + notifyIfPhaseUpdated(ctx, c, recorder, new, oldPhase, string(new.Status.Phase), "Integration Platform", new.Name, ReasonIntegrationPlatformPhaseUpdated) if new.Status.Phase != v1.IntegrationPlatformPhaseNone { notifyIfConditionUpdated(recorder, new, oldConditions, new.Status.GetConditions(), "Integration Platform", new.Name, ReasonIntegrationPlatformConditionChanged) } } // NotifyIntegrationPlatformError automatically generates error events when the integration Platform reconcile cycle phase has an error -func NotifyIntegrationPlatformError(recorder record.EventRecorder, old, new *v1.IntegrationPlatform, err error) { +func NotifyIntegrationPlatformError(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.IntegrationPlatform, err error) { p := old if new != nil { p = new @@ -144,7 +152,7 @@ func NotifyIntegrationPlatformError(recorder record.EventRecorder, old, new *v1. } // NotifyBuildUpdated automatically generates events when a build changes -func NotifyBuildUpdated(recorder record.EventRecorder, old, new *v1.Build) { +func NotifyBuildUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.Build) { if new == nil { return } @@ -154,14 +162,14 @@ func NotifyBuildUpdated(recorder record.EventRecorder, old, new *v1.Build) { oldPhase = string(old.Status.Phase) oldConditions = old.Status.GetConditions() } - notifyIfPhaseUpdated(recorder, new, oldPhase, string(new.Status.Phase), "Build", new.Name, ReasonBuildPhaseUpdated) + notifyIfPhaseUpdated(ctx, c, recorder, new, oldPhase, string(new.Status.Phase), "Build", new.Name, ReasonBuildPhaseUpdated) if new.Status.Phase != v1.BuildPhaseNone { notifyIfConditionUpdated(recorder, new, oldConditions, new.Status.GetConditions(), "Build", new.Name, ReasonBuildConditionChanged) } } // NotifyBuildError automatically generates error events when the build reconcile cycle phase has an error -func NotifyBuildError(recorder record.EventRecorder, old, new *v1.Build, err error) { +func NotifyBuildError(ctx context.Context, c client.Client, recorder record.EventRecorder, old, new *v1.Build, err error) { p := old if new != nil { p = new @@ -172,7 +180,7 @@ func NotifyBuildError(recorder record.EventRecorder, old, new *v1.Build, err err recorder.Eventf(p, corev1.EventTypeWarning, ReasonBuildError, "Cannot reconcile Build %s: %v", p.Name, err) } -func notifyIfPhaseUpdated(recorder record.EventRecorder, new runtime.Object, oldPhase, newPhase string, resourceType, name, reason string) { +func notifyIfPhaseUpdated(ctx context.Context, c client.Client, recorder record.EventRecorder, new runtime.Object, oldPhase, newPhase string, resourceType, name, reason string) { // Update information about phase changes if oldPhase != newPhase { phase := newPhase @@ -180,6 +188,10 @@ func notifyIfPhaseUpdated(recorder record.EventRecorder, new runtime.Object, old phase = "[none]" } recorder.Eventf(new, corev1.EventTypeNormal, reason, "%s %s in phase %s", resourceType, name, phase) + + if creatorRef, creator := getCreatorObject(ctx, c, new); creatorRef != nil && creator != nil { + recorder.Eventf(creator, corev1.EventTypeNormal, ReasonRelatedObjectChanged, "%s %s dependent resource %s (%s) changed phase to %s", creatorRef.Kind, creatorRef.Name, name, resourceType, phase) + } } } @@ -212,3 +224,21 @@ func getCommonChangedConditions(old, new []v1.ResourceCondition) (res []v1.Resou } return res } + +func getCreatorObject(ctx context.Context, c client.Client, obj runtime.Object) (ref *corev1.ObjectReference, creator 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) + return nil, nil + } + return ref, &it + } + } + return nil, nil +} diff --git a/pkg/util/kubernetes/camel_labels.go b/pkg/util/kubernetes/camel_labels.go new file mode 100644 index 0000000..cc31559 --- /dev/null +++ b/pkg/util/kubernetes/camel_labels.go @@ -0,0 +1,74 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kubernetes + +import ( + "strings" + + camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +// CamelCreatorLabelPrefix -- +const ( + CamelCreatorLabelPrefix = "camel.apache.org/created.by" + + CamelCreatorLabelKind = CamelCreatorLabelPrefix + ".kind" + CamelCreatorLabelName = CamelCreatorLabelPrefix + ".name" +) + +// FilterCamelCreatorLabels is used to inherit the creator information among resources +func FilterCamelCreatorLabels(source map[string]string) map[string]string { + res := make(map[string]string) + for k, v := range source { + if strings.HasPrefix(k, CamelCreatorLabelPrefix) { + res[k] = v + } + } + return res +} + +// MergeCamelCreatorLabels is used to inject the creator information from another set of labels +func MergeCamelCreatorLabels(source map[string]string, target map[string]string) map[string]string { + if target == nil { + target = make(map[string]string) + } + for k, v := range FilterCamelCreatorLabels(source) { + target[k] = v + } + return target +} + +// GetCamelCreator returns the Camel creator object referenced by this runtime object, if present +func GetCamelCreator(obj runtime.Object) *v1.ObjectReference { + if m, ok := obj.(metav1.Object); ok { + kind := m.GetLabels()[CamelCreatorLabelKind] + name := m.GetLabels()[CamelCreatorLabelName] + if kind != "" && name != "" { + return &v1.ObjectReference{ + Kind: kind, + Namespace: m.GetNamespace(), + Name: name, + APIVersion: camelv1.SchemeGroupVersion.String(), + } + } + } + return nil +}