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 499ddeb1e fix(cmd): promote annotations/labels
499ddeb1e is described below

commit 499ddeb1ee5402df77105db68f6843546a92d7bc
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Jun 9 14:48:52 2023 +0200

    fix(cmd): promote annotations/labels
    
    Closes #4471
---
 pkg/cmd/promote.go      | 24 +++++++++++++
 pkg/cmd/promote_test.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)

diff --git a/pkg/cmd/promote.go b/pkg/cmd/promote.go
index c3968582e..274cffb18 100644
--- a/pkg/cmd/promote.go
+++ b/pkg/cmd/promote.go
@@ -441,6 +441,8 @@ func (o *promoteCmdOptions) editIntegration(it 
*v1.Integration) *v1.Integration
        dst := v1.NewIntegration(o.To, it.Name)
        contImage := it.Status.Image
        dst.Spec = *it.Spec.DeepCopy()
+       dst.Annotations = cloneAnnotations(it.Annotations)
+       dst.Labels = cloneLabels(it.Labels)
        if dst.Spec.Traits.Container == nil {
                dst.Spec.Traits.Container = &traitv1.ContainerTrait{}
        }
@@ -448,9 +450,31 @@ func (o *promoteCmdOptions) editIntegration(it 
*v1.Integration) *v1.Integration
        return &dst
 }
 
+// Return all annotations but the ones specific to source (ie, the operator).
+func cloneAnnotations(ann map[string]string) map[string]string {
+       newMap := make(map[string]string)
+       for k, v := range ann {
+               if k != v1.OperatorIDAnnotation {
+                       newMap[k] = v
+               }
+       }
+       return newMap
+}
+
+// Return all labels. The method is a reference if in the future we need to 
apply any filtering.
+func cloneLabels(lbs map[string]string) map[string]string {
+       newMap := make(map[string]string)
+       for k, v := range lbs {
+               newMap[k] = v
+       }
+       return newMap
+}
+
 func (o *promoteCmdOptions) editPipe(kb *v1.Pipe, it *v1.Integration) *v1.Pipe 
{
        dst := v1.NewPipe(o.To, kb.Name)
        dst.Spec = *kb.Spec.DeepCopy()
+       dst.Annotations = cloneAnnotations(kb.Annotations)
+       dst.Labels = cloneLabels(kb.Labels)
        contImage := it.Status.Image
        if dst.Spec.Integration == nil {
                dst.Spec.Integration = &v1.IntegrationSpec{}
diff --git a/pkg/cmd/promote_test.go b/pkg/cmd/promote_test.go
index d63543902..de35de413 100644
--- a/pkg/cmd/promote_test.go
+++ b/pkg/cmd/promote_test.go
@@ -160,3 +160,92 @@ func createTestCamelCatalog(platform 
v1.IntegrationPlatform) v1.CamelCatalog {
        c.Spec = v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Provider: 
platform.Status.Build.RuntimeProvider, Version: 
platform.Status.Build.RuntimeVersion}}
        return c
 }
+
+func TestIntegrationWithMetadataDryRun(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 := nominalIntegration("my-it-test")
+       defaultIntegration.Annotations = map[string]string{
+               "camel.apache.org/operator.id": "camel-k",
+               "my-annotation":                "my-value",
+       }
+       defaultIntegration.Labels = map[string]string{
+               "my-label": "my-value",
+       }
+       srcCatalog := createTestCamelCatalog(srcPlatform)
+       dstCatalog := createTestCamelCatalog(dstPlatform)
+
+       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultIntegration, &srcCatalog, &dstCatalog)
+       output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-it-test", "--to", "prod-namespace", "-o", "yaml", "-n", "default")
+       assert.Equal(t, "yaml", promoteCmdOptions.OutputFormat)
+       assert.Nil(t, err)
+       assert.Equal(t, `apiVersion: camel.apache.org/v1
+kind: Integration
+metadata:
+  annotations:
+    my-annotation: my-value
+  creationTimestamp: null
+  labels:
+    my-label: my-value
+  name: my-it-test
+  namespace: prod-namespace
+spec:
+  traits:
+    container:
+      image: my-special-image
+status: {}
+`, output)
+}
+
+func TestPipeWithMetadataDryRun(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
+       defaultKB := nominalPipe("my-kb-test")
+       defaultKB.Annotations = map[string]string{
+               "camel.apache.org/operator.id": "camel-k",
+               "my-annotation":                "my-value",
+       }
+       defaultKB.Labels = map[string]string{
+               "my-label": "my-value",
+       }
+       defaultIntegration := nominalIntegration("my-kb-test")
+       srcCatalog := createTestCamelCatalog(srcPlatform)
+       dstCatalog := createTestCamelCatalog(dstPlatform)
+
+       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultKB, &defaultIntegration, &srcCatalog, 
&dstCatalog)
+       output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-kb-test", "--to", "prod-namespace", "-o", "yaml", "-n", "default")
+       assert.Equal(t, "yaml", promoteCmdOptions.OutputFormat)
+       assert.Nil(t, err)
+       assert.Equal(t, `apiVersion: camel.apache.org/v1
+kind: Pipe
+metadata:
+  annotations:
+    my-annotation: my-value
+  creationTimestamp: null
+  labels:
+    my-label: my-value
+  name: my-kb-test
+  namespace: prod-namespace
+  resourceVersion: "1"
+spec:
+  integration:
+    traits:
+      container:
+        image: my-special-image
+  sink: {}
+  source: {}
+status: {}
+`, output)
+}

Reply via email to