Skip to content

Commit

Permalink
Add support to add labels and annotations to build pod
Browse files Browse the repository at this point in the history
Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
  • Loading branch information
wanjunlei committed Nov 9, 2022
1 parent c3d13c7 commit 5a3dd36
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 28 deletions.
36 changes: 36 additions & 0 deletions deploy/crds/shipwright.io_buildruns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions deploy/crds/shipwright.io_builds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/build/v1alpha1/build_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/build/v1alpha1/buildrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 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.

87 changes: 59 additions & 28 deletions pkg/reconciler/buildrun/resources/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down

0 comments on commit 5a3dd36

Please sign in to comment.