From 7831ade128bf4998b4e776d9337356d9f319e560 Mon Sep 17 00:00:00 2001 From: wanjunlei Date: Tue, 8 Nov 2022 11:08:56 +0800 Subject: [PATCH] Add support for adding labels and annotations to build pod Signed-off-by: wanjunlei --- deploy/crds/shipwright.io_buildruns.yaml | 36 ++++++++ deploy/crds/shipwright.io_builds.yaml | 12 +++ pkg/apis/build/v1alpha1/build_types.go | 8 ++ pkg/apis/build/v1alpha1/buildrun_types.go | 8 ++ .../build/v1alpha1/zz_generated.deepcopy.go | 28 ++++++ pkg/reconciler/buildrun/resources/taskrun.go | 87 +++++++++++++------ 6 files changed, 151 insertions(+), 28 deletions(-) diff --git a/deploy/crds/shipwright.io_buildruns.yaml b/deploy/crds/shipwright.io_buildruns.yaml index ce0415f6f2..cb7fdc1fff 100644 --- a/deploy/crds/shipwright.io_buildruns.yaml +++ b/deploy/crds/shipwright.io_buildruns.yaml @@ -56,6 +56,12 @@ spec: spec: description: BuildRunSpec defines the desired state of BuildRun properties: + annotations: + additionalProperties: + type: string + description: Annotations contains annotations that should be passed + to the build pod + type: object buildRef: description: BuildRef refers to the Build properties: @@ -71,6 +77,12 @@ spec: buildSpec: description: BuildSpec refers to an embedded build specification properties: + annotations: + additionalProperties: + type: string + description: Annotations contains annotations that should be passed + to the build pod + type: object builder: description: "Builder refers to the image containing the build tools inside which the source code would be built. \n NOTICE: @@ -223,6 +235,12 @@ spec: - name type: object type: array + labels: + additionalProperties: + type: string + description: Labels contains labels that should be passed to the + build pod + type: object output: description: Output refers to the location where the built image would be pushed. @@ -2204,6 +2222,12 @@ spec: - name type: object type: array + labels: + additionalProperties: + type: string + description: Labels contains labels that should be passed to the build + pod + type: object output: description: Output refers to the location where the generated image would be pushed to. It will overwrite the output image in build @@ -3885,6 +3909,12 @@ spec: buildSpec: description: BuildSpec is the Build Spec of this BuildRun. properties: + annotations: + additionalProperties: + type: string + description: Annotations contains annotations that should be passed + to the build pod + type: object builder: description: "Builder refers to the image containing the build tools inside which the source code would be built. \n NOTICE: @@ -4037,6 +4067,12 @@ spec: - name type: object type: array + labels: + additionalProperties: + type: string + description: Labels contains labels that should be passed to the + build pod + type: object output: description: Output refers to the location where the built image would be pushed. diff --git a/deploy/crds/shipwright.io_builds.yaml b/deploy/crds/shipwright.io_builds.yaml index b2db555771..677f8e2918 100644 --- a/deploy/crds/shipwright.io_builds.yaml +++ b/deploy/crds/shipwright.io_builds.yaml @@ -58,6 +58,12 @@ spec: spec: description: BuildSpec defines the desired state of Build properties: + annotations: + additionalProperties: + type: string + description: Annotations contains annotations that should be passed + to the build pod + type: object builder: description: "Builder refers to the image containing the build tools inside which the source code would be built. \n NOTICE: Builder @@ -205,6 +211,12 @@ spec: - name type: object type: array + labels: + additionalProperties: + type: string + description: Labels contains labels that should be passed to the build + pod + type: object output: description: Output refers to the location where the built image would be pushed. diff --git a/pkg/apis/build/v1alpha1/build_types.go b/pkg/apis/build/v1alpha1/build_types.go index 60dd084d11..342f22eda0 100644 --- a/pkg/apis/build/v1alpha1/build_types.go +++ b/pkg/apis/build/v1alpha1/build_types.go @@ -178,6 +178,14 @@ type BuildSpec struct { // to be overridden. Must only contain volumes that exist in the corresponding BuildStrategy // +optional Volumes []BuildVolume `json:"volumes,omitempty"` + + // Labels contains labels that should be passed to the build pod + // +optional + Labels map[string]string `json:"labels,omitempty"` + + // Annotations contains annotations that should be passed to the build pod + // +optional + Annotations map[string]string `json:"annotations,omitempty"` } // BuildVolume is a volume that will be mounted in build pod during build step diff --git a/pkg/apis/build/v1alpha1/buildrun_types.go b/pkg/apis/build/v1alpha1/buildrun_types.go index 3f85bdfec4..363149de95 100644 --- a/pkg/apis/build/v1alpha1/buildrun_types.go +++ b/pkg/apis/build/v1alpha1/buildrun_types.go @@ -81,6 +81,14 @@ type BuildRunSpec struct { // to be overridden. Must only contain volumes that exist in the corresponding BuildStrategy // +optional Volumes []BuildVolume `json:"volumes,omitempty"` + + // Labels contains labels that should be passed to the build pod + // +optional + Labels map[string]string `json:"labels,omitempty"` + + // Annotations contains annotations that should be passed to the build pod + // +optional + Annotations map[string]string `json:"annotations,omitempty"` } // BuildRunRequestedState defines the buildrun state the user can provide to override whatever is the current state. diff --git a/pkg/apis/build/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/build/v1alpha1/zz_generated.deepcopy.go index be57746009..42bdb37bfa 100644 --- a/pkg/apis/build/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/build/v1alpha1/zz_generated.deepcopy.go @@ -286,6 +286,20 @@ func (in *BuildRunSpec) DeepCopyInto(out *BuildRunSpec) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } return } @@ -442,6 +456,20 @@ func (in *BuildSpec) DeepCopyInto(out *BuildSpec) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } return } diff --git a/pkg/reconciler/buildrun/resources/taskrun.go b/pkg/reconciler/buildrun/resources/taskrun.go index 97b00a8f8a..6fe6834ec2 100644 --- a/pkg/reconciler/buildrun/resources/taskrun.go +++ b/pkg/reconciler/buildrun/resources/taskrun.go @@ -245,6 +245,63 @@ func GenerateTaskSpec( return &generatedTaskSpec, nil } +func generateTaskRunLabels( + build *buildv1alpha1.Build, + buildRun *buildv1alpha1.BuildRun, + strategy buildv1alpha1.BuilderStrategy, +) map[string]string { + // Add BuildRun name reference to the TaskRun labels + taskRunLabels := map[string]string{ + buildv1alpha1.LabelBuildRun: buildRun.Name, + buildv1alpha1.LabelBuildRunGeneration: strconv.FormatInt(buildRun.Generation, 10), + } + + // Add Build name reference unless it is an embedded Build (empty build name) + if build.Name != "" { + taskRunLabels[buildv1alpha1.LabelBuild] = build.Name + taskRunLabels[buildv1alpha1.LabelBuildGeneration] = strconv.FormatInt(build.Generation, 10) + } + + for label, value := range strategy.GetResourceLabels() { + taskRunLabels[label] = value + } + + for label, value := range build.Spec.Labels { + taskRunLabels[label] = value + } + + for label, value := range buildRun.Spec.Labels { + taskRunLabels[label] = value + } + + return taskRunLabels +} + +func generateTaskRunAnnotations( + build *buildv1alpha1.Build, + buildRun *buildv1alpha1.BuildRun, + strategy buildv1alpha1.BuilderStrategy, +) map[string]string { + // assign the annotations from the build strategy, filter out those that should not be propagated + taskRunAnnotations := make(map[string]string) + for key, value := range strategy.GetAnnotations() { + if isPropagatableAnnotation(key) { + taskRunAnnotations[key] = value + } + } + + for label, value := range build.Spec.Annotations { + taskRunAnnotations[label] = value + } + + for label, value := range buildRun.Spec.Annotations { + taskRunAnnotations[label] = value + } + + return taskRunAnnotations + +} + // GenerateTaskRun creates a Tekton TaskRun to be used for a build run func GenerateTaskRun( cfg *config.Config, @@ -274,23 +331,12 @@ func GenerateTaskRun( return nil, err } - // Add BuildRun name reference to the TaskRun labels - taskRunLabels := map[string]string{ - buildv1alpha1.LabelBuildRun: buildRun.Name, - buildv1alpha1.LabelBuildRunGeneration: strconv.FormatInt(buildRun.Generation, 10), - } - - // Add Build name reference unless it is an embedded Build (empty build name) - if build.Name != "" { - taskRunLabels[buildv1alpha1.LabelBuild] = build.Name - taskRunLabels[buildv1alpha1.LabelBuildGeneration] = strconv.FormatInt(build.Generation, 10) - } - expectedTaskRun := &v1beta1.TaskRun{ ObjectMeta: metav1.ObjectMeta{ GenerateName: buildRun.Name + "-", Namespace: buildRun.Namespace, - Labels: taskRunLabels, + Labels: generateTaskRunLabels(build, buildRun, strategy), + Annotations: generateTaskRunAnnotations(build, buildRun, strategy), }, Spec: v1beta1.TaskRunSpec{ ServiceAccountName: serviceAccountName, @@ -305,21 +351,6 @@ func GenerateTaskRun( }, } - // assign the annotations from the build strategy, filter out those that should not be propagated - taskRunAnnotations := make(map[string]string) - for key, value := range strategy.GetAnnotations() { - if isPropagatableAnnotation(key) { - taskRunAnnotations[key] = value - } - } - if len(taskRunAnnotations) > 0 { - expectedTaskRun.Annotations = taskRunAnnotations - } - - for label, value := range strategy.GetResourceLabels() { - expectedTaskRun.Labels[label] = value - } - expectedTaskRun.Spec.Timeout = effectiveTimeout(build, buildRun) params := []v1beta1.Param{