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

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

commit 656bb06729c41056ed0e7e90c1a554279f468031
Author: Christoph Deppisch <cdeppi...@redhat.com>
AuthorDate: Thu Jun 2 16:49:04 2022 +0200

    Fix #2177: Ensure unique operator id during install
    
    - Make sure to not install same operator id twice
    - Users must use --force option to overwrite existing operator with given id
    - Searches for all integration platforms in any namespace and verify that 
platform name does not exist
---
 pkg/cmd/install.go       | 37 ++++++++++++++++++++++++++++++++-----
 pkg/platform/platform.go | 21 +++++++++++++++++++++
 2 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index 0287d69f5..368eaeb31 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -20,12 +20,14 @@ package cmd
 import (
        "context"
        "fmt"
+       "io"
        "os"
        "regexp"
        "strconv"
        "strings"
        "time"
 
+       platformutil "github.com/apache/camel-k/pkg/platform"
        "github.com/pkg/errors"
        "github.com/spf13/cobra"
        "github.com/spf13/viper"
@@ -289,6 +291,11 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
                        fmt.Fprintln(cobraCmd.OutOrStdout(), "Camel K cluster 
setup completed successfully")
                }
        } else {
+               operatorID, err := getOperatorID(o.EnvVars)
+               if err != nil {
+                       return err
+               }
+
                c, err := o.GetCmdClient()
                if err != nil {
                        return err
@@ -296,7 +303,18 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
 
                namespace := o.Namespace
 
+               platformName := platformutil.DefaultPlatformName
+               if operatorID != "" {
+                       platformName = operatorID
+               }
+
                if !o.SkipOperatorSetup && !installViaOLM {
+                       if ok, err := isInstallAllowed(o.Context, c, 
platformName, o.Force, cobraCmd.OutOrStdout()); err != nil {
+                               return err
+                       } else if !ok {
+                               return errors.New(fmt.Sprintf("installation not 
allowed because operator with id '%s' already exists, use the --force option to 
skip this check", platformName))
+                       }
+
                        cfg := install.OperatorConfiguration{
                                CustomImage:           o.OperatorImage,
                                CustomImagePullPolicy: 
o.OperatorImagePullPolicy,
@@ -343,11 +361,7 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
                        fmt.Fprintln(cobraCmd.OutOrStdout(), "Camel K operator 
registry setup skipped")
                }
 
-               operatorID, err := getOperatorID(o.EnvVars)
-               if err != nil {
-                       return err
-               }
-               platform, err := install.NewPlatform(o.Context, c, 
o.ClusterType, o.SkipRegistrySetup, o.registry, operatorID)
+               platform, err := install.NewPlatform(o.Context, c, 
o.ClusterType, o.SkipRegistrySetup, o.registry, platformName)
                if err != nil {
                        return err
                }
@@ -505,6 +519,19 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
        return nil
 }
 
+func isInstallAllowed(ctx context.Context, c client.Client, operatorID string, 
force bool, out io.Writer) (bool, error) {
+       // find existing platform with given name in any namespace
+       pl, err := platformutil.LookupForPlatformName(ctx, c, operatorID)
+
+       if pl != nil && force {
+               fmt.Fprintln(out, fmt.Sprintf("Overwriting existing operator 
with id '%s'", operatorID))
+               return true, nil
+       }
+
+       // only allow installation when platform with given name is not found
+       return pl == nil, err
+}
+
 func getOperatorID(vars []string) (string, error) {
        envs, _, _, err := env.ParseEnv(vars, nil)
        if err != nil {
diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go
index affba22b6..8d27391e4 100644
--- a/pkg/platform/platform.go
+++ b/pkg/platform/platform.go
@@ -31,6 +31,27 @@ const (
        DefaultPlatformName = "camel-k"
 )
 
+// LookupForPlatformName finds integration platform with given operator id as 
name in any namespace
+func LookupForPlatformName(ctx context.Context, c k8sclient.Reader, name 
string) (*v1.IntegrationPlatform, error) {
+       platformList := v1.NewIntegrationPlatformList()
+
+       // get all integration platform instances on the cluster
+       err := c.List(ctx, &platformList)
+       if err != nil {
+               return nil, err
+       }
+
+       // Check if platform with same name as given operator id already exists
+       for _, pl := range platformList.Items {
+               if pl.Name == name {
+                       // platform already exists installation not allowed
+                       return &pl, nil
+               }
+       }
+
+       return nil, nil
+}
+
 func GetForResource(ctx context.Context, c k8sclient.Reader, o 
k8sclient.Object) (*v1.IntegrationPlatform, error) {
        return GetOrFindForResource(ctx, c, o, true)
 }

Reply via email to