This is an automated email from the ASF dual-hosted git repository.

tsato pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 28550fcac6c57a954ccc3c558d2c4590d5af168e
Author: Tadayoshi Sato <[email protected]>
AuthorDate: Tue Sep 13 13:45:20 2022 +0900

    fix(trait): traits for Initialization phase should not trigger at platform 
setup action
---
 pkg/apis/camel/v1/integration_types.go       |  4 +-
 pkg/controller/integration/platform_setup.go |  7 +++
 pkg/trait/platform.go                        | 94 +++++++++++++++-------------
 pkg/trait/platform_test.go                   |  2 +-
 4 files changed, 62 insertions(+), 45 deletions(-)

diff --git a/pkg/apis/camel/v1/integration_types.go 
b/pkg/apis/camel/v1/integration_types.go
index 3ba0c2768..c6f1e750e 100644
--- a/pkg/apis/camel/v1/integration_types.go
+++ b/pkg/apis/camel/v1/integration_types.go
@@ -148,10 +148,10 @@ const (
 
        // IntegrationPhaseNone --
        IntegrationPhaseNone IntegrationPhase = ""
-       // IntegrationPhaseInitialization --
-       IntegrationPhaseInitialization IntegrationPhase = "Initialization"
        // IntegrationPhaseWaitingForPlatform --
        IntegrationPhaseWaitingForPlatform IntegrationPhase = "Waiting For 
Platform"
+       // IntegrationPhaseInitialization --
+       IntegrationPhaseInitialization IntegrationPhase = "Initialization"
        // IntegrationPhaseBuildingKit --
        IntegrationPhaseBuildingKit IntegrationPhase = "Building Kit"
        // IntegrationPhaseDeploying --
diff --git a/pkg/controller/integration/platform_setup.go 
b/pkg/controller/integration/platform_setup.go
index 148673ec2..6c1b85bb1 100644
--- a/pkg/controller/integration/platform_setup.go
+++ b/pkg/controller/integration/platform_setup.go
@@ -62,6 +62,13 @@ func (action *platformSetupAction) Handle(ctx 
context.Context, integration *v1.I
                integration.Status.Profile = determineBestProfile(ctx, 
action.client, integration, pl)
        }
 
+       // Change the integration phase to Initialization after traits have 
been applied
+       // so that traits targeting Initialization phase don't get applied 
unintentionally
+       // at the platform setup step.
+       if integration.Status.Phase != v1.IntegrationPhaseWaitingForPlatform {
+               integration.Status.Phase = v1.IntegrationPhaseInitialization
+       }
+
        return integration, nil
 }
 
diff --git a/pkg/trait/platform.go b/pkg/trait/platform.go
index d2e7b6783..b6ed793cd 100644
--- a/pkg/trait/platform.go
+++ b/pkg/trait/platform.go
@@ -80,20 +80,31 @@ func (t *platformTrait) Apply(e *Environment) error {
        initial := e.Integration.DeepCopy()
 
        pl, err := t.getOrCreatePlatform(e)
-       if err != nil || pl.Status.Phase != v1.IntegrationPlatformPhaseReady {
+       // Do not change to Initialization phase within the trait
+       switch {
+       case err != nil:
                e.Integration.Status.Phase = 
v1.IntegrationPhaseWaitingForPlatform
-       } else {
-               e.Integration.Status.Phase = v1.IntegrationPhaseInitialization
-       }
-
-       if initial.Status.Phase != e.Integration.Status.Phase {
-               if err != nil {
-                       
e.Integration.Status.SetErrorCondition(v1.IntegrationConditionPlatformAvailable,
 v1.IntegrationConditionPlatformAvailableReason, err)
+               if initial.Status.Phase != e.Integration.Status.Phase {
+                       e.Integration.Status.SetErrorCondition(
+                               v1.IntegrationConditionPlatformAvailable,
+                               v1.IntegrationConditionPlatformAvailableReason,
+                               err)
+
+                       if pl != nil {
+                               e.Integration.SetIntegrationPlatform(pl)
+                       }
                }
-
-               if pl != nil {
+       case pl == nil:
+               e.Integration.Status.Phase = 
v1.IntegrationPhaseWaitingForPlatform
+       case pl.Status.Phase != v1.IntegrationPlatformPhaseReady:
+               e.Integration.Status.Phase = 
v1.IntegrationPhaseWaitingForPlatform
+               if initial.Status.Phase != e.Integration.Status.Phase {
                        e.Integration.SetIntegrationPlatform(pl)
                }
+       default:
+               // In success case, phase should be reset to none
+               e.Integration.Status.Phase = v1.IntegrationPhaseNone
+               e.Integration.SetIntegrationPlatform(pl)
        }
 
        return nil
@@ -101,43 +112,42 @@ func (t *platformTrait) Apply(e *Environment) error {
 
 func (t *platformTrait) getOrCreatePlatform(e *Environment) 
(*v1.IntegrationPlatform, error) {
        pl, err := platform.GetOrFindForResource(e.Ctx, t.Client, 
e.Integration, false)
-       if err != nil && apierrors.IsNotFound(err) {
-               if pointer.BoolDeref(t.CreateDefault, false) {
-                       platformName := e.Integration.Status.Platform
-                       if platformName == "" {
-                               platformName = defaults.OperatorID()
-                       }
-
-                       if platformName == "" {
-                               platformName = platform.DefaultPlatformName
-                       }
-                       namespace := e.Integration.Namespace
-                       if pointer.BoolDeref(t.Global, false) {
-                               operatorNamespace := 
platform.GetOperatorNamespace()
-                               if operatorNamespace != "" {
-                                       namespace = operatorNamespace
-                               }
-                       }
-                       defaultPlatform := v1.NewIntegrationPlatform(namespace, 
platformName)
-                       if defaultPlatform.Labels == nil {
-                               defaultPlatform.Labels = make(map[string]string)
-                       }
-                       
defaultPlatform.Labels["camel.apache.org/platform.generated"] = True
-                       // Cascade the operator id in charge to reconcile the 
Integration
-                       if v1.GetOperatorIDAnnotation(e.Integration) != "" {
-                               
defaultPlatform.SetOperatorID(v1.GetOperatorIDAnnotation(e.Integration))
-                       }
-                       pl = &defaultPlatform
-                       e.Resources.Add(pl)
+       if err != nil && apierrors.IsNotFound(err) && 
pointer.BoolDeref(t.CreateDefault, false) {
+               platformName := e.Integration.Status.Platform
+               if platformName == "" {
+                       platformName = defaults.OperatorID()
+               }
 
-                       // Make sure that IntegrationPlatform installed in 
operator namespace can be seen by others
-                       if err := install.IntegrationPlatformViewerRole(e.Ctx, 
t.Client, namespace); err != nil && !apierrors.IsAlreadyExists(err) {
-                               t.L.Info(fmt.Sprintf("Cannot install global 
IntegrationPlatform viewer role in namespace '%s': skipping.", namespace))
+               if platformName == "" {
+                       platformName = platform.DefaultPlatformName
+               }
+               namespace := e.Integration.Namespace
+               if pointer.BoolDeref(t.Global, false) {
+                       operatorNamespace := platform.GetOperatorNamespace()
+                       if operatorNamespace != "" {
+                               namespace = operatorNamespace
                        }
+               }
+               defaultPlatform := v1.NewIntegrationPlatform(namespace, 
platformName)
+               if defaultPlatform.Labels == nil {
+                       defaultPlatform.Labels = make(map[string]string)
+               }
+               defaultPlatform.Labels["camel.apache.org/platform.generated"] = 
True
+               // Cascade the operator id in charge to reconcile the 
Integration
+               if v1.GetOperatorIDAnnotation(e.Integration) != "" {
+                       
defaultPlatform.SetOperatorID(v1.GetOperatorIDAnnotation(e.Integration))
+               }
+               pl = &defaultPlatform
+               e.Resources.Add(pl)
 
-                       return pl, nil
+               // Make sure that IntegrationPlatform installed in operator 
namespace can be seen by others
+               if err := install.IntegrationPlatformViewerRole(e.Ctx, 
t.Client, namespace); err != nil && !apierrors.IsAlreadyExists(err) {
+                       t.L.Info(fmt.Sprintf("Cannot install global 
IntegrationPlatform viewer role in namespace '%s': skipping.", namespace))
                }
+
+               return pl, nil
        }
+
        return pl, err
 }
 
diff --git a/pkg/trait/platform_test.go b/pkg/trait/platform_test.go
index 02fe8e4f1..488cbb739 100644
--- a/pkg/trait/platform_test.go
+++ b/pkg/trait/platform_test.go
@@ -127,7 +127,7 @@ func TestPlatformTraitExisting(t *testing.T) {
                {
                        name:          "Move state",
                        platformPhase: v1.IntegrationPlatformPhaseReady,
-                       expectedPhase: v1.IntegrationPhaseInitialization,
+                       expectedPhase: v1.IntegrationPhaseNone,
                },
        }
 

Reply via email to