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 ee137fedc1a247b82719e5be5cf36229e792ee93
Author: Christoph Deppisch <[email protected]>
AuthorDate: Wed Jun 8 10:42:00 2022 +0200

    Fix #2177: Use operator id by default
    
    - By default use an operator-id (default='camel-k')
    - Introduce operator-id setting on kamel CLI when installing Camel K
    - Introduce operator-id setting on kamel CLI when running integrations
    - Make sure to use an operator-id when installing Camel K and running 
integrations
    - Set camel.apache.org/operator.id annotation on resources managed by 
operator with given id
    - Add operator id as environment variable to integration pods
    - Use default operator id in OLM deployment
    - Add more comfortable way to set operator id annotation on resources
---
 config/manager/operator-deployment.yaml                |  2 ++
 e2e/support/test_support.go                            |  4 ++--
 pkg/apis/camel/v1/integration_types_support.go         |  9 +++++++++
 pkg/apis/camel/v1/integrationkit_types_support.go      |  9 +++++++++
 pkg/apis/camel/v1/integrationplatform_types_support.go |  9 +++++++++
 pkg/cmd/install.go                                     | 11 ++++++++++-
 pkg/cmd/run.go                                         | 16 +++++++++++++---
 pkg/cmd/run_test.go                                    |  4 ++++
 pkg/install/operator.go                                |  7 ++++---
 pkg/resources/resources.go                             |  4 ++--
 pkg/trait/container.go                                 |  2 +-
 pkg/trait/environment.go                               |  2 ++
 pkg/trait/quarkus.go                                   |  2 +-
 13 files changed, 68 insertions(+), 13 deletions(-)

diff --git a/config/manager/operator-deployment.yaml 
b/config/manager/operator-deployment.yaml
index 15311da44..36b75343b 100644
--- a/config/manager/operator-deployment.yaml
+++ b/config/manager/operator-deployment.yaml
@@ -61,6 +61,8 @@ spec:
                   fieldPath: metadata.namespace
             - name: OPERATOR_NAME
               value: "camel-k"
+            - name: OPERATOR_ID
+              value: "camel-k"
             - name: POD_NAME
               valueFrom:
                 fieldRef:
diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go
index 967580303..b7ab45f15 100644
--- a/e2e/support/test_support.go
+++ b/e2e/support/test_support.go
@@ -545,7 +545,7 @@ func AssignIntegrationToOperator(ns, name, operator string) 
error {
        if it.Annotations == nil {
                it.Annotations = make(map[string]string)
        }
-       it.Annotations[v1.OperatorIDAnnotation] = operator
+       it.SetOperatorID(operator)
        return TestClient().Update(TestContext, it)
 }
 
@@ -1300,7 +1300,7 @@ func AssignPlatformToOperator(ns, operator string) error {
        if pl.Annotations == nil {
                pl.Annotations = make(map[string]string)
        }
-       pl.Annotations[v1.OperatorIDAnnotation] = operator
+       pl.SetOperatorID(operator)
        return TestClient().Update(TestContext, pl)
 }
 
diff --git a/pkg/apis/camel/v1/integration_types_support.go 
b/pkg/apis/camel/v1/integration_types_support.go
index 360c1df48..9d137de41 100644
--- a/pkg/apis/camel/v1/integration_types_support.go
+++ b/pkg/apis/camel/v1/integration_types_support.go
@@ -255,6 +255,15 @@ func (in *SourceSpec) InferLanguage() Language {
        return ""
 }
 
+// SetOperatorID sets the given operator id as an annotation
+func (in *Integration) SetOperatorID(operatorID string) {
+       if in.Annotations == nil {
+               in.Annotations = make(map[string]string)
+       }
+
+       in.Annotations[OperatorIDAnnotation] = operatorID
+}
+
 func (in *Integration) SetIntegrationPlatform(platform *IntegrationPlatform) {
        cs := corev1.ConditionTrue
 
diff --git a/pkg/apis/camel/v1/integrationkit_types_support.go 
b/pkg/apis/camel/v1/integrationkit_types_support.go
index 408a88416..5862805f9 100644
--- a/pkg/apis/camel/v1/integrationkit_types_support.go
+++ b/pkg/apis/camel/v1/integrationkit_types_support.go
@@ -54,6 +54,15 @@ func (in *IntegrationKitSpec) Configurations() 
[]ConfigurationSpec {
        return in.Configuration
 }
 
+// SetOperatorID sets the given operator id as an annotation
+func (in *IntegrationKit) SetOperatorID(operatorID string) {
+       if in.Annotations == nil {
+               in.Annotations = make(map[string]string)
+       }
+
+       in.Annotations[OperatorIDAnnotation] = operatorID
+}
+
 func (in *IntegrationKit) Configurations() []ConfigurationSpec {
        if in == nil {
                return []ConfigurationSpec{}
diff --git a/pkg/apis/camel/v1/integrationplatform_types_support.go 
b/pkg/apis/camel/v1/integrationplatform_types_support.go
index b644f462e..faa287750 100644
--- a/pkg/apis/camel/v1/integrationplatform_types_support.go
+++ b/pkg/apis/camel/v1/integrationplatform_types_support.go
@@ -68,6 +68,15 @@ func (in *IntegrationPlatformSpec) Configurations() 
[]ConfigurationSpec {
        return in.Configuration
 }
 
+// SetOperatorID sets the given operator id as an annotation
+func (in *IntegrationPlatform) SetOperatorID(operatorID string) {
+       if in.Annotations == nil {
+               in.Annotations = make(map[string]string)
+       }
+
+       in.Annotations[OperatorIDAnnotation] = operatorID
+}
+
 // Configurations --
 func (in *IntegrationPlatform) Configurations() []ConfigurationSpec {
        if in == nil {
diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index 89d36621a..0287d69f5 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -101,6 +101,7 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) 
(*cobra.Command, *installCmdO
        cmd.Flags().StringArrayP("property", "p", nil, "Add a camel property")
        cmd.Flags().String("runtime-version", "", "Set the camel-k runtime 
version")
        cmd.Flags().String("base-image", "", "Set the base Image used to run 
integrations")
+       cmd.Flags().String("operator-id", "camel-k", "Set the operator id that 
is used to select the resources this operator should manage")
        cmd.Flags().String("operator-image", "", "Set the operator Image used 
for the operator deployment")
        cmd.Flags().String("operator-image-pull-policy", "", "Set the operator 
ImagePullPolicy used for the operator deployment")
        cmd.Flags().String("build-strategy", "", "Set the build strategy")
@@ -177,6 +178,7 @@ type installCmdOptions struct {
        OutputFormat             string   `mapstructure:"output"`
        RuntimeVersion           string   `mapstructure:"runtime-version"`
        BaseImage                string   `mapstructure:"base-image"`
+       OperatorId               string   `mapstructure:"operator-id"`
        OperatorImage            string   `mapstructure:"operator-image"`
        OperatorImagePullPolicy  string   
`mapstructure:"operator-image-pull-policy"`
        BuildStrategy            string   `mapstructure:"build-strategy"`
@@ -216,6 +218,9 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
        // Let's use a client provider during cluster installation, to 
eliminate the problem of CRD object caching
        clientProvider := client.Provider{Get: o.NewCmdClient}
 
+       // --operator-id={id} is a syntax sugar for '--operator-env-vars 
KAMEL_OPERATOR_ID={id}'
+       o.EnvVars = append(o.EnvVars, fmt.Sprintf("KAMEL_OPERATOR_ID=%s", 
strings.TrimSpace(o.OperatorId)))
+
        // --skip-default-kamelets-setup is a syntax sugar for 
'--operator-env-vars KAMEL_INSTALL_DEFAULT_KAMELETS=false'
        if o.SkipDefaultKameletsSetup {
                o.EnvVars = append(o.EnvVars, 
"KAMEL_INSTALL_DEFAULT_KAMELETS=false")
@@ -342,7 +347,7 @@ func (o *installCmdOptions) install(cobraCmd 
*cobra.Command, _ []string) error {
                if err != nil {
                        return err
                }
-               platform, err := install.PlatformOrCollect(o.Context, c, 
o.ClusterType, namespace, o.SkipRegistrySetup, o.registry, collection, 
operatorID)
+               platform, err := install.NewPlatform(o.Context, c, 
o.ClusterType, o.SkipRegistrySetup, o.registry, operatorID)
                if err != nil {
                        return err
                }
@@ -600,6 +605,10 @@ func (o *installCmdOptions) decode(cmd *cobra.Command, _ 
[]string) error {
 func (o *installCmdOptions) validate(_ *cobra.Command, _ []string) error {
        var result error
 
+       if o.OperatorId == "" {
+               result = multierr.Append(result, fmt.Errorf("cannot use empty 
operator id"))
+       }
+
        if len(o.MavenRepositories) > 0 && o.MavenSettings != "" {
                err := fmt.Errorf("incompatible options combinations: you 
cannot set both mavenRepository and mavenSettings")
                result = multierr.Append(result, err)
diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go
index a37ac19cd..fd1e5c53f 100644
--- a/pkg/cmd/run.go
+++ b/pkg/cmd/run.go
@@ -106,6 +106,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) 
(*cobra.Command, *runCmdOptions)
        cmd.Flags().Bool("sync", false, "Synchronize the local source file with 
the cluster, republishing at each change")
        cmd.Flags().Bool("dev", false, "Enable Dev mode (equivalent to \"-w 
--logs --sync\")")
        cmd.Flags().Bool("use-flows", true, "Write yaml sources as Flow objects 
in the integration custom resource")
+       cmd.Flags().String("operator-id", "camel-k", "Operator id selected to 
manage this integration.")
        cmd.Flags().String("profile", "", "Trait profile used for deployment")
        cmd.Flags().StringArrayP("trait", "t", nil, "Configure a trait. E.g. 
\"-t service.enabled=false\"")
        cmd.Flags().StringP("output", "o", "", "Output format. One of: 
json|yaml")
@@ -138,6 +139,7 @@ type runCmdOptions struct {
        IntegrationKit  string   `mapstructure:"kit" yaml:",omitempty"`
        IntegrationName string   `mapstructure:"name" yaml:",omitempty"`
        Profile         string   `mapstructure:"profile" yaml:",omitempty"`
+       OperatorId      string   `mapstructure:"operator-id" yaml:",omitempty"`
        OutputFormat    string   `mapstructure:"output" yaml:",omitempty"`
        PodTemplate     string   `mapstructure:"pod-template" yaml:",omitempty"`
        Connects        []string `mapstructure:"connects" yaml:",omitempty"`
@@ -240,6 +242,10 @@ func (o *runCmdOptions) validateArgs(cmd *cobra.Command, 
args []string) error {
 }
 
 func (o *runCmdOptions) validate() error {
+       if o.OperatorId == "" {
+               return fmt.Errorf("cannot use empty operator id")
+       }
+
        for _, volume := range o.Volumes {
                volumeConfig := strings.Split(volume, ":")
                if len(volumeConfig) != 2 || 
len(strings.TrimSpace(volumeConfig[0])) == 0 || 
len(strings.TrimSpace(volumeConfig[1])) == 0 {
@@ -540,12 +546,16 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd 
*cobra.Command, c client.C
                }
        }
 
+       if integration.Annotations == nil {
+               integration.Annotations = make(map[string]string)
+       }
+
+       // --operator-id={id} is a syntax sugar for '--annotation 
camel.apache.org/operator.id={id}'
+       integration.SetOperatorID(strings.TrimSpace(o.OperatorId))
+
        for _, annotation := range o.Annotations {
                parts := strings.SplitN(annotation, "=", 2)
                if len(parts) == 2 {
-                       if integration.Annotations == nil {
-                               integration.Annotations = 
make(map[string]string)
-                       }
                        integration.Annotations[parts[0]] = parts[1]
                }
        }
diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go
index 4e1fc28a2..fff96f88d 100644
--- a/pkg/cmd/run_test.go
+++ b/pkg/cmd/run_test.go
@@ -566,6 +566,8 @@ func TestOutputYaml(t *testing.T) {
        assert.Equal(t, fmt.Sprintf(`apiVersion: camel.apache.org/v1
 kind: Integration
 metadata:
+  annotations:
+    camel.apache.org/operator.id: camel-k
   creationTimestamp: null
   name: %s
 spec:
@@ -598,6 +600,8 @@ func TestTrait(t *testing.T) {
        assert.Equal(t, fmt.Sprintf(`apiVersion: camel.apache.org/v1
 kind: Integration
 metadata:
+  annotations:
+    camel.apache.org/operator.id: camel-k
   creationTimestamp: null
   name: %s
 spec:
diff --git a/pkg/install/operator.go b/pkg/install/operator.go
index 70b1ae26c..7ce9ad75f 100644
--- a/pkg/install/operator.go
+++ b/pkg/install/operator.go
@@ -508,9 +508,9 @@ func installLeaseBindings(ctx context.Context, c 
client.Client, namespace string
        )
 }
 
-// PlatformOrCollect --
+// NewPlatform --
 // nolint: lll
-func PlatformOrCollect(ctx context.Context, c client.Client, clusterType 
string, namespace string, skipRegistrySetup bool, registry v1.RegistrySpec, 
collection *kubernetes.Collection, operatorID string) (*v1.IntegrationPlatform, 
error) {
+func NewPlatform(ctx context.Context, c client.Client, clusterType string, 
skipRegistrySetup bool, registry v1.RegistrySpec, operatorID string) 
(*v1.IntegrationPlatform, error) {
        isOpenShift, err := isOpenShift(c, clusterType)
        if err != nil {
                return nil, err
@@ -536,7 +536,8 @@ func PlatformOrCollect(ctx context.Context, c 
client.Client, clusterType string,
                if pl.Annotations == nil {
                        pl.Annotations = make(map[string]string)
                }
-               pl.Annotations[v1.OperatorIDAnnotation] = operatorID
+               pl.SetOperatorID(operatorID)
+               pl.Name = operatorID
        }
 
        if !skipRegistrySetup {
diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go
index af230eb89..2b685329a 100644
--- a/pkg/resources/resources.go
+++ b/pkg/resources/resources.go
@@ -170,9 +170,9 @@ var assets = func() http.FileSystem {
                "/manager/operator-deployment.yaml": &vfsgen۰CompressedFileInfo{
                        name:             "operator-deployment.yaml",
                        modTime:          time.Time{},
-                       uncompressedSize: 2689,
+                       uncompressedSize: 2752,
 
-                       compressedContent: 
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x56\xcf\x6f\xe2\x46\x14\xbe\xfb\xaf\xf8\x84\x2f\xbb\x52\x30\xb0\xa7\x95\x7b\x72\x13\xd2\xa0\xa6\x06\x61\xb6\xd1\x9e\xaa\x61\xfc\xb0\x47\x8c\x67\xdc\x99\x31\x2c\xfd\xeb\xab\x31\x98\x00\x49\x68\x23\x45\x5a\x9f\x82\xdf\x9b\xef\x7d\x3f\x9e\xed\x84\xe8\x7f\xdc\x15\x84\x78\x14\x9c\x94\xa5\x1c\x4e\xc3\x95\x84\xa4\x66\xbc\x24\x64\x7a\xe5\xb6\xcc\x10\xee\x75\xa3\x72\xe6\x84\x56\xf8\x94\x64\xf7\x9f\xd1\xa8\x9c\x0c\xb4\x22\x
 [...]
+                       compressedContent: 
[]byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x56\x4d\x8f\xe2\x46\x10\xbd\xfb\x57\x3c\xe1\xcb\xae\x34\x7c\xed\x69\xe5\x9c\x9c\x81\xc9\x58\x99\x18\x84\xd9\x8c\xf6\x14\x35\xed\xc2\x6e\xd1\xee\x76\xba\xdb\xb0\xe4\xd7\x47\x6d\x30\x03\xcc\x47\x32\xd1\x48\xf1\x09\xbb\xaa\x5e\xbd\x7a\xaf\xda\x26\x44\xff\xe3\xae\x20\xc4\x83\xe0\xa4\x2c\xe5\x70\x1a\xae\x24\xc4\x35\xe3\x25\x21\xd3\x6b\xb7\x63\x86\x70\xa7\x1b\x95\x33\x27\xb4\xc2\xa7\x38\xbb\xfb\x8c\x46\xe5\x64\xa0\x15\x
 [...]
                },
                "/manager/operator-service-account.yaml": 
&vfsgen۰CompressedFileInfo{
                        name:             "operator-service-account.yaml",
diff --git a/pkg/trait/container.go b/pkg/trait/container.go
index dc510701a..522120218 100644
--- a/pkg/trait/container.go
+++ b/pkg/trait/container.go
@@ -136,7 +136,7 @@ func (t *containerTrait) configureImageIntegrationKit(e 
*Environment) error {
                }
                operatorID := defaults.OperatorID()
                if operatorID != "" {
-                       kit.Annotations[v1.OperatorIDAnnotation] = operatorID
+                       kit.SetOperatorID(operatorID)
                }
 
                t.L.Infof("image %s", kit.Spec.Image)
diff --git a/pkg/trait/environment.go b/pkg/trait/environment.go
index a9ed40a70..7155a7552 100644
--- a/pkg/trait/environment.go
+++ b/pkg/trait/environment.go
@@ -37,6 +37,7 @@ type environmentTrait struct {
 const (
        envVarNamespace            = "NAMESPACE"
        envVarPodName              = "POD_NAME"
+       envVarOperatorId           = "CAMEL_K_OPERATOR_ID"
        envVarCamelKVersion        = "CAMEL_K_VERSION"
        envVarCamelKIntegration    = "CAMEL_K_INTEGRATION"
        envVarCamelKRuntimeVersion = "CAMEL_K_RUNTIME_VERSION"
@@ -70,6 +71,7 @@ func (t *environmentTrait) Configure(e *Environment) (bool, 
error) {
 
 func (t *environmentTrait) Apply(e *Environment) error {
        envvar.SetVal(&e.EnvVars, envVarCamelKVersion, defaults.Version)
+       envvar.SetVal(&e.EnvVars, envVarOperatorId, defaults.OperatorID())
        if e.Integration != nil {
                envvar.SetVal(&e.EnvVars, envVarCamelKIntegration, 
e.Integration.Name)
        }
diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go
index 0951fd3ad..1ad680634 100644
--- a/pkg/trait/quarkus.go
+++ b/pkg/trait/quarkus.go
@@ -202,7 +202,7 @@ func (t *quarkusTrait) newIntegrationKit(e *Environment, 
packageType traitv1.Qua
        }
        operatorID := defaults.OperatorID()
        if operatorID != "" {
-               kit.Annotations[v1.OperatorIDAnnotation] = operatorID
+               kit.SetOperatorID(operatorID)
        }
 
        kit.Spec = v1.IntegrationKitSpec{

Reply via email to