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 a3be4d23bb0d356d6d9795fb0815428dc7bbf09f
Author: Christoph Deppisch <cdeppi...@redhat.com>
AuthorDate: Fri Jun 3 10:17:53 2022 +0200

    Fix #2177: Create proper integration platform as part of the operator
    
    - Make sure that each operator instance adds its integration platform if 
not already exists
    - Automatically adds an integration platform in operator start-up
    - Also adds integration platform viewer role so authenticated users can see 
the platform instance
    - Required for kamel CLI to verify existing operator ids on the cluster 
when doing an operator install or running integrations
---
 pkg/cmd/operator/operator.go | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go
index e87e41ebe..2ae7436e2 100644
--- a/pkg/cmd/operator/operator.go
+++ b/pkg/cmd/operator/operator.go
@@ -29,6 +29,7 @@ import (
        "strings"
        "time"
 
+       "github.com/pkg/errors"
        "go.uber.org/automaxprocs/maxprocs"
 
        appsv1 "k8s.io/api/apps/v1"
@@ -36,6 +37,7 @@ import (
        coordination "k8s.io/api/coordination/v1"
        corev1 "k8s.io/api/core/v1"
        k8serrors "k8s.io/apimachinery/pkg/api/errors"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/fields"
        "k8s.io/apimachinery/pkg/labels"
        "k8s.io/apimachinery/pkg/selection"
@@ -237,11 +239,41 @@ func Run(healthPort, monitoringPort int32, leaderElection 
bool, leaderElectionID
        installCtx, installCancel := context.WithTimeout(context.Background(), 
1*time.Minute)
        defer installCancel()
        install.OperatorStartupOptionalTools(installCtx, c, watchNamespace, 
operatorNamespace, log)
+       exitOnError(findOrCreateIntegrationPlatform(installCtx, c, 
operatorNamespace), "failed to create integration platform")
 
        log.Info("Starting the manager")
        exitOnError(mgr.Start(signals.SetupSignalHandler()), "manager exited 
non-zero")
 }
 
+// findOrCreateIntegrationPlatform create default integration platform in 
operator namespace if not already exists
+func findOrCreateIntegrationPlatform(ctx context.Context, c client.Client, 
operatorNamespace string) error {
+       platformName := defaults.OperatorID()
+       if platformName == "" {
+               platformName = platform.DefaultPlatformName
+       }
+
+       if pl, err := kubernetes.GetIntegrationPlatform(ctx, c, platformName, 
operatorNamespace); pl == nil || k8serrors.IsNotFound(err) {
+               defaultPlatform := v1.NewIntegrationPlatform(operatorNamespace, 
platformName)
+               if defaultPlatform.Labels == nil {
+                       defaultPlatform.Labels = make(map[string]string)
+               }
+               defaultPlatform.Labels["camel.apache.org/platform.generated"] = 
"true"
+
+               if _, err := 
c.CamelV1().IntegrationPlatforms(operatorNamespace).Create(ctx, 
&defaultPlatform, metav1.CreateOptions{}); err != nil {
+                       return err
+               }
+
+               // Make sure that IntegrationPlatform installed in operator 
namespace can be seen by others
+               if err := install.IntegrationPlatformViewerRole(ctx, c, 
operatorNamespace); err != nil && !k8serrors.IsAlreadyExists(err) {
+                       return errors.Wrap(err, "Error while installing global 
IntegrationPlatform viewer role")
+               }
+       } else {
+               return err
+       }
+
+       return nil
+}
+
 // getWatchNamespace returns the Namespace the operator should be watching for 
changes.
 func getWatchNamespace() (string, error) {
        ns, found := os.LookupEnv(platform.OperatorWatchNamespaceEnvVariable)

Reply via email to