forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipelinerunresolution.go
799 lines (738 loc) · 28.3 KB
/
pipelinerunresolution.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
Copyright 2019 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 resources
import (
"context"
"errors"
"fmt"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/resources"
"github.com/tektoncd/pipeline/pkg/remote"
"github.com/tektoncd/pipeline/pkg/trustedresources"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"knative.dev/pkg/apis"
"knative.dev/pkg/kmeta"
)
const (
// ReasonConditionCheckFailed indicates that the reason for the failure status is that the
// condition check associated to the pipeline task evaluated to false
ReasonConditionCheckFailed = "ConditionCheckFailed"
)
// TaskSkipStatus stores whether a task was skipped and why
type TaskSkipStatus struct {
IsSkipped bool
SkippingReason v1beta1.SkippingReason
}
// TaskNotFoundError indicates that the resolution failed because a referenced Task couldn't be retrieved
type TaskNotFoundError struct {
Name string
Msg string
}
func (e *TaskNotFoundError) Error() string {
return fmt.Sprintf("Couldn't retrieve Task %q: %s", e.Name, e.Msg)
}
// ResolvedPipelineTask contains a PipelineTask and its associated TaskRun(s) or RunObjects, if they exist.
type ResolvedPipelineTask struct {
TaskRunName string
TaskRun *v1beta1.TaskRun
TaskRunNames []string
TaskRuns []*v1beta1.TaskRun
// If the PipelineTask is a Custom Task, RunObjectName and RunObject will be set.
CustomTask bool
RunObjectName string
RunObject v1beta1.RunObject
RunObjectNames []string
RunObjects []v1beta1.RunObject
PipelineTask *v1beta1.PipelineTask
ResolvedTask *resources.ResolvedTask
}
// isDone returns true only if the task is skipped, succeeded or failed
func (t ResolvedPipelineTask) isDone(facts *PipelineRunFacts) bool {
return t.Skip(facts).IsSkipped || t.isSuccessful() || t.isFailure()
}
// IsRunning returns true only if the task is neither succeeded, cancelled nor failed
func (t ResolvedPipelineTask) IsRunning() bool {
switch {
case t.IsCustomTask() && t.PipelineTask.IsMatrixed():
if len(t.RunObjects) == 0 {
return false
}
case t.IsCustomTask():
if t.RunObject == nil {
return false
}
case t.PipelineTask.IsMatrixed():
if len(t.TaskRuns) == 0 {
return false
}
default:
if t.TaskRun == nil {
return false
}
}
return !t.isSuccessful() && !t.isFailure()
}
// IsCustomTask returns true if the PipelineTask references a Custom Task.
func (t ResolvedPipelineTask) IsCustomTask() bool {
return t.CustomTask
}
// isSuccessful returns true only if the run has completed successfully
// If the PipelineTask has a Matrix, isSuccessful returns true if all runs have completed successfully
func (t ResolvedPipelineTask) isSuccessful() bool {
switch {
case t.IsCustomTask() && t.PipelineTask.IsMatrixed():
if len(t.RunObjects) == 0 {
return false
}
for _, run := range t.RunObjects {
if !run.IsSuccessful() {
return false
}
}
return true
case t.IsCustomTask():
return t.RunObject != nil && t.RunObject.IsSuccessful()
case t.PipelineTask.IsMatrixed():
if len(t.TaskRuns) == 0 {
return false
}
for _, taskRun := range t.TaskRuns {
if !taskRun.IsSuccessful() {
return false
}
}
return true
default:
return t.TaskRun.IsSuccessful()
}
}
// isFailure returns true only if the run has failed and will not be retried.
// If the PipelineTask has a Matrix, isFailure returns true if any run has failed (no remaining retries)
// and all other runs are done.
func (t ResolvedPipelineTask) isFailure() bool {
if t.isCancelledForTimeOut() {
return true
}
if t.isCancelled() {
return true
}
if t.isSuccessful() {
return false
}
var c *apis.Condition
var isDone bool
switch {
case t.IsCustomTask() && t.PipelineTask.IsMatrixed():
if len(t.RunObjects) == 0 {
return false
}
isDone = true
atLeastOneFailed := false
for _, run := range t.RunObjects {
isDone = isDone && run.IsDone()
runFailed := run.GetStatusCondition().GetCondition(apis.ConditionSucceeded).IsFalse()
atLeastOneFailed = atLeastOneFailed || runFailed
}
return atLeastOneFailed && isDone
case t.IsCustomTask():
if t.RunObject == nil {
return false
}
c = t.RunObject.GetStatusCondition().GetCondition(apis.ConditionSucceeded)
isDone = t.RunObject.IsDone()
return isDone && c.IsFalse()
case t.PipelineTask.IsMatrixed():
if len(t.TaskRuns) == 0 {
return false
}
isDone = true
atLeastOneFailed := false
for _, taskRun := range t.TaskRuns {
isDone = isDone && taskRun.IsDone()
taskRunFailed := taskRun.Status.GetCondition(apis.ConditionSucceeded).IsFalse()
atLeastOneFailed = atLeastOneFailed || taskRunFailed
}
return atLeastOneFailed && isDone
default:
if t.TaskRun == nil {
return false
}
c = t.TaskRun.Status.GetCondition(apis.ConditionSucceeded)
isDone = t.TaskRun.IsDone()
return isDone && c.IsFalse()
}
}
// isCancelledForTimeOut returns true only if the run is cancelled due to PipelineRun-controlled timeout
// If the PipelineTask has a Matrix, isCancelled returns true if any run is cancelled due to PipelineRun-controlled timeout and all other runs are done.
func (t ResolvedPipelineTask) isCancelledForTimeOut() bool {
switch {
case t.IsCustomTask() && t.PipelineTask.IsMatrixed():
if len(t.RunObjects) == 0 {
return false
}
isDone := true
atLeastOneCancelled := false
for _, run := range t.RunObjects {
isDone = isDone && run.IsDone()
c := run.GetStatusCondition().GetCondition(apis.ConditionSucceeded)
runCancelled := c.IsFalse() &&
c.Reason == v1beta1.CustomRunReasonCancelled.String() &&
isCustomRunCancelledByPipelineRunTimeout(run)
atLeastOneCancelled = atLeastOneCancelled || runCancelled
}
return atLeastOneCancelled && isDone
case t.IsCustomTask():
if t.RunObject == nil {
return false
}
c := t.RunObject.GetStatusCondition().GetCondition(apis.ConditionSucceeded)
return c != nil && c.IsFalse() &&
c.Reason == v1beta1.CustomRunReasonCancelled.String() &&
isCustomRunCancelledByPipelineRunTimeout(t.RunObject)
case t.PipelineTask.IsMatrixed():
if len(t.TaskRuns) == 0 {
return false
}
isDone := true
atLeastOneCancelled := false
for _, taskRun := range t.TaskRuns {
isDone = isDone && taskRun.IsDone()
c := taskRun.Status.GetCondition(apis.ConditionSucceeded)
taskRunCancelled := c.IsFalse() &&
c.Reason == v1beta1.TaskRunReasonCancelled.String() &&
taskRun.Spec.StatusMessage == v1beta1.TaskRunCancelledByPipelineTimeoutMsg
atLeastOneCancelled = atLeastOneCancelled || taskRunCancelled
}
return atLeastOneCancelled && isDone
default:
if t.TaskRun == nil {
return false
}
c := t.TaskRun.Status.GetCondition(apis.ConditionSucceeded)
return c != nil && c.IsFalse() &&
c.Reason == v1beta1.TaskRunReasonCancelled.String() &&
t.TaskRun.Spec.StatusMessage == v1beta1.TaskRunCancelledByPipelineTimeoutMsg
}
}
// isCancelled returns true only if the run is cancelled
// If the PipelineTask has a Matrix, isCancelled returns true if any run is cancelled and all other runs are done.
func (t ResolvedPipelineTask) isCancelled() bool {
switch {
case t.IsCustomTask() && t.PipelineTask.IsMatrixed():
if len(t.RunObjects) == 0 {
return false
}
isDone := true
atLeastOneCancelled := false
for _, run := range t.RunObjects {
isDone = isDone && run.IsDone()
c := run.GetStatusCondition().GetCondition(apis.ConditionSucceeded)
runCancelled := c.IsFalse() && c.Reason == v1beta1.CustomRunReasonCancelled.String()
atLeastOneCancelled = atLeastOneCancelled || runCancelled
}
return atLeastOneCancelled && isDone
case t.IsCustomTask():
if t.RunObject == nil {
return false
}
c := t.RunObject.GetStatusCondition().GetCondition(apis.ConditionSucceeded)
return c != nil && c.IsFalse() && c.Reason == v1beta1.CustomRunReasonCancelled.String()
case t.PipelineTask.IsMatrixed():
if len(t.TaskRuns) == 0 {
return false
}
isDone := true
atLeastOneCancelled := false
for _, taskRun := range t.TaskRuns {
isDone = isDone && taskRun.IsDone()
c := taskRun.Status.GetCondition(apis.ConditionSucceeded)
taskRunCancelled := c.IsFalse() && c.Reason == v1beta1.TaskRunReasonCancelled.String()
atLeastOneCancelled = atLeastOneCancelled || taskRunCancelled
}
return atLeastOneCancelled && isDone
default:
if t.TaskRun == nil {
return false
}
c := t.TaskRun.Status.GetCondition(apis.ConditionSucceeded)
return c != nil && c.IsFalse() && c.Reason == v1beta1.TaskRunReasonCancelled.String()
}
}
// isScheduled returns true when the PipelineRunTask itself has a TaskRun
// or Run associated.
func (t ResolvedPipelineTask) isScheduled() bool {
if t.IsCustomTask() {
return t.RunObject != nil
}
return t.TaskRun != nil
}
// isStarted returns true only if the PipelineRunTask itself has a TaskRun or
// Run associated that has a Succeeded-type condition.
func (t ResolvedPipelineTask) isStarted() bool {
if t.IsCustomTask() {
return t.RunObject != nil && t.RunObject.GetStatusCondition().GetCondition(apis.ConditionSucceeded) != nil
}
return t.TaskRun != nil && t.TaskRun.Status.GetCondition(apis.ConditionSucceeded) != nil
}
// isConditionStatusFalse returns true when a task has succeeded condition with status set to false
// it includes task failed after retries are exhausted, cancelled tasks, and time outs
func (t ResolvedPipelineTask) isConditionStatusFalse() bool {
if t.isStarted() {
if t.IsCustomTask() {
return t.RunObject.GetStatusCondition().GetCondition(apis.ConditionSucceeded).IsFalse()
}
return t.TaskRun.Status.GetCondition(apis.ConditionSucceeded).IsFalse()
}
return false
}
func (t *ResolvedPipelineTask) checkParentsDone(facts *PipelineRunFacts) bool {
if facts.isFinalTask(t.PipelineTask.Name) {
return true
}
stateMap := facts.State.ToMap()
node := facts.TasksGraph.Nodes[t.PipelineTask.Name]
for _, p := range node.Prev {
if !stateMap[p.Key].isDone(facts) {
return false
}
}
return true
}
func (t *ResolvedPipelineTask) skip(facts *PipelineRunFacts) TaskSkipStatus {
var skippingReason v1beta1.SkippingReason
switch {
case facts.isFinalTask(t.PipelineTask.Name) || t.isScheduled():
skippingReason = v1beta1.None
case facts.IsStopping():
skippingReason = v1beta1.StoppingSkip
case facts.IsGracefullyCancelled():
skippingReason = v1beta1.GracefullyCancelledSkip
case facts.IsGracefullyStopped():
skippingReason = v1beta1.GracefullyStoppedSkip
case t.skipBecauseParentTaskWasSkipped(facts):
skippingReason = v1beta1.ParentTasksSkip
case t.skipBecauseResultReferencesAreMissing(facts):
skippingReason = v1beta1.MissingResultsSkip
case t.skipBecauseWhenExpressionsEvaluatedToFalse(facts):
skippingReason = v1beta1.WhenExpressionsSkip
case t.skipBecausePipelineRunPipelineTimeoutReached(facts):
skippingReason = v1beta1.PipelineTimedOutSkip
case t.skipBecausePipelineRunTasksTimeoutReached(facts):
skippingReason = v1beta1.TasksTimedOutSkip
case t.skipBecauseEmptyArrayInMatrixParams():
skippingReason = v1beta1.EmptyArrayInMatrixParams
default:
skippingReason = v1beta1.None
}
return TaskSkipStatus{
IsSkipped: skippingReason != v1beta1.None,
SkippingReason: skippingReason,
}
}
// Skip returns true if a PipelineTask will not be run because
// (1) its When Expressions evaluated to false
// (2) its Condition Checks failed
// (3) its parent task was skipped
// (4) Pipeline is in stopping state (one of the PipelineTasks failed)
// (5) Pipeline is gracefully cancelled or stopped
func (t *ResolvedPipelineTask) Skip(facts *PipelineRunFacts) TaskSkipStatus {
if facts.SkipCache == nil {
facts.SkipCache = make(map[string]TaskSkipStatus)
}
if _, cached := facts.SkipCache[t.PipelineTask.Name]; !cached {
facts.SkipCache[t.PipelineTask.Name] = t.skip(facts)
}
return facts.SkipCache[t.PipelineTask.Name]
}
// skipBecauseWhenExpressionsEvaluatedToFalse confirms that the when expressions have completed evaluating, and
// it returns true if any of the when expressions evaluate to false
func (t *ResolvedPipelineTask) skipBecauseWhenExpressionsEvaluatedToFalse(facts *PipelineRunFacts) bool {
if t.checkParentsDone(facts) {
if !t.PipelineTask.WhenExpressions.AllowsExecution() {
return true
}
}
return false
}
// skipBecauseParentTaskWasSkipped loops through the parent tasks and checks if the parent task skipped:
//
// if yes, is it because of when expressions?
// if yes, it ignores this parent skip and continue evaluating other parent tasks
// if no, it returns true to skip the current task because this parent task was skipped
// if no, it continues checking the other parent tasks
func (t *ResolvedPipelineTask) skipBecauseParentTaskWasSkipped(facts *PipelineRunFacts) bool {
stateMap := facts.State.ToMap()
node := facts.TasksGraph.Nodes[t.PipelineTask.Name]
for _, p := range node.Prev {
parentTask := stateMap[p.Key]
if parentSkipStatus := parentTask.Skip(facts); parentSkipStatus.IsSkipped {
// if the parent task was skipped due to its `when` expressions,
// then we should ignore that and continue evaluating if we should skip because of other parent tasks
if parentSkipStatus.SkippingReason == v1beta1.WhenExpressionsSkip {
continue
}
return true
}
}
return false
}
// skipBecauseResultReferencesAreMissing checks if the task references results that cannot be resolved, which is a
// reason for skipping the task, and applies result references if found
func (t *ResolvedPipelineTask) skipBecauseResultReferencesAreMissing(facts *PipelineRunFacts) bool {
if t.checkParentsDone(facts) && t.hasResultReferences() {
resolvedResultRefs, pt, err := ResolveResultRefs(facts.State, PipelineRunState{t})
rpt := facts.State.ToMap()[pt]
if rpt != nil {
if err != nil && (t.IsFinalTask(facts) || rpt.Skip(facts).SkippingReason == v1beta1.WhenExpressionsSkip) {
return true
}
}
ApplyTaskResults(PipelineRunState{t}, resolvedResultRefs)
facts.ResetSkippedCache()
}
return false
}
// skipBecausePipelineRunPipelineTimeoutReached returns true if the task shouldn't be launched because the elapsed time since
// the PipelineRun started is greater than the PipelineRun's pipeline timeout
func (t *ResolvedPipelineTask) skipBecausePipelineRunPipelineTimeoutReached(facts *PipelineRunFacts) bool {
if t.checkParentsDone(facts) {
if facts.TimeoutsState.PipelineTimeout != nil && *facts.TimeoutsState.PipelineTimeout != config.NoTimeoutDuration && facts.TimeoutsState.StartTime != nil {
// If the elapsed time since the PipelineRun's start time is greater than the PipelineRun's Pipeline timeout, skip.
return facts.TimeoutsState.Clock.Since(*facts.TimeoutsState.StartTime) > *facts.TimeoutsState.PipelineTimeout
}
}
return false
}
// skipBecausePipelineRunTasksTimeoutReached returns true if the task shouldn't be launched because the elapsed time since
// the PipelineRun started is greater than the PipelineRun's tasks timeout
func (t *ResolvedPipelineTask) skipBecausePipelineRunTasksTimeoutReached(facts *PipelineRunFacts) bool {
if t.checkParentsDone(facts) && !t.IsFinalTask(facts) {
if facts.TimeoutsState.TasksTimeout != nil && *facts.TimeoutsState.TasksTimeout != config.NoTimeoutDuration && facts.TimeoutsState.StartTime != nil {
// If the elapsed time since the PipelineRun's start time is greater than the PipelineRun's Tasks timeout, skip.
return facts.TimeoutsState.Clock.Since(*facts.TimeoutsState.StartTime) > *facts.TimeoutsState.TasksTimeout
}
}
return false
}
// skipBecausePipelineRunFinallyTimeoutReached returns true if the task shouldn't be launched because the elapsed time since
// finally tasks started being executed is greater than the PipelineRun's finally timeout
func (t *ResolvedPipelineTask) skipBecausePipelineRunFinallyTimeoutReached(facts *PipelineRunFacts) bool {
if t.checkParentsDone(facts) && t.IsFinalTask(facts) {
if facts.TimeoutsState.FinallyTimeout != nil && *facts.TimeoutsState.FinallyTimeout != config.NoTimeoutDuration && facts.TimeoutsState.FinallyStartTime != nil {
// If the elapsed time since the PipelineRun's finally start time is greater than the PipelineRun's finally timeout, skip.
return facts.TimeoutsState.Clock.Since(*facts.TimeoutsState.FinallyStartTime) > *facts.TimeoutsState.FinallyTimeout
}
}
return false
}
// skipBecauseEmptyArrayInMatrixParams returns true if the matrix parameters contain an empty array
func (t *ResolvedPipelineTask) skipBecauseEmptyArrayInMatrixParams() bool {
if t.PipelineTask.IsMatrixed() {
for _, ps := range t.PipelineTask.Matrix.Params {
if ps.Value.Type == v1beta1.ParamTypeArray && len(ps.Value.ArrayVal) == 0 {
return true
}
}
}
return false
}
// IsFinalTask returns true if a task is a finally task
func (t *ResolvedPipelineTask) IsFinalTask(facts *PipelineRunFacts) bool {
return facts.isFinalTask(t.PipelineTask.Name)
}
// IsFinallySkipped returns true if a finally task is not executed and skipped due to task result validation failure
func (t *ResolvedPipelineTask) IsFinallySkipped(facts *PipelineRunFacts) TaskSkipStatus {
var skippingReason v1beta1.SkippingReason
switch {
case t.isScheduled():
skippingReason = v1beta1.None
case facts.checkDAGTasksDone() && facts.isFinalTask(t.PipelineTask.Name):
switch {
case t.skipBecauseResultReferencesAreMissing(facts):
skippingReason = v1beta1.MissingResultsSkip
case t.skipBecauseWhenExpressionsEvaluatedToFalse(facts):
skippingReason = v1beta1.WhenExpressionsSkip
case t.skipBecausePipelineRunPipelineTimeoutReached(facts):
skippingReason = v1beta1.PipelineTimedOutSkip
case t.skipBecausePipelineRunFinallyTimeoutReached(facts):
skippingReason = v1beta1.FinallyTimedOutSkip
case t.skipBecauseEmptyArrayInMatrixParams():
skippingReason = v1beta1.EmptyArrayInMatrixParams
default:
skippingReason = v1beta1.None
}
default:
skippingReason = v1beta1.None
}
return TaskSkipStatus{
IsSkipped: skippingReason != v1beta1.None,
SkippingReason: skippingReason,
}
}
// GetRun is a function that will retrieve a Run by name.
type GetRun func(name string) (v1beta1.RunObject, error)
// ValidateWorkspaceBindings validates that the Workspaces expected by a Pipeline are provided by a PipelineRun.
func ValidateWorkspaceBindings(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) error {
pipelineRunWorkspaces := make(map[string]v1beta1.WorkspaceBinding)
for _, binding := range pr.Spec.Workspaces {
pipelineRunWorkspaces[binding.Name] = binding
}
for _, ws := range p.Workspaces {
if ws.Optional {
continue
}
if _, ok := pipelineRunWorkspaces[ws.Name]; !ok {
return fmt.Errorf("pipeline requires workspace with name %q be provided by pipelinerun", ws.Name)
}
}
return nil
}
// ValidateTaskRunSpecs that the TaskRunSpecs defined by a PipelineRun are correct.
func ValidateTaskRunSpecs(p *v1beta1.PipelineSpec, pr *v1beta1.PipelineRun) error {
pipelineTasks := make(map[string]string)
for _, task := range p.Tasks {
pipelineTasks[task.Name] = task.Name
}
for _, task := range p.Finally {
pipelineTasks[task.Name] = task.Name
}
for _, taskrunSpec := range pr.Spec.TaskRunSpecs {
if _, ok := pipelineTasks[taskrunSpec.PipelineTaskName]; !ok {
return fmt.Errorf("PipelineRun's taskrunSpecs defined wrong taskName: %q, does not exist in Pipeline", taskrunSpec.PipelineTaskName)
}
}
return nil
}
// ResolvePipelineTask returns a new ResolvedPipelineTask representing any TaskRuns or CustomRuns
// associated with this Pipeline Task, if they exist.
//
// If the Pipeline Task is a Task, it retrieves any TaskRuns, plus the Task spec, and updates the ResolvedPipelineTask
// with this information. It also sets the ResolvedPipelineTask's TaskRunName(s) with the names of TaskRuns
// that should be or already have been created.
//
// If the Pipeline Task is a Custom Task, it retrieves any CustomRuns and updates the ResolvedPipelineTask with this information.
// It also sets the ResolvedPipelineTask's RunName(s) with the names of CustomRuns that should be or already have been created.
func ResolvePipelineTask(
ctx context.Context,
pipelineRun v1beta1.PipelineRun,
getTask resources.GetTask,
getTaskRun resources.GetTaskRun,
getRun GetRun,
pipelineTask v1beta1.PipelineTask,
) (*ResolvedPipelineTask, error) {
rpt := ResolvedPipelineTask{
PipelineTask: &pipelineTask,
}
rpt.CustomTask = rpt.PipelineTask.TaskRef.IsCustomTask() || rpt.PipelineTask.TaskSpec.IsCustomTask()
switch {
case rpt.IsCustomTask() && rpt.PipelineTask.IsMatrixed():
rpt.RunObjectNames = getNamesOfRuns(pipelineRun.Status.ChildReferences, pipelineTask.Name, pipelineRun.Name, pipelineTask.Matrix.CountCombinations())
for _, runName := range rpt.RunObjectNames {
run, err := getRun(runName)
if err != nil && !kerrors.IsNotFound(err) {
return nil, fmt.Errorf("error retrieving RunObject %s: %w", runName, err)
}
if run != nil {
rpt.RunObjects = append(rpt.RunObjects, run)
}
}
case rpt.IsCustomTask():
rpt.RunObjectName = getRunName(pipelineRun.Status.ChildReferences, pipelineTask.Name, pipelineRun.Name)
run, err := getRun(rpt.RunObjectName)
if err != nil && !kerrors.IsNotFound(err) {
return nil, fmt.Errorf("error retrieving RunObject %s: %w", rpt.RunObjectName, err)
}
if run != nil {
rpt.RunObject = run
}
case rpt.PipelineTask.IsMatrixed():
rpt.TaskRunNames = GetNamesOfTaskRuns(pipelineRun.Status.ChildReferences, pipelineTask.Name, pipelineRun.Name, pipelineTask.Matrix.CountCombinations())
for _, taskRunName := range rpt.TaskRunNames {
if err := rpt.setTaskRunsAndResolvedTask(ctx, taskRunName, getTask, getTaskRun, pipelineTask); err != nil {
return nil, err
}
}
default:
rpt.TaskRunName = GetTaskRunName(pipelineRun.Status.ChildReferences, pipelineTask.Name, pipelineRun.Name)
if err := rpt.setTaskRunsAndResolvedTask(ctx, rpt.TaskRunName, getTask, getTaskRun, pipelineTask); err != nil {
return nil, err
}
}
return &rpt, nil
}
// setTaskRunsAndResolvedTask fetches the named TaskRun using the input function getTaskRun,
// and the resolved Task spec of the Pipeline Task using the input function getTask.
// It updates the ResolvedPipelineTask with the ResolvedTask and a pointer to the fetched TaskRun.
func (t *ResolvedPipelineTask) setTaskRunsAndResolvedTask(
ctx context.Context,
taskRunName string,
getTask resources.GetTask,
getTaskRun resources.GetTaskRun,
pipelineTask v1beta1.PipelineTask,
) error {
taskRun, err := getTaskRun(taskRunName)
if err != nil {
if !kerrors.IsNotFound(err) {
return fmt.Errorf("error retrieving TaskRun %s: %w", taskRunName, err)
}
}
if taskRun != nil {
if t.PipelineTask.IsMatrixed() {
t.TaskRuns = append(t.TaskRuns, taskRun)
} else {
t.TaskRun = taskRun
}
}
rt, err := resolveTask(ctx, taskRun, getTask, pipelineTask)
if err != nil {
return err
}
t.ResolvedTask = rt
return nil
}
// resolveTask fetches the Task spec for the PipelineTask and sets its default values.
// It returns a ResolvedTask with the defaulted spec, name, and kind (namespaced Task or Cluster Task) of the Task.
// Returns an error if the Task could not be found because resolution was in progress,
// verification failed, or any other reason.
func resolveTask(
ctx context.Context,
taskRun *v1beta1.TaskRun,
getTask resources.GetTask,
pipelineTask v1beta1.PipelineTask,
) (*resources.ResolvedTask, error) {
rt := &resources.ResolvedTask{}
if pipelineTask.TaskRef != nil {
// If the TaskRun has already a stored TaskSpec in its status, use it as source of truth
if taskRun != nil && taskRun.Status.TaskSpec != nil {
rt.TaskSpec = taskRun.Status.TaskSpec
rt.TaskName = pipelineTask.TaskRef.Name
} else {
// Following minimum status principle (TEP-0100), no need to propagate the RefSource about PipelineTask up to PipelineRun status.
// Instead, the child TaskRun's status will be the place recording the RefSource of individual task.
t, _, err := getTask(ctx, pipelineTask.TaskRef.Name)
switch {
case errors.Is(err, remote.ErrRequestInProgress):
return rt, err
case errors.Is(err, trustedresources.ErrResourceVerificationFailed):
return rt, err
case err != nil:
return rt, &TaskNotFoundError{
Name: pipelineTask.TaskRef.Name,
Msg: err.Error(),
}
default:
spec := t.TaskSpec()
rt.TaskSpec = &spec
rt.TaskName = t.TaskMetadata().Name
}
}
rt.Kind = pipelineTask.TaskRef.Kind
} else {
rt.TaskSpec = &pipelineTask.TaskSpec.TaskSpec
}
rt.TaskSpec.SetDefaults(ctx)
return rt, nil
}
// GetTaskRunName should return a unique name for a `TaskRun` if one has not already been defined, and the existing one otherwise.
func GetTaskRunName(childRefs []v1beta1.ChildStatusReference, ptName, prName string) string {
for _, cr := range childRefs {
if cr.Kind == pipeline.TaskRunControllerName && cr.PipelineTaskName == ptName {
return cr.Name
}
}
return kmeta.ChildName(prName, fmt.Sprintf("-%s", ptName))
}
// GetNamesOfTaskRuns should return unique names for `TaskRuns` if one has not already been defined, and the existing one otherwise.
func GetNamesOfTaskRuns(childRefs []v1beta1.ChildStatusReference, ptName, prName string, combinationCount int) []string {
if taskRunNames := getTaskRunNamesFromChildRefs(childRefs, ptName); taskRunNames != nil {
return taskRunNames
}
return getNewTaskRunNames(ptName, prName, combinationCount)
}
// getTaskRunNamesFromChildRefs returns the names of TaskRuns defined in childRefs that are associated with the named Pipeline Task.
func getTaskRunNamesFromChildRefs(childRefs []v1beta1.ChildStatusReference, ptName string) []string {
var taskRunNames []string
for _, cr := range childRefs {
if cr.Kind == pipeline.TaskRunControllerName && cr.PipelineTaskName == ptName {
taskRunNames = append(taskRunNames, cr.Name)
}
}
return taskRunNames
}
func getNewTaskRunNames(ptName, prName string, combinationCount int) []string {
var taskRunNames []string
for i := 0; i < combinationCount; i++ {
taskRunName := kmeta.ChildName(prName, fmt.Sprintf("-%s-%d", ptName, i))
taskRunNames = append(taskRunNames, taskRunName)
}
return taskRunNames
}
// getRunName should return a unique name for a `Run` if one has not already
// been defined, and the existing one otherwise.
func getRunName(childRefs []v1beta1.ChildStatusReference, ptName, prName string) string {
for _, cr := range childRefs {
if cr.PipelineTaskName == ptName {
if cr.Kind == pipeline.CustomRunControllerName {
return cr.Name
}
}
}
return kmeta.ChildName(prName, fmt.Sprintf("-%s", ptName))
}
// getNamesOfRuns should return a unique names for `RunObjects` if they have not already been defined,
// and the existing ones otherwise.
func getNamesOfRuns(childRefs []v1beta1.ChildStatusReference, ptName, prName string, combinationCount int) []string {
if runNames := getRunNamesFromChildRefs(childRefs, ptName); runNames != nil {
return runNames
}
return getNewTaskRunNames(ptName, prName, combinationCount)
}
// getRunNamesFromChildRefs returns the names of CustomRuns defined in childRefs that are associated with the named Pipeline Task.
func getRunNamesFromChildRefs(childRefs []v1beta1.ChildStatusReference, ptName string) []string {
var runNames []string
for _, cr := range childRefs {
if cr.PipelineTaskName == ptName {
if cr.Kind == pipeline.CustomRunControllerName {
runNames = append(runNames, cr.Name)
}
}
}
return runNames
}
func (t *ResolvedPipelineTask) hasResultReferences() bool {
var matrixParams v1beta1.Params
if t.PipelineTask.IsMatrixed() {
matrixParams = t.PipelineTask.Params
}
for _, param := range append(t.PipelineTask.Params, matrixParams...) {
if ps, ok := v1beta1.GetVarSubstitutionExpressionsForParam(param); ok {
if v1beta1.LooksLikeContainsResultRefs(ps) {
return true
}
}
}
for _, we := range t.PipelineTask.WhenExpressions {
if ps, ok := we.GetVarSubstitutionExpressions(); ok {
if v1beta1.LooksLikeContainsResultRefs(ps) {
return true
}
}
}
return false
}
func isCustomRunCancelledByPipelineRunTimeout(ro v1beta1.RunObject) bool {
cr := ro.(*v1beta1.CustomRun)
return cr.Spec.StatusMessage == v1beta1.CustomRunCancelledByPipelineTimeoutMsg
}