diff --git a/gitops/snapshot.go b/gitops/snapshot.go index f9507055a..62a4e800e 100644 --- a/gitops/snapshot.go +++ b/gitops/snapshot.go @@ -41,6 +41,12 @@ const ( // PipelinesAsCodePrefix contains the prefix applied to labels and annotations copied from Pipelines as Code resources. PipelinesAsCodePrefix = "pac.test.appstudio.openshift.io" + // TestLabelPrefix contains the prefix applied to labels and annotations related to testing. + TestLabelPrefix = "test.appstudio.openshift.io" + + // CustomLabelPrefix contains the prefix applied to custom user-defined labels and annotations. + CustomLabelPrefix = "custom.appstudio.openshift.io" + // SnapshotTypeLabel contains the type of the Snapshot. SnapshotTypeLabel = "test.appstudio.openshift.io/type" @@ -820,8 +826,8 @@ func ResetSnapshotStatusConditions(ctx context.Context, adapterClient client.Cli } // CopySnapshotLabelsAndAnnotation coppies labels and annotations from build pipelineRun or tested snapshot -// into regular snapshot -func CopySnapshotLabelsAndAnnotation(application *applicationapiv1alpha1.Application, snapshot *applicationapiv1alpha1.Snapshot, componentName string, source *metav1.ObjectMeta, prefix string) { +// into regular or composite snapshot +func CopySnapshotLabelsAndAnnotation(application *applicationapiv1alpha1.Application, snapshot *applicationapiv1alpha1.Snapshot, componentName string, source *metav1.ObjectMeta, prefixes []string) { if snapshot.Labels == nil { snapshot.Labels = map[string]string{} @@ -839,9 +845,11 @@ func CopySnapshotLabelsAndAnnotation(application *applicationapiv1alpha1.Applica _ = metadata.CopyLabelsWithPrefixReplacement(source, &snapshot.ObjectMeta, "pipelinesascode.tekton.dev", PipelinesAsCodePrefix) _ = metadata.CopyAnnotationsWithPrefixReplacement(source, &snapshot.ObjectMeta, "pipelinesascode.tekton.dev", PipelinesAsCodePrefix) - // Copy labels and annotations prefixed with defined prefix - _ = metadata.CopyLabelsByPrefix(source, &snapshot.ObjectMeta, prefix) - _ = metadata.CopyAnnotationsByPrefix(source, &snapshot.ObjectMeta, prefix) + for _, prefix := range prefixes { + // Copy labels and annotations prefixed with defined prefix + _ = metadata.CopyLabelsByPrefix(source, &snapshot.ObjectMeta, prefix) + _ = metadata.CopyAnnotationsByPrefix(source, &snapshot.ObjectMeta, prefix) + } } diff --git a/internal/controller/buildpipeline/buildpipeline_adapter.go b/internal/controller/buildpipeline/buildpipeline_adapter.go index aa59c54cf..5849b1f1e 100644 --- a/internal/controller/buildpipeline/buildpipeline_adapter.go +++ b/internal/controller/buildpipeline/buildpipeline_adapter.go @@ -210,7 +210,8 @@ func (a *Adapter) prepareSnapshotForPipelineRun(pipelineRun *tektonv1.PipelineRu return nil, err } - gitops.CopySnapshotLabelsAndAnnotation(application, snapshot, a.component.Name, &pipelineRun.ObjectMeta, gitops.BuildPipelineRunPrefix) + prefixes := []string{gitops.BuildPipelineRunPrefix, gitops.TestLabelPrefix, gitops.CustomLabelPrefix} + gitops.CopySnapshotLabelsAndAnnotation(application, snapshot, a.component.Name, &pipelineRun.ObjectMeta, prefixes) snapshot.Labels[gitops.BuildPipelineRunNameLabel] = pipelineRun.Name if pipelineRun.Status.CompletionTime != nil { diff --git a/internal/controller/buildpipeline/buildpipeline_adapter_test.go b/internal/controller/buildpipeline/buildpipeline_adapter_test.go index ca3f7ba73..3a7188be6 100644 --- a/internal/controller/buildpipeline/buildpipeline_adapter_test.go +++ b/internal/controller/buildpipeline/buildpipeline_adapter_test.go @@ -70,6 +70,7 @@ var _ = Describe("Pipeline Adapter", Ordered, func() { SampleImageWithoutDigest = "quay.io/redhat-appstudio/sample-image" SampleImage = SampleImageWithoutDigest + "@" + SampleDigest invalidDigest = "invalidDigest" + customLabel = "custom.appstudio.openshift.io/custom-label" ) BeforeAll(func() { @@ -256,8 +257,9 @@ var _ = Describe("Pipeline Adapter", Ordered, func() { "pipelines.openshift.io/runtime": "nodejs", "pipelines.openshift.io/strategy": "s2i", "appstudio.openshift.io/component": "component-sample", - "pipelinesascode.tekton.dev/event-type": "pull_request", "build.appstudio.redhat.com/target_branch": "main", + "pipelinesascode.tekton.dev/event-type": "pull_request", + customLabel: "custom-label", }, Annotations: map[string]string{ "appstudio.redhat.com/updateComponentOnSuccess": "false", @@ -415,12 +417,15 @@ var _ = Describe("Pipeline Adapter", Ordered, func() { Expect(err).ToNot(HaveOccurred()) Expect(copyToSnapshot).NotTo(BeNil()) - gitops.CopySnapshotLabelsAndAnnotation(hasApp, copyToSnapshot, hasComp.Name, &buildPipelineRun.ObjectMeta, gitops.BuildPipelineRunPrefix) + prefixes := []string{gitops.BuildPipelineRunPrefix, gitops.CustomLabelPrefix, gitops.TestLabelPrefix} + gitops.CopySnapshotLabelsAndAnnotation(hasApp, copyToSnapshot, hasComp.Name, &buildPipelineRun.ObjectMeta, prefixes) Expect(copyToSnapshot.Labels[gitops.SnapshotTypeLabel]).To(Equal(gitops.SnapshotComponentType)) Expect(copyToSnapshot.Labels[gitops.SnapshotComponentLabel]).To(Equal(hasComp.Name)) Expect(copyToSnapshot.Labels[gitops.ApplicationNameLabel]).To(Equal(hasApp.Name)) Expect(copyToSnapshot.Labels["build.appstudio.redhat.com/target_branch"]).To(Equal("main")) Expect(copyToSnapshot.Annotations["build.appstudio.openshift.io/repo"]).To(Equal("https://github.com/devfile-samples/devfile-sample-go-basic?rev=c713067b0e65fb3de50d1f7c457eb51c2ab0dbb0")) + Expect(copyToSnapshot.Labels[gitops.PipelineAsCodeEventTypeLabel]).To(Equal(buildPipelineRun.Labels["pipelinesascode.tekton.dev/event-type"])) + Expect(copyToSnapshot.Labels[customLabel]).To(Equal(buildPipelineRun.Labels[customLabel])) }) diff --git a/internal/controller/snapshot/snapshot_adapter.go b/internal/controller/snapshot/snapshot_adapter.go index 22bd84e46..bc65892bf 100644 --- a/internal/controller/snapshot/snapshot_adapter.go +++ b/internal/controller/snapshot/snapshot_adapter.go @@ -563,13 +563,6 @@ func (a *Adapter) createIntegrationPipelineRun(application *applicationapiv1alph } pipelineRun := pipelineRunBuilder.AsPipelineRun() - // copy PipelineRun PAC annotations/labels from snapshot to integration test PipelineRuns - _ = metadata.CopyAnnotationsByPrefix(&snapshot.ObjectMeta, &pipelineRun.ObjectMeta, gitops.PipelinesAsCodePrefix) - _ = metadata.CopyLabelsByPrefix(&snapshot.ObjectMeta, &pipelineRun.ObjectMeta, gitops.PipelinesAsCodePrefix) - - // Copy build labels and annotations prefixed with build.appstudio from snapshot to integration test PipelineRuns - _ = metadata.CopyLabelsByPrefix(&snapshot.ObjectMeta, &pipelineRun.ObjectMeta, gitops.BuildPipelineRunPrefix) - _ = metadata.CopyAnnotationsByPrefix(&snapshot.ObjectMeta, &pipelineRun.ObjectMeta, gitops.BuildPipelineRunPrefix) err := ctrl.SetControllerReference(snapshot, pipelineRun, a.client.Scheme()) if err != nil { diff --git a/internal/controller/snapshot/snapshot_adapter_test.go b/internal/controller/snapshot/snapshot_adapter_test.go index 7bfbedf00..502d829c9 100644 --- a/internal/controller/snapshot/snapshot_adapter_test.go +++ b/internal/controller/snapshot/snapshot_adapter_test.go @@ -71,6 +71,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { sample_image = "quay.io/redhat-appstudio/sample-image" sample_revision = "random-value" sampleDigest = "sha256:841328df1b9f8c4087adbdcfec6cc99ac8308805dea83f6d415d6fb8d40227c1" + customLabel = "custom.appstudio.openshift.io/custom-label" ) BeforeAll(func() { @@ -184,6 +185,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { gitops.SnapshotComponentLabel: "component-sample", "build.appstudio.redhat.com/pipeline": "enterprise-contract", gitops.PipelineAsCodeEventTypeLabel: "push", + customLabel: "custom-label", }, Annotations: map[string]string{ gitops.PipelineAsCodeInstallationIDAnnotation: "123", @@ -601,11 +603,12 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { Expect(buf.String()).Should(ContainSubstring(expectedLogEntry)) }) - It("ensures build labels/annotations prefixed with 'build.appstudio' are propagated from snapshot to Integration test PLR", func() { + It("ensures build, PaC, test, and custom labels/annotations are propagated from snapshot to Integration test PLR", func() { pipelineRun, err := adapter.createIntegrationPipelineRun(hasApp, integrationTestScenario, hasSnapshot) Expect(err).To(BeNil()) Expect(pipelineRun).ToNot(BeNil()) + // build annotations and labels prefixed with `build.appstudio` are copied annotation, found := pipelineRun.GetAnnotations()["build.appstudio.redhat.com/commit_sha"] Expect(found).To(BeTrue()) Expect(annotation).To(Equal("6c65b2fcaea3e1a0a92476c8b5dc89e92a85f025")) @@ -613,9 +616,31 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { label, found := pipelineRun.GetLabels()["build.appstudio.redhat.com/pipeline"] Expect(found).To(BeTrue()) Expect(label).To(Equal("enterprise-contract")) + + // Pac labels prefixed with 'pac.test.appstudio.openshift.io' are copied + _, found = hasSnapshot.GetLabels()[gitops.PipelineAsCodeEventTypeLabel] + Expect(found).To(BeTrue()) + label, found = pipelineRun.GetLabels()[gitops.PipelineAsCodeEventTypeLabel] + Expect(found).To(BeTrue()) + Expect(label).To(Equal(hasSnapshot.GetLabels()[gitops.PipelineAsCodeEventTypeLabel])) + + // test labels prefixed with 'test.appstudio.openshift.io' are copied + _, found = hasSnapshot.GetLabels()[gitops.SnapshotTypeLabel] + Expect(found).To(BeTrue()) + label, found = pipelineRun.GetLabels()[gitops.SnapshotTypeLabel] + Expect(found).To(BeTrue()) + Expect(label).To(Equal(hasSnapshot.GetLabels()[gitops.SnapshotTypeLabel])) + + // custom labels prefixed with 'custom.appstudio.openshift.io' are copied + _, found = hasSnapshot.GetLabels()[customLabel] + Expect(found).To(BeTrue()) + label, found = pipelineRun.GetLabels()[customLabel] + Expect(found).To(BeTrue()) + Expect(label).To(Equal(hasSnapshot.GetLabels()[customLabel])) + }) - It("ensures build labels/annotations non-prefixed with 'build.appstudio' are NOT propagated from snapshot to Integration test PLR", func() { + It("ensures other labels/annotations are NOT propagated from snapshot to Integration test PLR", func() { pipelineRun, err := adapter.createIntegrationPipelineRun(hasApp, integrationTestScenario, hasSnapshot) Expect(err).To(BeNil()) Expect(pipelineRun).ToNot(BeNil()) @@ -625,13 +650,6 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { Expect(found).To(BeTrue()) _, found = pipelineRun.GetAnnotations()["appstudio.redhat.com/updateComponentOnSuccess"] Expect(found).To(BeFalse()) - - // build labels non-prefixed with 'build.appstudio' are not copied - _, found = hasSnapshot.GetLabels()[gitops.SnapshotTypeLabel] - Expect(found).To(BeTrue()) - _, found = pipelineRun.GetLabels()[gitops.SnapshotTypeLabel] - Expect(found).To(BeFalse()) - }) When("pull request updates repo with integration test", func() { diff --git a/tekton/integration_pipeline.go b/tekton/integration_pipeline.go index 3195d92bb..611bf9da2 100644 --- a/tekton/integration_pipeline.go +++ b/tekton/integration_pipeline.go @@ -37,9 +37,18 @@ const ( // PipelinesLabelPrefix is the prefix of the pipelines label PipelinesLabelPrefix = "pipelines.appstudio.openshift.io" - // TestLabelPrefix is the prefix of the test labels + // TestLabelPrefix contains the prefix applied to labels and annotations related to testing. TestLabelPrefix = "test.appstudio.openshift.io" + // PipelinesAsCodePrefix contains the prefix applied to labels and annotations copied from Pipelines as Code resources. + PipelinesAsCodePrefix = "pac.test.appstudio.openshift.io" + + // BuildPipelineRunPrefix contains the build pipeline run related labels and annotations + BuildPipelineRunPrefix = "build.appstudio" + + // CustomLabelPrefix contains the prefix applied to custom user-defined labels and annotations. + CustomLabelPrefix = "custom.appstudio.openshift.io" + // resource labels for snapshot, application and component ResourceLabelSuffix = "appstudio.openshift.io" @@ -204,6 +213,14 @@ func (r *IntegrationPipelineRun) WithSnapshot(snapshot *applicationapiv1alpha1.S r.ObjectMeta.Labels[ComponentNameLabel] = componentLabel } + // copy PipelineRun PAC, build, test and custom annotations/labels from Snapshot to integration test PipelineRun + prefixes := []string{PipelinesAsCodePrefix, BuildPipelineRunPrefix, TestLabelPrefix, CustomLabelPrefix} + for _, prefix := range prefixes { + // Copy labels and annotations prefixed with defined prefix + _ = metadata.CopyAnnotationsByPrefix(&snapshot.ObjectMeta, &r.ObjectMeta, prefix) + _ = metadata.CopyLabelsByPrefix(&snapshot.ObjectMeta, &r.ObjectMeta, prefix) + } + return r }