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 ba9623bd8d5f5808e7715a4170eaac0fe2a130bf
Author: nicolaferraro <ni.ferr...@gmail.com>
AuthorDate: Wed Oct 13 17:54:24 2021 +0200

    Fix #2687: select platform based on annotations
---
 docs/modules/traits/pages/cron.adoc          | 11 ++++++++
 pkg/apis/camel/v1/common_types.go            |  7 ++---
 pkg/builder/builder.go                       |  2 +-
 pkg/builder/image.go                         |  8 +++---
 pkg/controller/integration/initialize.go     |  2 +-
 pkg/controller/integration/kits.go           |  2 +-
 pkg/controller/integration/platform_setup.go |  2 +-
 pkg/controller/kameletbinding/common.go      |  2 +-
 pkg/platform/platform.go                     | 38 ++++++++++++++++++++++++++--
 pkg/trait/builder.go                         |  1 +
 pkg/trait/trait.go                           |  9 ++++---
 11 files changed, 65 insertions(+), 19 deletions(-)

diff --git a/docs/modules/traits/pages/cron.adoc 
b/docs/modules/traits/pages/cron.adoc
index 7e67e51..8708acf 100755
--- a/docs/modules/traits/pages/cron.adoc
+++ b/docs/modules/traits/pages/cron.adoc
@@ -76,6 +76,17 @@ while `35m` or `50s` cannot).
 | Optional deadline in seconds for starting the job if it misses scheduled
 time for any reason.  Missed jobs executions will be counted as failed ones.
 
+| cron.active-deadline-seconds
+| int64
+| Specifies the duration in seconds, relative to the start time, that the job
+may be continuously active before it is considered to be failed.
+It defaults to 60s.
+
+| cron.backoff-limit
+| int32
+| Specifies the number of retries before marking the job failed.
+It defaults to 2.
+
 |===
 
 // End of autogenerated code - DO NOT EDIT! (configuration)
diff --git a/pkg/apis/camel/v1/common_types.go 
b/pkg/apis/camel/v1/common_types.go
index 649ce6e..571f547 100644
--- a/pkg/apis/camel/v1/common_types.go
+++ b/pkg/apis/camel/v1/common_types.go
@@ -23,9 +23,10 @@ import (
 )
 
 const (
-       TraitAnnotationPrefix  = "trait.camel.apache.org/"
-       OperatorIDLabel        = "camel.apache.org/operator.id"
-       SecondaryPlatformLabel = "camel.apache.org/secondary.platform"
+       TraitAnnotationPrefix      = "trait.camel.apache.org/"
+       OperatorIDLabel            = "camel.apache.org/operator.id"
+       SecondaryPlatformLabel     = "camel.apache.org/secondary.platform"
+       PlatformSelectorAnnotation = "camel.apache.org/platform.id"
 )
 
 // BuildStrategy specifies how the Build should be executed
diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go
index 5668119..fea59f7 100644
--- a/pkg/builder/builder.go
+++ b/pkg/builder/builder.go
@@ -88,7 +88,7 @@ func (t *builderTask) Do(ctx context.Context) v1.BuildStatus {
 
        steps, err := StepsFrom(t.task.Steps...)
        if err != nil {
-               t.log.Errorf(err,"invalid builder steps: %s", t.task.Steps)
+               t.log.Errorf(err, "invalid builder steps: %s", t.task.Steps)
                result.Failed(err)
                return result
        }
diff --git a/pkg/builder/image.go b/pkg/builder/image.go
index 637ca97..9d1835e 100644
--- a/pkg/builder/image.go
+++ b/pkg/builder/image.go
@@ -28,7 +28,6 @@ import (
        ctrl "sigs.k8s.io/controller-runtime/pkg/client"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
-       "github.com/apache/camel-k/pkg/platform"
        "github.com/apache/camel-k/pkg/util"
        "github.com/apache/camel-k/pkg/util/defaults"
 )
@@ -146,11 +145,10 @@ func incrementalImageContext(ctx *builderContext) error {
                                }
                        }
                } else {
-                       pl, err := platform.GetCurrent(ctx.C, ctx, 
ctx.Namespace)
-                       if err != nil {
-                               return err
+                       if ctx.BaseImage == "" {
+                               // TODO: transient workaround to be removed in 
1.8.x
+                               ctx.BaseImage = defaults.BaseImage()
                        }
-                       ctx.BaseImage = pl.Status.Build.BaseImage
                }
 
                return nil
diff --git a/pkg/controller/integration/initialize.go 
b/pkg/controller/integration/initialize.go
index 8f72899..c168fa7 100644
--- a/pkg/controller/integration/initialize.go
+++ b/pkg/controller/integration/initialize.go
@@ -68,7 +68,7 @@ func (action *initializeAction) Handle(ctx context.Context, 
integration *v1.Inte
                        kitName := integration.Spec.IntegrationKit.Name
 
                        if kitNamespace == "" {
-                               pl, err := platform.GetCurrent(ctx, 
action.client, integration.Namespace)
+                               pl, err := platform.GetForResource(ctx, 
action.client, integration)
                                if err != nil && !k8serrors.IsNotFound(err) {
                                        return nil, err
                                }
diff --git a/pkg/controller/integration/kits.go 
b/pkg/controller/integration/kits.go
index 49927da..f28723d 100644
--- a/pkg/controller/integration/kits.go
+++ b/pkg/controller/integration/kits.go
@@ -36,7 +36,7 @@ import (
 )
 
 func lookupKitsForIntegration(ctx context.Context, c ctrl.Reader, integration 
*v1.Integration, options ...ctrl.ListOption) ([]v1.IntegrationKit, error) {
-       pl, err := platform.GetCurrent(ctx, c, integration.Namespace)
+       pl, err := platform.GetForResource(ctx, c, integration)
        if err != nil && !errors.IsNotFound(err) {
                return nil, err
        }
diff --git a/pkg/controller/integration/platform_setup.go 
b/pkg/controller/integration/platform_setup.go
index 97cd3bb..de6ac1f 100644
--- a/pkg/controller/integration/platform_setup.go
+++ b/pkg/controller/integration/platform_setup.go
@@ -55,7 +55,7 @@ func (action *platformSetupAction) Handle(ctx 
context.Context, integration *v1.I
                return nil, err
        }
 
-       pl, err := platform.GetCurrent(ctx, action.client, 
integration.Namespace)
+       pl, err := platform.GetForResource(ctx, action.client, integration)
        if err != nil && !k8serrors.IsNotFound(err) {
                return nil, err
        } else if pl != nil {
diff --git a/pkg/controller/kameletbinding/common.go 
b/pkg/controller/kameletbinding/common.go
index ce219d8..08892fd 100644
--- a/pkg/controller/kameletbinding/common.go
+++ b/pkg/controller/kameletbinding/common.go
@@ -231,7 +231,7 @@ func determineProfile(ctx context.Context, c client.Client, 
binding *v1alpha1.Ka
        if binding.Spec.Integration != nil && binding.Spec.Integration.Profile 
!= "" {
                return binding.Spec.Integration.Profile, nil
        }
-       pl, err := platform.GetCurrent(ctx, c, binding.Namespace)
+       pl, err := platform.GetForResource(ctx, c, binding)
        if err != nil && !k8serrors.IsNotFound(err) {
                return "", errors.Wrap(err, "error while retrieving the 
integration platform")
        }
diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go
index d273f3d..8cd6466 100644
--- a/pkg/platform/platform.go
+++ b/pkg/platform/platform.go
@@ -19,9 +19,12 @@ package platform
 
 import (
        "context"
+       "fmt"
+       "strings"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
        "github.com/apache/camel-k/pkg/util/kubernetes"
+       corev1 "k8s.io/api/core/v1"
        k8serrors "k8s.io/apimachinery/pkg/api/errors"
        "k8s.io/apimachinery/pkg/labels"
        "k8s.io/apimachinery/pkg/selection"
@@ -33,8 +36,39 @@ const (
        DefaultPlatformName = "camel-k"
 )
 
-// GetCurrent returns the currently installed platform (local or global)
-func GetCurrent(ctx context.Context, c k8sclient.Reader, namespace string) 
(*v1.IntegrationPlatform, error) {
+func GetForResource(ctx context.Context, c k8sclient.Reader, o 
k8sclient.Object) (*v1.IntegrationPlatform, error) {
+       ref, err := extractPlatformSelector(o)
+       if err != nil {
+               return nil, err
+       }
+       if ref != nil {
+               return get(ctx, c, ref.Namespace, ref.Name)
+       }
+       return getCurrent(ctx, c, o.GetNamespace())
+}
+
+func extractPlatformSelector(o k8sclient.Object) (*corev1.ObjectReference, 
error) {
+       selectedPlatform := o.GetAnnotations()[v1.PlatformSelectorAnnotation]
+       if selectedPlatform == "" {
+               return nil, nil
+       }
+       parts := strings.Split(selectedPlatform, "/")
+       if len(parts) == 0 || len(parts) > 2 {
+               return nil, fmt.Errorf("Invalid platform selector: expected 
\"[namespace/]name\", got %q", selectedPlatform)
+       }
+       namespace := o.GetNamespace()
+       if len(parts) == 2 {
+               namespace = parts[0]
+       }
+       name := parts[len(parts)-1]
+       return &corev1.ObjectReference{
+               Namespace: namespace,
+               Name:      name,
+       }, 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)
 }
 
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index 7a78a25..03db319 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -145,6 +145,7 @@ func (t *builderTrait) builderTask(e *Environment) 
(*v1.BuilderTask, error) {
                BaseTask: v1.BaseTask{
                        Name: "builder",
                },
+               BaseImage:    e.Platform.Status.Build.BaseImage,
                Runtime:      e.CamelCatalog.Runtime,
                Dependencies: e.IntegrationKit.Spec.Dependencies,
                Maven:        maven,
diff --git a/pkg/trait/trait.go b/pkg/trait/trait.go
index fe837f9..cbb3a9c 100644
--- a/pkg/trait/trait.go
+++ b/pkg/trait/trait.go
@@ -29,6 +29,7 @@ import (
        "github.com/apache/camel-k/pkg/client"
        "github.com/apache/camel-k/pkg/platform"
        "github.com/apache/camel-k/pkg/util/kubernetes"
+       k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 )
 
 func Apply(ctx context.Context, c client.Client, integration *v1.Integration, 
kit *v1.IntegrationKit) (*Environment, error) {
@@ -64,14 +65,14 @@ func newEnvironment(ctx context.Context, c client.Client, 
integration *v1.Integr
                return nil, errors.New("neither integration nor kit are set")
        }
 
-       namespace := ""
+       var obj k8sclient.Object
        if integration != nil {
-               namespace = integration.Namespace
+               obj = integration
        } else if kit != nil {
-               namespace = kit.Namespace
+               obj = kit
        }
 
-       pl, err := platform.GetCurrent(ctx, c, namespace)
+       pl, err := platform.GetForResource(ctx, c, obj)
        if err != nil && !k8serrors.IsNotFound(err) {
                return nil, err
        }

Reply via email to