forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipelinerun_conversion.go
373 lines (346 loc) · 11.1 KB
/
pipelinerun_conversion.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Copyright 2020 The Tekton Authors
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 v1beta1
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/version"
"knative.dev/pkg/apis"
)
var _ apis.Convertible = (*PipelineRun)(nil)
// ConvertTo implements apis.Convertible
func (pr *PipelineRun) ConvertTo(ctx context.Context, to apis.Convertible) error {
if apis.IsInDelete(ctx) {
return nil
}
switch sink := to.(type) {
case *v1.PipelineRun:
sink.ObjectMeta = pr.ObjectMeta
if err := serializePipelineRunResources(&sink.ObjectMeta, &pr.Spec); err != nil {
return err
}
if err := pr.Status.convertTo(ctx, &sink.Status, &sink.ObjectMeta); err != nil {
return err
}
return pr.Spec.ConvertTo(ctx, &sink.Spec, &sink.ObjectMeta)
default:
return fmt.Errorf("unknown version, got: %T", sink)
}
}
// ConvertTo implements apis.Convertible
func (prs PipelineRunSpec) ConvertTo(ctx context.Context, sink *v1.PipelineRunSpec, meta *metav1.ObjectMeta) error {
if prs.PipelineRef != nil {
sink.PipelineRef = &v1.PipelineRef{}
prs.PipelineRef.convertTo(ctx, sink.PipelineRef)
}
if prs.PipelineSpec != nil {
sink.PipelineSpec = &v1.PipelineSpec{}
err := prs.PipelineSpec.ConvertTo(ctx, sink.PipelineSpec, meta)
if err != nil {
return err
}
}
sink.Params = nil
for _, p := range prs.Params {
new := v1.Param{}
p.convertTo(ctx, &new)
sink.Params = append(sink.Params, new)
}
sink.Status = v1.PipelineRunSpecStatus(prs.Status)
if prs.Timeouts != nil {
sink.Timeouts = &v1.TimeoutFields{}
prs.Timeouts.convertTo(ctx, sink.Timeouts)
}
if prs.Timeout != nil {
sink.Timeouts = &v1.TimeoutFields{}
sink.Timeouts.Pipeline = prs.Timeout
}
sink.TaskRunTemplate = v1.PipelineTaskRunTemplate{}
sink.TaskRunTemplate.PodTemplate = prs.PodTemplate
sink.TaskRunTemplate.ServiceAccountName = prs.ServiceAccountName
sink.Workspaces = nil
for _, w := range prs.Workspaces {
new := v1.WorkspaceBinding{}
w.convertTo(ctx, &new)
sink.Workspaces = append(sink.Workspaces, new)
}
sink.TaskRunSpecs = nil
for _, ptrs := range prs.TaskRunSpecs {
new := v1.PipelineTaskRunSpec{}
ptrs.convertTo(ctx, &new)
sink.TaskRunSpecs = append(sink.TaskRunSpecs, new)
}
return nil
}
// ConvertFrom implements apis.Convertible
func (pr *PipelineRun) ConvertFrom(ctx context.Context, from apis.Convertible) error {
switch source := from.(type) {
case *v1.PipelineRun:
pr.ObjectMeta = source.ObjectMeta
if err := deserializePipelineRunResources(&pr.ObjectMeta, &pr.Spec); err != nil {
return err
}
if err := pr.Status.convertFrom(ctx, &source.Status, &pr.ObjectMeta); err != nil {
return err
}
return pr.Spec.ConvertFrom(ctx, &source.Spec, &pr.ObjectMeta)
default:
return fmt.Errorf("unknown version, got: %T", pr)
}
}
// ConvertFrom implements apis.Convertible
func (prs *PipelineRunSpec) ConvertFrom(ctx context.Context, source *v1.PipelineRunSpec, meta *metav1.ObjectMeta) error {
if source.PipelineRef != nil {
newPipelineRef := PipelineRef{}
newPipelineRef.convertFrom(ctx, *source.PipelineRef)
prs.PipelineRef = &newPipelineRef
}
if source.PipelineSpec != nil {
newPipelineSpec := PipelineSpec{}
err := newPipelineSpec.ConvertFrom(ctx, source.PipelineSpec, meta)
if err != nil {
return err
}
prs.PipelineSpec = &newPipelineSpec
}
prs.Params = nil
for _, p := range source.Params {
new := Param{}
new.ConvertFrom(ctx, p)
prs.Params = append(prs.Params, new)
}
prs.ServiceAccountName = source.TaskRunTemplate.ServiceAccountName
prs.Status = PipelineRunSpecStatus(source.Status)
if source.Timeouts != nil {
newTimeouts := &TimeoutFields{}
newTimeouts.convertFrom(ctx, *source.Timeouts)
prs.Timeouts = newTimeouts
}
prs.PodTemplate = source.TaskRunTemplate.PodTemplate
prs.Workspaces = nil
for _, w := range source.Workspaces {
new := WorkspaceBinding{}
new.ConvertFrom(ctx, w)
prs.Workspaces = append(prs.Workspaces, new)
}
prs.TaskRunSpecs = nil
for _, trs := range source.TaskRunSpecs {
new := PipelineTaskRunSpec{}
new.convertFrom(ctx, trs)
prs.TaskRunSpecs = append(prs.TaskRunSpecs, new)
}
return nil
}
func (tf TimeoutFields) convertTo(ctx context.Context, sink *v1.TimeoutFields) {
sink.Pipeline = tf.Pipeline
sink.Tasks = tf.Tasks
sink.Finally = tf.Finally
}
func (tf *TimeoutFields) convertFrom(ctx context.Context, source v1.TimeoutFields) {
tf.Pipeline = source.Pipeline
tf.Tasks = source.Tasks
tf.Finally = source.Finally
}
func (ptrs PipelineTaskRunSpec) convertTo(ctx context.Context, sink *v1.PipelineTaskRunSpec) {
sink.PipelineTaskName = ptrs.PipelineTaskName
sink.ServiceAccountName = ptrs.TaskServiceAccountName
sink.PodTemplate = ptrs.TaskPodTemplate
sink.StepSpecs = nil
for _, so := range ptrs.StepOverrides {
new := v1.TaskRunStepSpec{}
so.convertTo(ctx, &new)
sink.StepSpecs = append(sink.StepSpecs, new)
}
sink.SidecarSpecs = nil
for _, so := range ptrs.SidecarOverrides {
new := v1.TaskRunSidecarSpec{}
so.convertTo(ctx, &new)
sink.SidecarSpecs = append(sink.SidecarSpecs, new)
}
if ptrs.Metadata != nil {
sink.Metadata = &v1.PipelineTaskMetadata{}
ptrs.Metadata.convertTo(ctx, sink.Metadata)
}
sink.ComputeResources = ptrs.ComputeResources
}
func (ptrs *PipelineTaskRunSpec) convertFrom(ctx context.Context, source v1.PipelineTaskRunSpec) {
ptrs.PipelineTaskName = source.PipelineTaskName
ptrs.TaskServiceAccountName = source.ServiceAccountName
ptrs.TaskPodTemplate = source.PodTemplate
ptrs.StepOverrides = nil
for _, so := range source.StepSpecs {
new := TaskRunStepOverride{}
new.convertFrom(ctx, so)
ptrs.StepOverrides = append(ptrs.StepOverrides, new)
}
ptrs.SidecarOverrides = nil
for _, so := range source.SidecarSpecs {
new := TaskRunSidecarOverride{}
new.convertFrom(ctx, so)
ptrs.SidecarOverrides = append(ptrs.SidecarOverrides, new)
}
if source.Metadata != nil {
newMetadata := PipelineTaskMetadata{}
newMetadata.convertFrom(ctx, *source.Metadata)
ptrs.Metadata = &newMetadata
}
ptrs.ComputeResources = source.ComputeResources
}
func (prs *PipelineRunStatus) convertTo(ctx context.Context, sink *v1.PipelineRunStatus, meta *metav1.ObjectMeta) error {
sink.Status = prs.Status
sink.StartTime = prs.StartTime
sink.CompletionTime = prs.CompletionTime
sink.Results = nil
for _, pr := range prs.PipelineResults {
new := v1.PipelineRunResult{}
pr.convertTo(ctx, &new)
sink.Results = append(sink.Results, new)
}
if prs.PipelineSpec != nil {
sink.PipelineSpec = &v1.PipelineSpec{}
err := prs.PipelineSpec.ConvertTo(ctx, sink.PipelineSpec, meta)
if err != nil {
return err
}
}
sink.SkippedTasks = nil
for _, st := range prs.SkippedTasks {
new := v1.SkippedTask{}
st.convertTo(ctx, &new)
sink.SkippedTasks = append(sink.SkippedTasks, new)
}
sink.ChildReferences = nil
for _, cr := range prs.ChildReferences {
new := v1.ChildStatusReference{}
cr.convertTo(ctx, &new)
sink.ChildReferences = append(sink.ChildReferences, new)
}
sink.FinallyStartTime = prs.FinallyStartTime
if prs.Provenance != nil {
new := v1.Provenance{}
prs.Provenance.convertTo(ctx, &new)
sink.Provenance = &new
}
return nil
}
func (prs *PipelineRunStatus) convertFrom(ctx context.Context, source *v1.PipelineRunStatus, meta *metav1.ObjectMeta) error {
prs.Status = source.Status
prs.StartTime = source.StartTime
prs.CompletionTime = source.CompletionTime
prs.PipelineResults = nil
for _, pr := range source.Results {
new := PipelineRunResult{}
new.convertFrom(ctx, pr)
prs.PipelineResults = append(prs.PipelineResults, new)
}
if source.PipelineSpec != nil {
newPipelineSpec := PipelineSpec{}
err := newPipelineSpec.ConvertFrom(ctx, source.PipelineSpec, meta)
if err != nil {
return err
}
prs.PipelineSpec = &newPipelineSpec
}
prs.SkippedTasks = nil
for _, st := range source.SkippedTasks {
new := SkippedTask{}
new.convertFrom(ctx, st)
prs.SkippedTasks = append(prs.SkippedTasks, new)
}
prs.ChildReferences = nil
for _, cr := range source.ChildReferences {
new := ChildStatusReference{}
new.convertFrom(ctx, cr)
prs.ChildReferences = append(prs.ChildReferences, new)
}
prs.FinallyStartTime = source.FinallyStartTime
if source.Provenance != nil {
new := Provenance{}
new.convertFrom(ctx, *source.Provenance)
prs.Provenance = &new
}
return nil
}
func (prr PipelineRunResult) convertTo(ctx context.Context, sink *v1.PipelineRunResult) {
sink.Name = prr.Name
newValue := v1.ParamValue{}
prr.Value.convertTo(ctx, &newValue)
sink.Value = newValue
}
func (prr *PipelineRunResult) convertFrom(ctx context.Context, source v1.PipelineRunResult) {
prr.Name = source.Name
newValue := ParamValue{}
newValue.convertFrom(ctx, source.Value)
prr.Value = newValue
}
func (st SkippedTask) convertTo(ctx context.Context, sink *v1.SkippedTask) {
sink.Name = st.Name
sink.Reason = v1.SkippingReason(st.Reason)
sink.WhenExpressions = nil
for _, we := range st.WhenExpressions {
new := v1.WhenExpression{}
we.convertTo(ctx, &new)
sink.WhenExpressions = append(sink.WhenExpressions, new)
}
}
func (st *SkippedTask) convertFrom(ctx context.Context, source v1.SkippedTask) {
st.Name = source.Name
st.Reason = SkippingReason(source.Reason)
st.WhenExpressions = nil
for _, we := range source.WhenExpressions {
new := WhenExpression{}
new.convertFrom(ctx, we)
st.WhenExpressions = append(st.WhenExpressions, new)
}
}
func (csr ChildStatusReference) convertTo(ctx context.Context, sink *v1.ChildStatusReference) {
sink.TypeMeta = csr.TypeMeta
sink.Name = csr.Name
sink.PipelineTaskName = csr.PipelineTaskName
sink.WhenExpressions = nil
for _, we := range csr.WhenExpressions {
new := v1.WhenExpression{}
we.convertTo(ctx, &new)
sink.WhenExpressions = append(sink.WhenExpressions, new)
}
}
func (csr *ChildStatusReference) convertFrom(ctx context.Context, source v1.ChildStatusReference) {
csr.TypeMeta = source.TypeMeta
csr.Name = source.Name
csr.PipelineTaskName = source.PipelineTaskName
csr.WhenExpressions = nil
for _, we := range source.WhenExpressions {
new := WhenExpression{}
new.convertFrom(ctx, we)
csr.WhenExpressions = append(csr.WhenExpressions, new)
}
}
func serializePipelineRunResources(meta *metav1.ObjectMeta, spec *PipelineRunSpec) error {
if spec.Resources == nil {
return nil
}
return version.SerializeToMetadata(meta, spec.Resources, resourcesAnnotationKey)
}
func deserializePipelineRunResources(meta *metav1.ObjectMeta, spec *PipelineRunSpec) error {
resources := []PipelineResourceBinding{}
err := version.DeserializeFromMetadata(meta, &resources, resourcesAnnotationKey)
if err != nil {
return err
}
if len(resources) != 0 {
spec.Resources = resources
}
return nil
}