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

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


The following commit(s) were added to refs/heads/main by this push:
     new 250b3c1af fix(cmd): promote correct labels
250b3c1af is described below

commit 250b3c1afc384e7d76a47cb0520d197f29a0d723
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri May 31 11:48:37 2024 +0200

    fix(cmd): promote correct labels
    
    Closes #5541
---
 e2e/advanced/operator_id_filtering_test.go |  2 +-
 e2e/install/cli/global_test.go             |  2 +-
 pkg/cmd/promote.go                         | 29 ++++++++++-
 pkg/cmd/promote_test.go                    | 83 ++++++++++++++++++++++++++++++
 pkg/util/kubernetes/camel.go               |  6 +++
 5 files changed, 118 insertions(+), 4 deletions(-)

diff --git a/e2e/advanced/operator_id_filtering_test.go 
b/e2e/advanced/operator_id_filtering_test.go
index b0c0cbfc1..074c90c21 100644
--- a/e2e/advanced/operator_id_filtering_test.go
+++ b/e2e/advanced/operator_id_filtering_test.go
@@ -105,7 +105,7 @@ func TestOperatorIDFiltering(t *testing.T) {
                                                        Namespace: ns,
                                                        Name:      "external",
                                                        Labels: 
map[string]string{
-                                                               
"camel.apache.org/kit.type": v1.IntegrationKitTypeExternal,
+                                                               
v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeExternal,
                                                        },
                                                        Annotations: 
map[string]string{
                                                                
"camel.apache.org/operator.id": operator2,
diff --git a/e2e/install/cli/global_test.go b/e2e/install/cli/global_test.go
index cff223304..1ff3dd114 100644
--- a/e2e/install/cli/global_test.go
+++ b/e2e/install/cli/global_test.go
@@ -133,7 +133,7 @@ func TestRunGlobalInstall(t *testing.T) {
                                                Namespace: ns5,
                                                Name:      "external",
                                                Labels: map[string]string{
-                                                       
"camel.apache.org/kit.type": v1.IntegrationKitTypeExternal,
+                                                       
v1.IntegrationKitTypeLabel: v1.IntegrationKitTypeExternal,
                                                },
                                        },
                                        Spec: v1.IntegrationKitSpec{
diff --git a/pkg/cmd/promote.go b/pkg/cmd/promote.go
index c86a8dc5e..b8197b556 100644
--- a/pkg/cmd/promote.go
+++ b/pkg/cmd/promote.go
@@ -503,7 +503,7 @@ func (o *promoteCmdOptions) editIntegration(it 
*v1.Integration, kit *v1.Integrat
        dstKit.Spec = *kit.Spec.DeepCopy()
        dstKit.Annotations = cloneAnnotations(kit.Annotations, o.ToOperator)
        dstKit.Labels = cloneLabels(kit.Labels)
-       dstKit.Labels["camel.apache.org/kit.type"] = 
v1.IntegrationKitTypeExternal
+       dstKit.Labels = alterKitLabels(dstKit.Labels, kit)
        dstKit.Spec.Image = contImage
        // Integration
        dstIt := v1.NewIntegration(o.To, it.Name)
@@ -562,6 +562,31 @@ func cloneLabels(lbs map[string]string) map[string]string {
        return newMap
 }
 
+// Change labels expected by Integration Kit replacing the creator to reflect 
the
+// fact the new kit was cloned by another one instead.
+func alterKitLabels(lbs map[string]string, kit *v1.IntegrationKit) 
map[string]string {
+       lbs[v1.IntegrationKitTypeLabel] = v1.IntegrationKitTypeExternal
+       if lbs[kubernetes.CamelCreatorLabelKind] != "" {
+               delete(lbs, kubernetes.CamelCreatorLabelKind)
+       }
+       if lbs[kubernetes.CamelCreatorLabelName] != "" {
+               delete(lbs, kubernetes.CamelCreatorLabelName)
+       }
+       if lbs[kubernetes.CamelCreatorLabelNamespace] != "" {
+               delete(lbs, kubernetes.CamelCreatorLabelNamespace)
+       }
+       if lbs[kubernetes.CamelCreatorLabelVersion] != "" {
+               delete(lbs, kubernetes.CamelCreatorLabelVersion)
+       }
+
+       lbs[kubernetes.CamelClonedLabelKind] = v1.IntegrationKitKind
+       lbs[kubernetes.CamelClonedLabelName] = kit.Name
+       lbs[kubernetes.CamelClonedLabelNamespace] = kit.Namespace
+       lbs[kubernetes.CamelClonedLabelVersion] = kit.ResourceVersion
+
+       return lbs
+}
+
 func (o *promoteCmdOptions) editPipe(kb *v1.Pipe, it *v1.Integration, kit 
*v1.IntegrationKit) (*v1.Pipe, *v1.IntegrationKit) {
        contImage := it.Status.Image
        // IntegrationKit
@@ -569,7 +594,7 @@ func (o *promoteCmdOptions) editPipe(kb *v1.Pipe, it 
*v1.Integration, kit *v1.In
        dstKit.Spec = *kit.Spec.DeepCopy()
        dstKit.Annotations = cloneAnnotations(kit.Annotations, o.ToOperator)
        dstKit.Labels = cloneLabels(kit.Labels)
-       dstKit.Labels["camel.apache.org/kit.type"] = 
v1.IntegrationKitTypeExternal
+       dstKit.Labels = alterKitLabels(dstKit.Labels, kit)
        dstKit.Spec.Image = contImage
        // Pipe
        dst := v1.NewPipe(o.To, kb.Name)
diff --git a/pkg/cmd/promote_test.go b/pkg/cmd/promote_test.go
index 91c1b9dc3..72b87d495 100644
--- a/pkg/cmd/promote_test.go
+++ b/pkg/cmd/promote_test.go
@@ -24,6 +24,7 @@ import (
        v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
        "github.com/apache/camel-k/v2/pkg/platform"
        "github.com/apache/camel-k/v2/pkg/util/defaults"
+       "github.com/apache/camel-k/v2/pkg/util/kubernetes"
        "github.com/apache/camel-k/v2/pkg/util/test"
        "github.com/spf13/cobra"
        "github.com/stretchr/testify/assert"
@@ -98,6 +99,10 @@ kind: IntegrationKit
 metadata:
   creationTimestamp: null
   labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-it-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
     camel.apache.org/kit.type: external
   name: my-it-test-kit
   namespace: prod-namespace
@@ -166,6 +171,10 @@ kind: IntegrationKit
 metadata:
   creationTimestamp: null
   labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-kb-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
     camel.apache.org/kit.type: external
   name: my-kb-test-kit
   namespace: prod-namespace
@@ -236,6 +245,10 @@ kind: IntegrationKit
 metadata:
   creationTimestamp: null
   labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-it-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
     camel.apache.org/kit.type: external
   name: my-it-test-kit
   namespace: prod-namespace
@@ -296,6 +309,10 @@ kind: IntegrationKit
 metadata:
   creationTimestamp: null
   labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-kb-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
     camel.apache.org/kit.type: external
   name: my-kb-test-kit
   namespace: prod-namespace
@@ -393,6 +410,10 @@ metadata:
     camel.apache.org/operator.id: my-prod-operator
   creationTimestamp: null
   labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-it-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
     camel.apache.org/kit.type: external
   name: my-it-test-kit
   namespace: prod
@@ -434,6 +455,10 @@ metadata:
     camel.apache.org/operator.id: my-prod-operator
   creationTimestamp: null
   labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-it-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
     camel.apache.org/kit.type: external
   name: my-it-test-kit
   namespace: prod
@@ -461,3 +486,61 @@ spec:
 status: {}
 `, output)
 }
+
+func TestIntegrationKitWithLabels(t *testing.T) {
+       srcPlatform := v1.NewIntegrationPlatform("default", 
platform.DefaultPlatformName)
+       srcPlatform.Status.Version = defaults.Version
+       srcPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       srcPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       dstPlatform := v1.NewIntegrationPlatform("prod-namespace", 
platform.DefaultPlatformName)
+       dstPlatform.Status.Version = defaults.Version
+       dstPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       dstPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       defaultIntegration, defaultKit := nominalIntegration("my-it-test")
+       defaultKit.Labels = map[string]string{
+               kubernetes.CamelCreatorLabelKind:      "Integration",
+               kubernetes.CamelCreatorLabelName:      "my-original-it-name",
+               kubernetes.CamelCreatorLabelNamespace: 
"my-original-it-namespace",
+               kubernetes.CamelCreatorLabelVersion:   "my-original-it-version",
+       }
+       srcCatalog := createTestCamelCatalog(srcPlatform)
+       dstCatalog := createTestCamelCatalog(dstPlatform)
+
+       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultIntegration, &defaultKit, &srcCatalog, 
&dstCatalog)
+       output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-it-test", "--to", "prod-namespace", "-o", "yaml", "-n", "default")
+       assert.Equal(t, "yaml", promoteCmdOptions.OutputFormat)
+       require.NoError(t, err)
+       assert.Equal(t, `apiVersion: camel.apache.org/v1
+kind: IntegrationKit
+metadata:
+  creationTimestamp: null
+  labels:
+    camel.apache.org/cloned.from.kind: IntegrationKit
+    camel.apache.org/cloned.from.name: my-it-test-kit
+    camel.apache.org/cloned.from.namespace: default
+    camel.apache.org/cloned.from.version: "999"
+    camel.apache.org/kit.type: external
+  name: my-it-test-kit
+  namespace: prod-namespace
+spec:
+  image: my-special-image
+  traits: {}
+status: {}
+---
+apiVersion: camel.apache.org/v1
+kind: Integration
+metadata:
+  creationTimestamp: null
+  name: my-it-test
+  namespace: prod-namespace
+spec:
+  integrationKit:
+    kind: IntegrationKit
+    name: my-it-test-kit
+    namespace: prod-namespace
+  traits:
+    jvm:
+      classpath: /path/to/artifact-1/*:/path/to/artifact-2/*
+status: {}
+`, output)
+}
diff --git a/pkg/util/kubernetes/camel.go b/pkg/util/kubernetes/camel.go
index 8c069b375..c7bf020a1 100644
--- a/pkg/util/kubernetes/camel.go
+++ b/pkg/util/kubernetes/camel.go
@@ -29,11 +29,17 @@ import (
 
 const (
        CamelCreatorLabelPrefix = "camel.apache.org/created.by"
+       CamelClonedLabelPrefix  = "camel.apache.org/cloned.from"
 
        CamelCreatorLabelKind      = CamelCreatorLabelPrefix + ".kind"
        CamelCreatorLabelName      = CamelCreatorLabelPrefix + ".name"
        CamelCreatorLabelNamespace = CamelCreatorLabelPrefix + ".namespace"
        CamelCreatorLabelVersion   = CamelCreatorLabelPrefix + ".version"
+
+       CamelClonedLabelKind      = CamelClonedLabelPrefix + ".kind"
+       CamelClonedLabelName      = CamelClonedLabelPrefix + ".name"
+       CamelClonedLabelNamespace = CamelClonedLabelPrefix + ".namespace"
+       CamelClonedLabelVersion   = CamelClonedLabelPrefix + ".version"
 )
 
 // FilterCamelCreatorLabels is used to inherit the creator information among 
resources.

Reply via email to