You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2021/10/21 16:12:13 UTC

[camel-k] 04/09: Fix #2687: use annotations for both operator and platform

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

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

commit 59466e182d2e56fd3bf02861d79dd0b2dfec8d48
Author: nicolaferraro <ni...@gmail.com>
AuthorDate: Thu Oct 14 13:24:07 2021 +0200

    Fix #2687: use annotations for both operator and platform
---
 e2e/support/test_support.go            | 18 +++++++++---------
 pkg/apis/camel/v1/common_types.go      |  9 +++++----
 pkg/controller/integrationkit/build.go | 13 ++++++-------
 pkg/platform/operator.go               |  4 ++--
 pkg/platform/platform.go               | 23 +++++++++++++++--------
 pkg/trait/container.go                 | 13 +++++++------
 pkg/trait/quarkus.go                   | 13 +++++++------
 7 files changed, 51 insertions(+), 42 deletions(-)

diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go
index 60fce20..64dbf5c 100644
--- a/e2e/support/test_support.go
+++ b/e2e/support/test_support.go
@@ -428,10 +428,10 @@ func AssignIntegrationToOperator(ns, name, operator string) error {
 	if it == nil {
 		return fmt.Errorf("cannot assign integration %q to operator: integration not found", name)
 	}
-	if it.Labels == nil {
-		it.Labels = make(map[string]string)
+	if it.Annotations == nil {
+		it.Annotations = make(map[string]string)
 	}
-	it.Labels[v1.OperatorIDLabel] = operator
+	it.Annotations[v1.OperatorIDAnnotation] = operator
 	return TestClient().Update(TestContext, it)
 }
 
@@ -745,10 +745,10 @@ func AssignKameletBindingToOperator(ns, name, operator string) error {
 	if klb == nil {
 		return fmt.Errorf("cannot assign kamelet binding %q to operator: kamelet binding not found", name)
 	}
-	if klb.Labels == nil {
-		klb.Labels = make(map[string]string)
+	if klb.Annotations == nil {
+		klb.Annotations = make(map[string]string)
 	}
-	klb.Labels[v1.OperatorIDLabel] = operator
+	klb.Annotations[v1.OperatorIDAnnotation] = operator
 	return TestClient().Update(TestContext, klb)
 }
 
@@ -1073,10 +1073,10 @@ func AssignPlatformToOperator(ns, operator string) error {
 	if pl == nil {
 		return errors.New("cannot assign platform to operator: no platform found")
 	}
-	if pl.Labels == nil {
-		pl.Labels = make(map[string]string)
+	if pl.Annotations == nil {
+		pl.Annotations = make(map[string]string)
 	}
-	pl.Labels[v1.OperatorIDLabel] = operator
+	pl.Annotations[v1.OperatorIDAnnotation] = operator
 	return TestClient().Update(TestContext, pl)
 }
 
diff --git a/pkg/apis/camel/v1/common_types.go b/pkg/apis/camel/v1/common_types.go
index 571f547..45e9f91 100644
--- a/pkg/apis/camel/v1/common_types.go
+++ b/pkg/apis/camel/v1/common_types.go
@@ -23,10 +23,11 @@ import (
 )
 
 const (
-	TraitAnnotationPrefix      = "trait.camel.apache.org/"
-	OperatorIDLabel            = "camel.apache.org/operator.id"
-	SecondaryPlatformLabel     = "camel.apache.org/secondary.platform"
-	PlatformSelectorAnnotation = "camel.apache.org/platform.id"
+	TraitAnnotationPrefix = "trait.camel.apache.org/"
+
+	OperatorIDAnnotation        = "camel.apache.org/operator.id"
+	SecondaryPlatformAnnotation = "camel.apache.org/secondary.platform"
+	PlatformSelectorAnnotation  = "camel.apache.org/platform.id"
 )
 
 // BuildStrategy specifies how the Build should be executed
diff --git a/pkg/controller/integrationkit/build.go b/pkg/controller/integrationkit/build.go
index cb74cd0..47fa5c6 100644
--- a/pkg/controller/integrationkit/build.go
+++ b/pkg/controller/integrationkit/build.go
@@ -86,15 +86,14 @@ func (action *buildAction) handleBuildSubmitted(ctx context.Context, kit *v1.Int
 
 		labels := kubernetes.FilterCamelCreatorLabels(kit.Labels)
 		labels[v1.IntegrationKitLayoutLabel] = kit.Labels[v1.IntegrationKitLayoutLabel]
+
+		var annotations = make(map[string]string)
+		if v, ok := kit.Annotations[v1.PlatformSelectorAnnotation]; ok {
+			annotations[v1.PlatformSelectorAnnotation] = v
+		}
 		operatorID := defaults.OperatorID()
 		if operatorID != "" {
-			labels[v1.OperatorIDLabel] = operatorID
-		}
-		var annotations map[string]string
-		if v, ok := kit.Annotations[v1.PlatformSelectorAnnotation]; ok {
-			annotations = map[string]string{
-				v1.PlatformSelectorAnnotation: v,
-			}
+			annotations[v1.OperatorIDAnnotation] = operatorID
 		}
 
 		timeout := env.Platform.Status.Build.GetTimeout()
diff --git a/pkg/platform/operator.go b/pkg/platform/operator.go
index d34c898..d8efd79 100644
--- a/pkg/platform/operator.go
+++ b/pkg/platform/operator.go
@@ -108,7 +108,7 @@ func IsOperatorHandler(object ctrl.Object) bool {
 	if object == nil {
 		return true
 	}
-	resourceID := object.GetLabels()[camelv1.OperatorIDLabel]
+	resourceID := object.GetAnnotations()[camelv1.OperatorIDAnnotation]
 	operatorID := defaults.OperatorID()
 	return resourceID == operatorID
 }
@@ -155,7 +155,7 @@ func (f FilteringFuncs) Update(e event.UpdateEvent) bool {
 		return false
 	}
 	if e.ObjectOld != nil && e.ObjectNew != nil &&
-		e.ObjectOld.GetLabels()[camelv1.OperatorIDLabel] != e.ObjectNew.GetLabels()[camelv1.OperatorIDLabel] {
+		e.ObjectOld.GetAnnotations()[camelv1.OperatorIDAnnotation] != e.ObjectNew.GetAnnotations()[camelv1.OperatorIDAnnotation] {
 		// Always force reconciliation when the object becomes managed by the current operator
 		return true
 	}
diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go
index e7e0774..73b365f 100644
--- a/pkg/platform/platform.go
+++ b/pkg/platform/platform.go
@@ -26,8 +26,6 @@ import (
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 	corev1 "k8s.io/api/core/v1"
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
-	"k8s.io/apimachinery/pkg/labels"
-	"k8s.io/apimachinery/pkg/selection"
 	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 )
 
@@ -167,15 +165,24 @@ func findLocal(ctx context.Context, c k8sclient.Reader, namespace string, active
 
 // ListPrimaryPlatforms returns all non-secondary platforms installed in a given namespace (only one will be active)
 func ListPrimaryPlatforms(ctx context.Context, c k8sclient.Reader, namespace string) (*v1.IntegrationPlatformList, error) {
-	lst := v1.NewIntegrationPlatformList()
-	primaryPlatform, err := labels.NewRequirement(v1.SecondaryPlatformLabel, selection.DoesNotExist, []string{})
+	lst, err := ListAllPlatforms(ctx, c, namespace)
 	if err != nil {
 		return nil, err
 	}
-	opt := k8sclient.MatchingLabelsSelector{
-		labels.NewSelector().Add(*primaryPlatform),
+
+	filtered := v1.NewIntegrationPlatformList()
+	for _, pl := range lst.Items {
+		if val, present := pl.Annotations[v1.SecondaryPlatformAnnotation]; !present || val != "true" {
+			filtered.Items = append(filtered.Items, pl)
+		}
 	}
-	if err := c.List(ctx, &lst, k8sclient.InNamespace(namespace), opt); err != nil {
+	return &filtered, nil
+}
+
+// ListAllPlatforms returns all platforms installed in a given namespace
+func ListAllPlatforms(ctx context.Context, c k8sclient.Reader, namespace string) (*v1.IntegrationPlatformList, error) {
+	lst := v1.NewIntegrationPlatformList()
+	if err := c.List(ctx, &lst, k8sclient.InNamespace(namespace)); err != nil {
 		return nil, err
 	}
 	return &lst, nil
@@ -188,7 +195,7 @@ func IsActive(p *v1.IntegrationPlatform) bool {
 
 // IsSecondary determines if the given platform is marked as secondary
 func IsSecondary(p *v1.IntegrationPlatform) bool {
-	if l, ok := p.Labels[v1.SecondaryPlatformLabel]; ok && l == "true" {
+	if l, ok := p.Annotations[v1.SecondaryPlatformAnnotation]; ok && l == "true" {
 		return true
 	}
 	return false
diff --git a/pkg/trait/container.go b/pkg/trait/container.go
index 00c9826..bf9316f 100644
--- a/pkg/trait/container.go
+++ b/pkg/trait/container.go
@@ -216,14 +216,15 @@ func (t *containerTrait) configureImageIntegrationKit(e *Environment) error {
 			kubernetes.CamelCreatorLabelVersion:   e.Integration.ResourceVersion,
 		}
 
-		operatorID := defaults.OperatorID()
-		if operatorID != "" {
-			kit.Labels[v1.OperatorIDLabel] = operatorID
+		if kit.Annotations == nil {
+			kit.Annotations = make(map[string]string)
 		}
 		if v, ok := e.Integration.Annotations[v1.PlatformSelectorAnnotation]; ok {
-			kit.Annotations = map[string]string{
-				v1.PlatformSelectorAnnotation: v,
-			}
+			kit.Annotations[v1.PlatformSelectorAnnotation] = v
+		}
+		operatorID := defaults.OperatorID()
+		if operatorID != "" {
+			kit.Annotations[v1.OperatorIDAnnotation] = operatorID
 		}
 
 		t.L.Infof("image %s", kit.Spec.Image)
diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go
index 1c66527..327afcc 100644
--- a/pkg/trait/quarkus.go
+++ b/pkg/trait/quarkus.go
@@ -261,14 +261,15 @@ func (t *quarkusTrait) newIntegrationKit(e *Environment, packageType quarkusPack
 		kubernetes.CamelCreatorLabelVersion:   integration.ResourceVersion,
 	}
 
-	operatorID := defaults.OperatorID()
-	if operatorID != "" {
-		kit.Labels[v1.OperatorIDLabel] = operatorID
+	if kit.Annotations == nil {
+		kit.Annotations = make(map[string]string)
 	}
 	if v, ok := integration.Annotations[v1.PlatformSelectorAnnotation]; ok {
-		kit.Annotations = map[string]string{
-			v1.PlatformSelectorAnnotation: v,
-		}
+		kit.Annotations[v1.PlatformSelectorAnnotation] = v
+	}
+	operatorID := defaults.OperatorID()
+	if operatorID != "" {
+		kit.Annotations[v1.OperatorIDAnnotation] = operatorID
 	}
 
 	traits := t.getKitTraits(e)