Skip to content

Commit

Permalink
feat(STONEINTG-953): copy test and custom labels/annotations
Browse files Browse the repository at this point in the history
* Copy custom.appstudio.openshift.io prefixed labels and
  annotations from build pipelineRuns to Snapshots
* Copy all labels and annotations prefixed with
  test.appstudio.openshift.io/ and custom.appstudio.openshift.io/
  from the Snapshot to integration pipelineRuns

Signed-off-by: dirgim <[email protected]>
  • Loading branch information
dirgim committed Jul 4, 2024
1 parent 904b28c commit 2abc6ec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
18 changes: 13 additions & 5 deletions gitops/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{}
Expand All @@ -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)
}

}

Expand Down
3 changes: 2 additions & 1 deletion internal/controller/buildpipeline/buildpipeline_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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]))

})

Expand Down
7 changes: 0 additions & 7 deletions internal/controller/snapshot/snapshot_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 27 additions & 9 deletions internal/controller/snapshot/snapshot_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -601,21 +603,44 @@ 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"))

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())
Expand All @@ -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() {
Expand Down
19 changes: 18 additions & 1 deletion tekton/integration_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 2abc6ec

Please sign in to comment.