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

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

commit fa10c8d303ddbce697feab25c04f8fbece79c06e
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Sep 13 16:37:03 2024 +0200

    fix(kamelets): don't fail on Kamelets recreation
---
 pkg/controller/integrationplatform/create.go       | 21 ++++++++++----------
 pkg/controller/integrationplatform/create_test.go  | 23 +++++++++++++++++++++-
 pkg/controller/integrationplatform/kamelets.go     | 15 ++++++++++----
 .../integrationplatform/kamelets_test.go           |  3 ++-
 4 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/pkg/controller/integrationplatform/create.go 
b/pkg/controller/integrationplatform/create.go
index 85edbf94a..55e9ca3f3 100644
--- a/pkg/controller/integrationplatform/create.go
+++ b/pkg/controller/integrationplatform/create.go
@@ -167,23 +167,22 @@ func installKamelets(ctx context.Context, c 
client.Client, platform *v1.Integrat
                camelVersion := platform.Status.Build.RuntimeCoreVersion
                installedKam, erroredKam, err := installKameletCatalog(ctx, c, 
platform, camelVersion)
                if err != nil {
-                       platform.Status.Phase = v1.IntegrationPlatformPhaseError
+                       // An error here should not be disruptive, we just 
report it happened
                        platform.Status.SetCondition(
                                
v1.IntegrationPlatformConditionKameletCatalogAvailable,
                                corev1.ConditionFalse,
                                "IntegrationPlatformKameletCatalogAvailable",
-                               fmt.Sprintf("kamelet catalog %s not available, 
please review given camel version. Error: %s", camelVersion, err),
+                               fmt.Sprintf("kamelet catalog %s not available. 
Error: %s", camelVersion, err),
+                       )
+               } else {
+                       platform.Status.SetCondition(
+                               
v1.IntegrationPlatformConditionKameletCatalogAvailable,
+                               corev1.ConditionTrue,
+                               "IntegrationPlatformKameletCatalogAvailable",
+                               fmt.Sprintf("successfully installed Kamelet 
catalog version %s: success %d Kamelets, failed %d Kamelets",
+                                       camelVersion, installedKam, erroredKam),
                        )
-
-                       return platform, nil
                }
-               platform.Status.SetCondition(
-                       v1.IntegrationPlatformConditionKameletCatalogAvailable,
-                       corev1.ConditionTrue,
-                       "IntegrationPlatformKameletCatalogAvailable",
-                       fmt.Sprintf("successfully installed Kamelet catalog 
version %s: success %d Kamelets, failed %d Kamelets",
-                               camelVersion, installedKam, erroredKam),
-               )
        }
 
        return platform, nil
diff --git a/pkg/controller/integrationplatform/create_test.go 
b/pkg/controller/integrationplatform/create_test.go
index c47532070..0124a81db 100644
--- a/pkg/controller/integrationplatform/create_test.go
+++ b/pkg/controller/integrationplatform/create_test.go
@@ -159,10 +159,10 @@ func TestCreateNewCatalog(t *testing.T) {
 
        // Set the folder where to install testing kamelets
        tmpDir, err := os.MkdirTemp("/tmp", "kamelets*")
+       defer os.Unsetenv(kameletDirEnv)
        assert.NoError(t, err)
        os.Setenv(kameletDirEnv, tmpDir)
        answer, err := action.Handle(context.TODO(), &ip)
-       os.Unsetenv(kameletDirEnv)
        require.NoError(t, err)
        assert.NotNil(t, answer)
 
@@ -180,6 +180,27 @@ func TestCreateNewCatalog(t *testing.T) {
 
        require.NoError(t, err)
        assert.NotEmpty(t, list.Items)
+
+       // Creating again a platform should not cause problem because any 
existing Kamelets file leftover
+       ip.Status = v1.IntegrationPlatformStatus{
+               IntegrationPlatformSpec: v1.IntegrationPlatformSpec{
+                       Build: v1.IntegrationPlatformBuildSpec{
+                               RuntimeProvider: v1.RuntimeProviderQuarkus,
+                               RuntimeVersion:  defaults.DefaultRuntimeVersion,
+                       },
+               },
+       }
+       // Refresh client with changed IP
+       c, err = test.NewFakeClient(&ip)
+       require.NoError(t, err)
+       action.InjectClient(c)
+       answer, err = action.Handle(context.TODO(), &ip)
+       require.NoError(t, err)
+       assert.NotNil(t, answer)
+
+       assert.Equal(t, v1.IntegrationPlatformPhaseReady, answer.Status.Phase)
+       assert.Equal(t, corev1.ConditionTrue, 
answer.Status.GetCondition(v1.IntegrationPlatformConditionCamelCatalogAvailable).Status)
+       assert.Equal(t, corev1.ConditionTrue, 
answer.Status.GetCondition(v1.IntegrationPlatformConditionKameletCatalogAvailable).Status)
 }
 
 func TestCreateCatalogError(t *testing.T) {
diff --git a/pkg/controller/integrationplatform/kamelets.go 
b/pkg/controller/integrationplatform/kamelets.go
index 370f464e0..83cfa5d6d 100644
--- a/pkg/controller/integrationplatform/kamelets.go
+++ b/pkg/controller/integrationplatform/kamelets.go
@@ -56,7 +56,10 @@ func installKameletCatalog(ctx context.Context, c 
client.Client, platform *v1.In
                return -1, -1, err
        }
        // Prepare directory to contains kamelets
-       kameletDir := prepareKameletDirectory()
+       kameletDir, err := prepareKameletDirectory()
+       if err != nil {
+               return -1, -1, err
+       }
        // Download Kamelet dependency
        if err := downloadKameletDependency(ctx, version, kameletDir); err != 
nil {
                return -1, -1, err
@@ -65,7 +68,6 @@ func installKameletCatalog(ctx context.Context, c 
client.Client, platform *v1.In
        if err := extractKameletsFromDependency(ctx, version, kameletDir); err 
!= nil {
                return -1, -1, err
        }
-
        // Store Kamelets as Kubernetes resources
        return applyKamelets(ctx, c, platform, kameletDir)
 }
@@ -84,13 +86,18 @@ func prepareKameletsPermissions(ctx context.Context, c 
client.Client, installing
        return nil
 }
 
-func prepareKameletDirectory() string {
+func prepareKameletDirectory() (string, error) {
        kameletDir := os.Getenv(kameletDirEnv)
        if kameletDir == "" {
                kameletDir = defaultKameletDir
        }
+       // If the directory exists, it is likely a leftover from any previous 
Kamelet
+       // catalog installation. We should remove to be able to proceed
+       if err := os.RemoveAll(kameletDir); err != nil {
+               return kameletDirEnv, err
+       }
 
-       return kameletDir
+       return kameletDir, nil
 }
 
 func downloadKameletDependency(ctx context.Context, version, kameletsDir 
string) error {
diff --git a/pkg/controller/integrationplatform/kamelets_test.go 
b/pkg/controller/integrationplatform/kamelets_test.go
index 7a1a79aa6..0bf31c0c2 100644
--- a/pkg/controller/integrationplatform/kamelets_test.go
+++ b/pkg/controller/integrationplatform/kamelets_test.go
@@ -106,7 +106,8 @@ func TestPrepareKameletsPermissions(t *testing.T) {
 }
 
 func TestPrepareKameletsDirectory(t *testing.T) {
-       kameletDir := prepareKameletDirectory()
+       kameletDir, err := prepareKameletDirectory()
+       assert.NoError(t, err)
        assert.Equal(t, defaultKameletDir, kameletDir)
 }
 

Reply via email to