Skip to content

Commit

Permalink
fix(kamelets): don't fail on Kamelets recreation
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Sep 14, 2024
1 parent dcb7f36 commit fa10c8d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
21 changes: 10 additions & 11 deletions pkg/controller/integrationplatform/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion pkg/controller/integrationplatform/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
15 changes: 11 additions & 4 deletions pkg/controller/integrationplatform/kamelets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/integrationplatform/kamelets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit fa10c8d

Please sign in to comment.