You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2022/07/11 15:30:01 UTC

[camel-k] 06/17: Fix #2177: Ensure valid operator id when running integrations

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 417b7b873f4aea59488cddfcc9ddf2f548248194
Author: Christoph Deppisch <cd...@redhat.com>
AuthorDate: Thu Jun 2 16:53:53 2022 +0200

    Fix #2177: Ensure valid operator id when running integrations
    
    - Make sure to use a known operator id when running an integration
    - Check performs a cluster wide search for integration platforms and verifies that the platform name exists
    - Users may use --force option to skip the check
---
 pkg/cmd/root_test.go | 11 ++++++++---
 pkg/cmd/run.go       | 20 +++++++++++++++++++-
 pkg/cmd/run_test.go  |  6 +++++-
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/pkg/cmd/root_test.go b/pkg/cmd/root_test.go
index 8fe884d0d..d2b4eb7d5 100644
--- a/pkg/cmd/root_test.go
+++ b/pkg/cmd/root_test.go
@@ -23,6 +23,7 @@ import (
 	"os"
 	"testing"
 
+	"github.com/apache/camel-k/pkg/client"
 	"github.com/apache/camel-k/pkg/util/test"
 	"github.com/spf13/cobra"
 	"github.com/spf13/viper"
@@ -37,17 +38,21 @@ func kamelTestPostAddCommandInit(t *testing.T, rootCmd *cobra.Command) {
 	}
 }
 
-func kamelTestPreAddCommandInit() (*RootCmdOptions, *cobra.Command) {
-	fakeClient, _ := test.NewFakeClient()
+func kamelTestPreAddCommandInitWithClient(client client.Client) (*RootCmdOptions, *cobra.Command) {
 	options := RootCmdOptions{
 		Context: context.Background(),
-		_client: fakeClient,
+		_client: client,
 	}
 	rootCmd := kamelPreAddCommandInit(&options)
 	rootCmd.Run = test.EmptyRun
 	return &options, rootCmd
 }
 
+func kamelTestPreAddCommandInit() (*RootCmdOptions, *cobra.Command) {
+	fakeClient, _ := test.NewFakeClient()
+	return kamelTestPreAddCommandInitWithClient(fakeClient)
+}
+
 func TestLoadFromEnvVar(t *testing.T) {
 	defer teardown(t)
 	// shows how to include a "," character inside an env value see VAR1 value
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index fd1e5c53f..a3ad6b6e1 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -118,6 +118,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions)
 	cmd.Flags().StringArray("label", nil, "Add a label to the integration. E.g. \"--label my.company=hello\"")
 	cmd.Flags().StringArray("source", nil, "Add source file to your integration, this is added to the list of files listed as arguments of the command")
 	cmd.Flags().String("pod-template", "", "The path of the YAML file containing a PodSpec template to be used for the Integration pods")
+	cmd.Flags().Bool("force", false, "Force creation of integration regardless of potential misconfiguration.")
 
 	cmd.Flags().Bool("save", false, "Save the run parameters into the default kamel configuration file (kamel-config.yaml)")
 
@@ -157,11 +158,12 @@ type runCmdOptions struct {
 	Annotations     []string `mapstructure:"annotations" yaml:",omitempty"`
 	Sources         []string `mapstructure:"sources" yaml:",omitempty"`
 	RegistryOptions url.Values
+	Force           bool `mapstructure:"force" yaml:",omitempty"`
 }
 
 func (o *runCmdOptions) preRunE(cmd *cobra.Command, args []string) error {
 	if o.OutputFormat != "" {
-		// let the command to work in offline mode
+		// let the command work in offline mode
 		cmd.Annotations[offlineCommandLabel] = "true"
 	}
 	return o.RootCmdOptions.preRun(cmd, args)
@@ -550,6 +552,22 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
 		integration.Annotations = make(map[string]string)
 	}
 
+	if o.OperatorId != "" {
+		if pl, err := platformutil.LookupForPlatformName(o.Context, c, o.OperatorId); err != nil {
+			if k8serrors.IsForbidden(err) {
+				o.PrintfVerboseOutf(cmd, "Unable to verify existence of operator id [%s] due to lack of user privileges\n", o.OperatorId)
+			} else {
+				return nil, err
+			}
+		} else if pl == nil {
+			if o.Force {
+				o.PrintfVerboseOutf(cmd, "Unable to find operator with given id [%s] - integration may not be reconciled and get stuck in waiting state\n", o.OperatorId)
+			} else {
+				return nil, errors.New(fmt.Sprintf("unable to find integration platform for given operator id '%s', use --force option or make sure to use a proper operator id", o.OperatorId))
+			}
+		}
+	}
+
 	// --operator-id={id} is a syntax sugar for '--annotation camel.apache.org/operator.id={id}'
 	integration.SetOperatorID(strings.TrimSpace(o.OperatorId))
 
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index fff96f88d..d53516400 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -27,6 +27,7 @@ import (
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	traitv1 "github.com/apache/camel-k/pkg/apis/camel/v1/trait"
+	"github.com/apache/camel-k/pkg/platform"
 	"github.com/apache/camel-k/pkg/trait"
 	"github.com/apache/camel-k/pkg/util/test"
 
@@ -55,7 +56,10 @@ func initializeRunCmdOptions(t *testing.T) (*runCmdOptions, *cobra.Command, Root
 func initializeRunCmdOptionsWithOutput(t *testing.T) (*runCmdOptions, *cobra.Command, RootCmdOptions) {
 	t.Helper()
 
-	options, rootCmd := kamelTestPreAddCommandInit()
+	defaultIntegrationPlatform := v1.NewIntegrationPlatform("default", platform.DefaultPlatformName)
+	fakeClient, _ := test.NewFakeClient(&defaultIntegrationPlatform)
+
+	options, rootCmd := kamelTestPreAddCommandInitWithClient(fakeClient)
 	runCmdOptions := addTestRunCmdWithOutput(*options, rootCmd)
 	kamelTestPostAddCommandInit(t, rootCmd)