Skip to content

Commit

Permalink
Merge pull request #944 from sm43/buildrun-image
Browse files Browse the repository at this point in the history
Adds support for specifying image labels and annotation in buildrun
  • Loading branch information
openshift-merge-robot authored Nov 22, 2021
2 parents 03d96e2 + 9d8ca47 commit c720a27
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 6 deletions.
87 changes: 87 additions & 0 deletions pkg/apis/build/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions pkg/reconciler/buildrun/resources/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,37 @@ import (
func amendTaskSpecWithImageMutate(
cfg *config.Config,
taskSpec *tektonv1beta1.TaskSpec,
buildOutput buildv1alpha1.Image,
buildOutput, buildRunOutput buildv1alpha1.Image,
) {
// initialize the step from the template
mutateStep := tektonv1beta1.Step{
Container: *cfg.MutateImageContainerTemplate.DeepCopy(),
}

mutateStep.Container.Name = imageMutateContainerName
mutateStep.Container.Args = mutateArgs(buildOutput.Annotations, buildOutput.Labels)

// if labels or annotations are specified in buildRun then merge them with build's
labels := mergeMaps(buildOutput.Labels, buildRunOutput.Labels)
annotations := mergeMaps(buildOutput.Annotations, buildRunOutput.Annotations)

mutateStep.Container.Args = mutateArgs(annotations, labels)

// append the mutate step
taskSpec.Steps = append(taskSpec.Steps, mutateStep)
}

// mergeMaps takes 2 maps as input and merge the second into the first
// values in second would takes precedence if both maps have same keys
func mergeMaps(first map[string]string, second map[string]string) map[string]string {
if len(first) == 0 {
first = map[string]string{}
}
for k, v := range second {
first[k] = v
}
return first
}

func mutateArgs(annotations, labels map[string]string) []string {
args := []string{
"--image",
Expand Down
14 changes: 10 additions & 4 deletions pkg/reconciler/buildrun/resources/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"

taskv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
v1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -213,10 +213,16 @@ func GenerateTaskSpec(
}
}

buildRunOutput := buildRun.Spec.Output
if buildRunOutput == nil {
buildRunOutput = &buildv1alpha1.Image{}
}

// Amending task spec with image mutate step if annotations or labels are
// specified in build manifest
if len(build.Spec.Output.Annotations) > 0 || len(build.Spec.Output.Labels) > 0 {
amendTaskSpecWithImageMutate(cfg, &generatedTaskSpec, build.Spec.Output)
// specified in build manifest or buildRun manifest
if len(build.Spec.Output.Annotations) > 0 || len(build.Spec.Output.Labels) > 0 ||
len(buildRunOutput.Annotations) > 0 || len(buildRunOutput.Labels) > 0 {
amendTaskSpecWithImageMutate(cfg, &generatedTaskSpec, build.Spec.Output, *buildRunOutput)
}

return &generatedTaskSpec, nil
Expand Down
84 changes: 84 additions & 0 deletions pkg/reconciler/buildrun/resources/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,90 @@ var _ = Describe("GenerateTaskrun", func() {
Expect(err.Error()).To(Equal("error(s) occurred merging environment variables into BuildStrategy \"buildah\" steps: [environment variable \"MY_VAR_1\" already exists, environment variable \"MY_VAR_2\" already exists]"))
})
})

Context("when only BuildRun has output image labels and annotation defined ", func() {
BeforeEach(func() {
build, err = ctl.LoadBuildYAML([]byte(test.BuildahBuildWithOutput))
Expect(err).To(BeNil())

buildRun, err = ctl.LoadBuildRunFromBytes([]byte(test.BuildahBuildRunWithOutputImageLabelsAndAnnotations))
Expect(err).To(BeNil())

buildStrategy, err = ctl.LoadBuildStrategyFromBytes([]byte(test.MinimalBuildahBuildStrategy))
Expect(err).To(BeNil())
buildStrategy.Spec.BuildSteps[0].ImagePullPolicy = "Always"

expectedCommandOrArg = []string{
"bud", "--tag=$(params.shp-output-image)", fmt.Sprintf("--file=$(inputs.params.%s)", "DOCKERFILE"), "$(params.shp-source-context)",
}

JustBeforeEach(func() {
got, err = resources.GenerateTaskSpec(config.NewDefaultConfig(), build, buildRun, buildStrategy.Spec.BuildSteps, []buildv1alpha1.Parameter{})
Expect(err).To(BeNil())
})

It("should contain a step to mutate the image with labels and annotations merged from build and buildrun", func() {
Expect(got.Steps[3].Name).To(Equal("mutate-image"))
Expect(got.Steps[3].Command[0]).To(Equal("/ko-app/mutate-image"))
Expect(got.Steps[3].Args).To(Equal([]string{
"--image",
"$(params.shp-output-image)",
"--result-file-image-digest",
"$(results.shp-image-digest.path)",
"result-file-image-size",
"$(results.shp-image-size.path)",
"--annotation",
"org.opencontainers.owner=my-company",
"--label",
"[email protected]",
"foo=bar",
}))
})
})
})

Context("when Build and BuildRun both have output image labels and annotation defined ", func() {
BeforeEach(func() {
build, err = ctl.LoadBuildYAML([]byte(test.BuildahBuildWithAnnotationAndLabel))
Expect(err).To(BeNil())

buildRun, err = ctl.LoadBuildRunFromBytes([]byte(test.BuildahBuildRunWithOutputImageLabelsAndAnnotations))
Expect(err).To(BeNil())

buildStrategy, err = ctl.LoadBuildStrategyFromBytes([]byte(test.MinimalBuildahBuildStrategy))
Expect(err).To(BeNil())
buildStrategy.Spec.BuildSteps[0].ImagePullPolicy = "Always"

expectedCommandOrArg = []string{
"bud", "--tag=$(params.shp-output-image)", fmt.Sprintf("--file=$(inputs.params.%s)", "DOCKERFILE"), "$(params.shp-source-context)",
}

JustBeforeEach(func() {
got, err = resources.GenerateTaskSpec(config.NewDefaultConfig(), build, buildRun, buildStrategy.Spec.BuildSteps, []buildv1alpha1.Parameter{})
Expect(err).To(BeNil())
})

It("should contain a step to mutate the image with labels and annotations merged from build and buildrun", func() {
Expect(got.Steps[3].Name).To(Equal("mutate-image"))
Expect(got.Steps[3].Command[0]).To(Equal("/ko-app/mutate-image"))
Expect(got.Steps[3].Args).To(Equal([]string{
"--image",
"$(params.shp-output-image)",
"--result-file-image-digest",
"$(results.shp-image-digest.path)",
"result-file-image-size",
"$(results.shp-image-size.path)",
"--annotation",
"org.opencontainers.owner=my-company",
"org.opencontainers.image.url=https://my-company.com/images",
"--label",
"[email protected]",
"foo=bar",
}))
})
})
})

})

Describe("Generate the TaskRun", func() {
Expand Down
22 changes: 22 additions & 0 deletions test/buildrun_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ spec:
fieldPath: "my-fieldpath"
`

// BuildahBuildRunWithOutputImageLabelsAndAnnotations defines a BuildRun
// with a output image labels and annotation
const BuildahBuildRunWithOutputImageLabelsAndAnnotations = `
apiVersion: shipwright.io/v1alpha1
kind: BuildRun
metadata:
name: buildah-run
namespace: build-test
spec:
buildRef:
name: buildah
serviceAccount:
name: buildpacks-v3-serviceaccount
output:
image: image-registry.openshift-image-registry.svc:5000/example/buildpacks-app-v2
labels:
"maintainer": "[email protected]"
"foo": "bar"
annotations:
"org.opencontainers.owner": "my-company"
`

// MinimalBuildahBuildRun defines a simple
// BuildRun with a referenced Build
const MinimalBuildahBuildRun = `
Expand Down

0 comments on commit c720a27

Please sign in to comment.