/
pipeline_run_builder.go
272 lines (226 loc) · 8.65 KB
/
pipeline_run_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"encoding/json"
"fmt"
"reflect"
"unicode"
"github.com/hashicorp/go-multierror"
libhandler "github.com/operator-framework/operator-lib/handler"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type PipelineRunBuilder struct {
err *multierror.Error
pipelineRun *tektonv1.PipelineRun
}
// NewPipelineRunBuilder initializes a new PipelineRunBuilder with the given name prefix and namespace.
// It sets the name of the PipelineRun to be generated with the provided prefix and sets its namespace.
func NewPipelineRunBuilder(namePrefix, namespace string) *PipelineRunBuilder {
return &PipelineRunBuilder{
pipelineRun: &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
GenerateName: namePrefix + "-",
Namespace: namespace,
},
Spec: tektonv1.PipelineRunSpec{},
},
}
}
// Build returns the constructed PipelineRun and any accumulated error.
func (b *PipelineRunBuilder) Build() (*tektonv1.PipelineRun, error) {
return b.pipelineRun, b.err.ErrorOrNil()
}
// WithAnnotations appends or updates annotations to the PipelineRun's metadata.
// If the PipelineRun does not have existing annotations, it initializes them before adding.
func (b *PipelineRunBuilder) WithAnnotations(annotations map[string]string) *PipelineRunBuilder {
if b.pipelineRun.ObjectMeta.Annotations == nil {
b.pipelineRun.ObjectMeta.Annotations = make(map[string]string)
}
for key, value := range annotations {
b.pipelineRun.ObjectMeta.Annotations[key] = value
}
return b
}
// WithFinalizer adds the given finalizer to the PipelineRun's metadata.
func (b *PipelineRunBuilder) WithFinalizer(finalizer string) *PipelineRunBuilder {
controllerutil.AddFinalizer(b.pipelineRun, finalizer)
return b
}
// WithLabels appends or updates labels to the PipelineRun's metadata.
// If the PipelineRun does not have existing labels, it initializes them before adding.
func (b *PipelineRunBuilder) WithLabels(labels map[string]string) *PipelineRunBuilder {
if b.pipelineRun.ObjectMeta.Labels == nil {
b.pipelineRun.ObjectMeta.Labels = make(map[string]string)
}
for key, value := range labels {
b.pipelineRun.ObjectMeta.Labels[key] = value
}
return b
}
// WithObjectReferences constructs tektonv1.Param entries for each of the provided client.Objects.
// Each param name is derived from the object's Kind (with the first letter made lowercase) and
// the value is a combination of the object's Namespace and Name.
func (b *PipelineRunBuilder) WithObjectReferences(objects ...client.Object) *PipelineRunBuilder {
for _, obj := range objects {
name := []rune(obj.GetObjectKind().GroupVersionKind().Kind)
name[0] = unicode.ToLower(name[0])
b.WithParams(tektonv1.Param{
Name: string(name),
Value: tektonv1.ParamValue{
Type: tektonv1.ParamTypeString,
StringVal: obj.GetNamespace() + "/" + obj.GetName(),
},
})
}
return b
}
// WithObjectSpecsAsJson constructs tektonv1.Param entries for the Spec field of each of the provided client.Objects.
// Each param name is derived from the object's Kind (with the first letter made lowercase).
// The value for each param is the JSON representation of the object's Spec.
// If an error occurs during extraction or serialization, it's accumulated in the builder's err field using multierror.
func (b *PipelineRunBuilder) WithObjectSpecsAsJson(objects ...client.Object) *PipelineRunBuilder {
for _, obj := range objects {
name := []rune(obj.GetObjectKind().GroupVersionKind().Kind)
name[0] = unicode.ToLower(name[0])
value := reflect.ValueOf(obj).Elem().FieldByName("Spec")
if !value.IsValid() {
b.err = multierror.Append(b.err, fmt.Errorf("failed to extract spec for object: %s", string(name)))
continue
}
jsonData, err := json.Marshal(value.Interface())
if err != nil {
b.err = multierror.Append(b.err, fmt.Errorf("failed to serialize spec of object %s to JSON: %v", string(name), err))
continue
}
b.WithParams(tektonv1.Param{
Name: string(name),
Value: tektonv1.ParamValue{
Type: tektonv1.ParamTypeString,
StringVal: string(jsonData),
},
})
}
return b
}
// WithOwner sets the given client.Object as the owner of the PipelineRun.
// It also adds the ReleaseFinalizer to the PipelineRun.
func (b *PipelineRunBuilder) WithOwner(object client.Object) *PipelineRunBuilder {
if err := libhandler.SetOwnerAnnotations(object, b.pipelineRun); err != nil {
b.err = multierror.Append(b.err, fmt.Errorf("failed to set owner annotations: %v", err))
return b
}
return b
}
// WithParams appends the provided params to the PipelineRun's spec.
func (b *PipelineRunBuilder) WithParams(params ...tektonv1.Param) *PipelineRunBuilder {
if b.pipelineRun.Spec.Params == nil {
b.pipelineRun.Spec.Params = make([]tektonv1.Param, 0)
}
b.pipelineRun.Spec.Params = append(b.pipelineRun.Spec.Params, params...)
return b
}
// WithParamsFromConfigMap adds parameters to the PipelineRun based on the provided keys from a given ConfigMap.
// If a key is present in the ConfigMap, a new tektonv1.Param is constructed with the key as the name and the associated
// value from the ConfigMap. Keys not found in the ConfigMap are ignored.
func (b *PipelineRunBuilder) WithParamsFromConfigMap(configMap *corev1.ConfigMap, keys []string) *PipelineRunBuilder {
if configMap == nil {
return b
}
var params []tektonv1.Param
for _, key := range keys {
if value, exists := configMap.Data[key]; exists {
params = append(params, tektonv1.Param{
Name: key,
Value: tektonv1.ParamValue{
Type: tektonv1.ParamTypeString,
StringVal: value,
},
})
}
}
return b.WithParams(params...)
}
// WithPipelineRef sets the PipelineRef for the PipelineRun's spec.
func (b *PipelineRunBuilder) WithPipelineRef(pipelineRef *tektonv1.PipelineRef) *PipelineRunBuilder {
b.pipelineRun.Spec.PipelineRef = pipelineRef
if pipelineRef.Resolver == "git" {
for _, param := range pipelineRef.Params {
if param.Name == "revision" {
b.WithParams(tektonv1.Param{
Name: "taskGitRevision",
Value: tektonv1.ParamValue{
Type: tektonv1.ParamTypeString,
StringVal: param.Value.StringVal,
},
})
}
if param.Name == "url" {
b.WithParams(tektonv1.Param{
Name: "taskGitUrl",
Value: tektonv1.ParamValue{
Type: tektonv1.ParamTypeString,
StringVal: param.Value.StringVal,
},
})
}
}
}
return b
}
// WithServiceAccount sets the ServiceAccountName for the PipelineRun's TaskRunTemplate.
func (b *PipelineRunBuilder) WithServiceAccount(serviceAccount string) *PipelineRunBuilder {
b.pipelineRun.Spec.TaskRunTemplate.ServiceAccountName = serviceAccount
return b
}
// WithTimeouts sets the Timeouts for the PipelineRun.
func (b *PipelineRunBuilder) WithTimeouts(timeouts, defaultTimeouts *tektonv1.TimeoutFields) *PipelineRunBuilder {
if timeouts == nil || *timeouts == (tektonv1.TimeoutFields{}) {
b.pipelineRun.Spec.Timeouts = defaultTimeouts
} else {
b.pipelineRun.Spec.Timeouts = timeouts
}
return b
}
// WithWorkspaceFromVolumeTemplate creates and adds a workspace binding to the PipelineRun's spec using
// the provided workspace name and volume size.
func (b *PipelineRunBuilder) WithWorkspaceFromVolumeTemplate(name, size string) *PipelineRunBuilder {
if b.pipelineRun.Spec.Workspaces == nil {
b.pipelineRun.Spec.Workspaces = []tektonv1.WorkspaceBinding{}
}
quantity, err := resource.ParseQuantity(size)
if err != nil {
b.err = multierror.Append(b.err, fmt.Errorf("invalid size format: %v", err))
return b
}
workspace := tektonv1.WorkspaceBinding{
Name: name,
VolumeClaimTemplate: &corev1.PersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: quantity,
},
},
},
},
}
b.pipelineRun.Spec.Workspaces = append(b.pipelineRun.Spec.Workspaces, workspace)
return b
}