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 358fdd1 commit ab1b8d3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 27 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 @@ -819,9 +825,9 @@ func ResetSnapshotStatusConditions(ctx context.Context, adapterClient client.Cli
return nil
}

// CopySnapshotLabelsAndAnnotation coppies labels and annotations from build pipelineRun or tested snapshot
// CopySnapshotLabelsAndAnnotations 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) {
func CopySnapshotLabelsAndAnnotations(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.CopySnapshotLabelsAndAnnotations(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.CopySnapshotLabelsAndAnnotations(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
9 changes: 7 additions & 2 deletions tekton/integration_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ var _ = Describe("Integration pipeline", func() {
Name: "snapshot-sample",
Namespace: "default",
Labels: map[string]string{
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: "component-sample",
gitops.SnapshotTypeLabel: "component",
gitops.SnapshotComponentLabel: "component-sample",
gitops.CustomLabelPrefix + "/custom-label": "custom-label",
},
},
Spec: applicationapiv1alpha1.SnapshotSpec{
Expand Down Expand Up @@ -357,6 +358,10 @@ var _ = Describe("Integration pipeline", func() {
To(Equal(hasSnapshot.Name))
Expect(newIntegrationPipelineRun.Labels["appstudio.openshift.io/component"]).
To(Equal(hasComp.Name))
Expect(newIntegrationPipelineRun.Labels[gitops.CustomLabelPrefix+"/custom_label"]).
To(Equal(hasSnapshot.Labels[gitops.CustomLabelPrefix+"/custom_label"]))
Expect(newIntegrationPipelineRun.Labels[gitops.SnapshotTypeLabel]).
To(Equal(hasSnapshot.Labels[gitops.SnapshotTypeLabel]))
})

It("can append labels coming from Application to IntegrationPipelineRun and making sure that label values matches application", func() {
Expand Down

0 comments on commit ab1b8d3

Please sign in to comment.