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 23f92a039a391dec8b8788605bc2f990c262b969
Author: nicolaferraro <ni.ferr...@gmail.com>
AuthorDate: Thu Oct 14 10:50:46 2021 +0200

    Fix #2687: cascade platform to resources
---
 pkg/controller/integrationkit/build.go             | 14 +++--
 .../integrationkit/integrationkit_controller.go    |  2 +-
 pkg/platform/platform.go                           | 59 ++++++++++++++--------
 pkg/trait/container.go                             |  5 ++
 pkg/trait/platform.go                              |  2 +-
 pkg/trait/quarkus.go                               |  5 ++
 6 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/pkg/controller/integrationkit/build.go 
b/pkg/controller/integrationkit/build.go
index 9f1c0ca..cb74cd0 100644
--- a/pkg/controller/integrationkit/build.go
+++ b/pkg/controller/integrationkit/build.go
@@ -90,6 +90,13 @@ func (action *buildAction) handleBuildSubmitted(ctx 
context.Context, kit *v1.Int
                if operatorID != "" {
                        labels[v1.OperatorIDLabel] = operatorID
                }
+               var annotations map[string]string
+               if v, ok := kit.Annotations[v1.PlatformSelectorAnnotation]; ok {
+                       annotations = map[string]string{
+                               v1.PlatformSelectorAnnotation: v,
+                       }
+               }
+
                timeout := env.Platform.Status.Build.GetTimeout()
                if layout := labels[v1.IntegrationKitLayoutLabel]; 
env.Platform.Spec.Build.Timeout == nil && layout == 
v1.IntegrationKitLayoutNative {
                        // Increase the timeout to a sensible default
@@ -103,9 +110,10 @@ func (action *buildAction) handleBuildSubmitted(ctx 
context.Context, kit *v1.Int
                                Kind:       v1.BuildKind,
                        },
                        ObjectMeta: metav1.ObjectMeta{
-                               Namespace: kit.Namespace,
-                               Name:      kit.Name,
-                               Labels:    labels,
+                               Namespace:   kit.Namespace,
+                               Name:        kit.Name,
+                               Labels:      labels,
+                               Annotations: annotations,
                        },
                        Spec: v1.BuildSpec{
                                Strategy: 
env.Platform.Status.Build.BuildStrategy,
diff --git a/pkg/controller/integrationkit/integrationkit_controller.go 
b/pkg/controller/integrationkit/integrationkit_controller.go
index 1eebefa..b82d354 100644
--- a/pkg/controller/integrationkit/integrationkit_controller.go
+++ b/pkg/controller/integrationkit/integrationkit_controller.go
@@ -216,7 +216,7 @@ func (r *reconcileIntegrationKit) Reconcile(ctx 
context.Context, request reconci
                        return r.update(ctx, &instance, target)
                } else {
                        // Platform is always local to the kit
-                       pl, err := platform.GetOrFindLocal(ctx, r.client, 
target.Namespace, target.Status.Platform, true)
+                       pl, err := platform.GetOrFindLocalForResource(ctx, 
r.client, target, true)
                        if err != nil || pl.Status.Phase != 
v1.IntegrationPlatformPhaseReady {
                                target.Status.Phase = 
v1.IntegrationKitPhaseWaitingForPlatform
                        } else {
diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go
index 8cd6466..e7e0774 100644
--- a/pkg/platform/platform.go
+++ b/pkg/platform/platform.go
@@ -37,6 +37,18 @@ const (
 )
 
 func GetForResource(ctx context.Context, c k8sclient.Reader, o 
k8sclient.Object) (*v1.IntegrationPlatform, error) {
+       return GetOrFindForResource(ctx, c, o, true)
+}
+
+func GetOrFindForResource(ctx context.Context, c k8sclient.Reader, o 
k8sclient.Object, active bool) (*v1.IntegrationPlatform, error) {
+       return getOrFindForResource(ctx, c, o, active, false)
+}
+
+func GetOrFindLocalForResource(ctx context.Context, c k8sclient.Reader, o 
k8sclient.Object, active bool) (*v1.IntegrationPlatform, error) {
+       return getOrFindForResource(ctx, c, o, active, true)
+}
+
+func getOrFindForResource(ctx context.Context, c k8sclient.Reader, o 
k8sclient.Object, active bool, local bool) (*v1.IntegrationPlatform, error) {
        ref, err := extractPlatformSelector(o)
        if err != nil {
                return nil, err
@@ -44,7 +56,14 @@ func GetForResource(ctx context.Context, c k8sclient.Reader, 
o k8sclient.Object)
        if ref != nil {
                return get(ctx, c, ref.Namespace, ref.Name)
        }
-       return getCurrent(ctx, c, o.GetNamespace())
+       if it, ok := o.(*v1.Integration); ok {
+               return getOrFind(ctx, c, it.Namespace, it.Status.Platform, 
active, local)
+       } else if ik, ok := o.(*v1.IntegrationKit); ok {
+               return getOrFind(ctx, c, ik.Namespace, ik.Status.Platform, 
active, local)
+       } else if b, ok := o.(*v1.Build); ok {
+               return getOrFind(ctx, c, b.Namespace, b.Status.Platform, 
active, local)
+       }
+       return find(ctx, c, o.GetNamespace(), active, local)
 }
 
 func extractPlatformSelector(o k8sclient.Object) (*corev1.ObjectReference, 
error) {
@@ -67,22 +86,24 @@ func extractPlatformSelector(o k8sclient.Object) 
(*corev1.ObjectReference, error
        }, nil
 }
 
-// getCurrent returns the currently installed platform (local or global)
-func getCurrent(ctx context.Context, c k8sclient.Reader, namespace string) 
(*v1.IntegrationPlatform, error) {
-       return find(ctx, c, namespace, true)
+func getOrFind(ctx context.Context, c k8sclient.Reader, namespace string, name 
string, active bool, local bool) (*v1.IntegrationPlatform, error) {
+       if local {
+               return getOrFindLocal(ctx, c, namespace, name, active)
+       }
+       return getOrFindAny(ctx, c, namespace, name, active)
 }
 
-// GetOrFind returns the named platform or any other platform in the local 
namespace or the global one
-func GetOrFind(ctx context.Context, c k8sclient.Reader, namespace string, name 
string, active bool) (*v1.IntegrationPlatform, error) {
+// getOrFindAny returns the named platform or any other platform in the local 
namespace or the global one
+func getOrFindAny(ctx context.Context, c k8sclient.Reader, namespace string, 
name string, active bool) (*v1.IntegrationPlatform, error) {
        if name != "" {
                return get(ctx, c, namespace, name)
        }
 
-       return find(ctx, c, namespace, active)
+       return findAny(ctx, c, namespace, active)
 }
 
-// GetOrFindLocal returns the named platform or any other platform in the 
local namespace
-func GetOrFindLocal(ctx context.Context, c k8sclient.Reader, namespace string, 
name string, active bool) (*v1.IntegrationPlatform, error) {
+// getOrFindLocal returns the named platform or any other platform in the 
local namespace
+func getOrFindLocal(ctx context.Context, c k8sclient.Reader, namespace string, 
name string, active bool) (*v1.IntegrationPlatform, error) {
        if name != "" {
                return kubernetes.GetIntegrationPlatform(ctx, c, name, 
namespace)
        }
@@ -102,8 +123,15 @@ func get(ctx context.Context, c k8sclient.Reader, 
namespace string, name string)
        return p, err
 }
 
-// find returns the currently installed platform or any platform existing in 
local or operator namespace
-func find(ctx context.Context, c k8sclient.Reader, namespace string, active 
bool) (*v1.IntegrationPlatform, error) {
+func find(ctx context.Context, c k8sclient.Reader, namespace string, active 
bool, local bool) (*v1.IntegrationPlatform, error) {
+       if local {
+               return findLocal(ctx, c, namespace, active)
+       }
+       return findAny(ctx, c, namespace, active)
+}
+
+// findAny returns the currently installed platform or any platform existing 
in local or operator namespace
+func findAny(ctx context.Context, c k8sclient.Reader, namespace string, active 
bool) (*v1.IntegrationPlatform, error) {
        p, err := findLocal(ctx, c, namespace, active)
        if err != nil && k8serrors.IsNotFound(err) {
                operatorNamespace := GetOperatorNamespace()
@@ -153,15 +181,6 @@ func ListPrimaryPlatforms(ctx context.Context, c 
k8sclient.Reader, namespace str
        return &lst, nil
 }
 
-// ListAllPlatforms returns all platforms installed in a given namespace
-func ListAllPlatforms(ctx context.Context, c k8sclient.Reader, namespace 
string) (*v1.IntegrationPlatformList, error) {
-       lst := v1.NewIntegrationPlatformList()
-       if err := c.List(ctx, &lst, k8sclient.InNamespace(namespace)); err != 
nil {
-               return nil, err
-       }
-       return &lst, nil
-}
-
 // IsActive determines if the given platform is being used
 func IsActive(p *v1.IntegrationPlatform) bool {
        return p.Status.Phase != "" && p.Status.Phase != 
v1.IntegrationPlatformPhaseDuplicate
diff --git a/pkg/trait/container.go b/pkg/trait/container.go
index ea4973f..00c9826 100644
--- a/pkg/trait/container.go
+++ b/pkg/trait/container.go
@@ -220,6 +220,11 @@ func (t *containerTrait) configureImageIntegrationKit(e 
*Environment) error {
                if operatorID != "" {
                        kit.Labels[v1.OperatorIDLabel] = operatorID
                }
+               if v, ok := 
e.Integration.Annotations[v1.PlatformSelectorAnnotation]; ok {
+                       kit.Annotations = map[string]string{
+                               v1.PlatformSelectorAnnotation: v,
+                       }
+               }
 
                t.L.Infof("image %s", kit.Spec.Image)
                e.Resources.Add(kit)
diff --git a/pkg/trait/platform.go b/pkg/trait/platform.go
index d0d9b23..e273040 100644
--- a/pkg/trait/platform.go
+++ b/pkg/trait/platform.go
@@ -101,7 +101,7 @@ func (t *platformTrait) Apply(e *Environment) error {
 }
 
 func (t *platformTrait) getOrCreatePlatform(e *Environment) 
(*v1.IntegrationPlatform, error) {
-       pl, err := platform.GetOrFind(e.Ctx, t.Client, e.Integration.Namespace, 
e.Integration.Status.Platform, false)
+       pl, err := platform.GetOrFindForResource(e.Ctx, t.Client, 
e.Integration, false)
        if err != nil && k8serrors.IsNotFound(err) {
                if IsTrue(t.CreateDefault) {
                        platformName := e.Integration.Status.Platform
diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go
index 2b53549..1c66527 100644
--- a/pkg/trait/quarkus.go
+++ b/pkg/trait/quarkus.go
@@ -265,6 +265,11 @@ func (t *quarkusTrait) newIntegrationKit(e *Environment, 
packageType quarkusPack
        if operatorID != "" {
                kit.Labels[v1.OperatorIDLabel] = operatorID
        }
+       if v, ok := integration.Annotations[v1.PlatformSelectorAnnotation]; ok {
+               kit.Annotations = map[string]string{
+                       v1.PlatformSelectorAnnotation: v,
+               }
+       }
 
        traits := t.getKitTraits(e)
 

Reply via email to