@@ -96,7 +96,7 @@ func (t ResolvedPipelineRunTask) IsSuccessful() bool {
96
96
return false
97
97
}
98
98
99
- // IsFailed returns true only if the taskrun itself has failed
99
+ // IsFailure returns true only if the taskrun itself has failed
100
100
func (t ResolvedPipelineRunTask ) IsFailure () bool {
101
101
if t .TaskRun == nil {
102
102
return false
@@ -169,12 +169,24 @@ func (state PipelineRunState) SuccessfulPipelineTaskNames() []string {
169
169
// GetTaskRun is a function that will retrieve the TaskRun name.
170
170
type GetTaskRun func (name string ) (* v1alpha1.TaskRun , error )
171
171
172
- // GetResourcesFromBindings will validate that all PipelineResources declared in Pipeline p are bound in PipelineRun pr
173
- // and if so, will return a map from the declared name of the PipelineResource (which is how the PipelineResource will
174
- // be referred to in the PipelineRun) to the ResourceRef .
175
- func GetResourcesFromBindings (p * v1alpha1.Pipeline , pr * v1alpha1. PipelineRun ) (map [string ]v1alpha1.PipelineResourceRef , error ) {
176
- resources := map [string ]v1alpha1.PipelineResourceRef {}
172
+ // GetResourcesFromBindings will retreive all Resources bound in PipelineRun pr and return a map
173
+ // from the declared name of the PipelineResource (which is how the PipelineResource will
174
+ // be referred to in the PipelineRun) to the PipelineResource, obtained via getResource .
175
+ func GetResourcesFromBindings (pr * v1alpha1.PipelineRun , getResource resources. GetResource ) (map [string ]* v1alpha1.PipelineResource , error ) {
176
+ resources := map [string ]* v1alpha1.PipelineResource {}
177
177
178
+ for _ , resource := range pr .Spec .Resources {
179
+ r , err := getResource (resource .ResourceRef .Name )
180
+ if err != nil {
181
+ return resources , xerrors .Errorf ("Error following resource reference for %s: %w" , resource .Name , err )
182
+ }
183
+ resources [resource .Name ] = r
184
+ }
185
+ return resources , nil
186
+ }
187
+
188
+ // ValidateResourceBindings validate that the PipelineResources declared in Pipeline p are bound in PipelineRun.
189
+ func ValidateResourceBindings (p * v1alpha1.Pipeline , pr * v1alpha1.PipelineRun ) error {
178
190
required := make ([]string , 0 , len (p .Spec .Resources ))
179
191
for _ , resource := range p .Spec .Resources {
180
192
required = append (required , resource .Name )
@@ -183,42 +195,10 @@ func GetResourcesFromBindings(p *v1alpha1.Pipeline, pr *v1alpha1.PipelineRun) (m
183
195
for _ , resource := range pr .Spec .Resources {
184
196
provided = append (provided , resource .Name )
185
197
}
186
- err := list .IsSame (required , provided )
187
- if err != nil {
188
- return resources , xerrors .Errorf ("PipelineRun bound resources didn't match Pipeline: %w" , err )
198
+ if err := list .IsSame (required , provided ); err != nil {
199
+ return xerrors .Errorf ("PipelineRun bound resources didn't match Pipeline: %w" , err )
189
200
}
190
-
191
- for _ , resource := range pr .Spec .Resources {
192
- resources [resource .Name ] = resource .ResourceRef
193
- }
194
- return resources , nil
195
- }
196
-
197
- func getPipelineRunTaskResources (pt v1alpha1.PipelineTask , providedResources map [string ]v1alpha1.PipelineResourceRef ) ([]v1alpha1.TaskResourceBinding , []v1alpha1.TaskResourceBinding , error ) {
198
- inputs , outputs := []v1alpha1.TaskResourceBinding {}, []v1alpha1.TaskResourceBinding {}
199
- if pt .Resources != nil {
200
- for _ , taskInput := range pt .Resources .Inputs {
201
- resource , ok := providedResources [taskInput .Resource ]
202
- if ! ok {
203
- return inputs , outputs , xerrors .Errorf ("pipelineTask tried to use input resource %s not present in declared resources" , taskInput .Resource )
204
- }
205
- inputs = append (inputs , v1alpha1.TaskResourceBinding {
206
- Name : taskInput .Name ,
207
- ResourceRef : resource ,
208
- })
209
- }
210
- for _ , taskOutput := range pt .Resources .Outputs {
211
- resource , ok := providedResources [taskOutput .Resource ]
212
- if ! ok {
213
- return outputs , outputs , xerrors .Errorf ("pipelineTask tried to use output resource %s not present in declared resources" , taskOutput .Resource )
214
- }
215
- outputs = append (outputs , v1alpha1.TaskResourceBinding {
216
- Name : taskOutput .Name ,
217
- ResourceRef : resource ,
218
- })
219
- }
220
- }
221
- return inputs , outputs , nil
201
+ return nil
222
202
}
223
203
224
204
// TaskNotFoundError indicates that the resolution failed because a referenced Task couldn't be retrieved
@@ -231,15 +211,6 @@ func (e *TaskNotFoundError) Error() string {
231
211
return fmt .Sprintf ("Couldn't retrieve Task %q: %s" , e .Name , e .Msg )
232
212
}
233
213
234
- // ResourceNotFoundError indicates that the resolution failed because a referenced PipelineResource couldn't be retrieved
235
- type ResourceNotFoundError struct {
236
- Msg string
237
- }
238
-
239
- func (e * ResourceNotFoundError ) Error () string {
240
- return fmt .Sprintf ("Couldn't retrieve PipelineResource: %s" , e .Msg )
241
- }
242
-
243
214
type ConditionNotFoundError struct {
244
215
Name string
245
216
Msg string
@@ -252,17 +223,15 @@ func (e *ConditionNotFoundError) Error() string {
252
223
// ResolvePipelineRun retrieves all Tasks instances which are reference by tasks, getting
253
224
// instances from getTask. If it is unable to retrieve an instance of a referenced Task, it
254
225
// will return an error, otherwise it returns a list of all of the Tasks retrieved.
255
- // It will retrieve the Resources needed for the TaskRun as well using getResource and the mapping
256
- // of providedResources.
226
+ // It will retrieve the Resources needed for the TaskRun using the mapping of providedResources.
257
227
func ResolvePipelineRun (
258
228
pipelineRun v1alpha1.PipelineRun ,
259
229
getTask resources.GetTask ,
260
230
getTaskRun resources.GetTaskRun ,
261
231
getClusterTask resources.GetClusterTask ,
262
- getResource resources.GetResource ,
263
232
getCondition GetCondition ,
264
233
tasks []v1alpha1.PipelineTask ,
265
- providedResources map [string ]v1alpha1.PipelineResourceRef ,
234
+ providedResources map [string ]* v1alpha1.PipelineResource ,
266
235
) (PipelineRunState , error ) {
267
236
268
237
state := []* ResolvedPipelineRunTask {}
@@ -289,16 +258,10 @@ func ResolvePipelineRun(
289
258
}
290
259
}
291
260
292
- // Get all the resources that this task will be using, if any
293
- inputs , outputs , err := getPipelineRunTaskResources (pt , providedResources )
294
- if err != nil {
295
- return nil , xerrors .Errorf ("unexpected error which should have been caught by Pipeline webhook: %w" , err )
296
- }
297
-
298
261
spec := t .TaskSpec ()
299
- rtr , err := resources . ResolveTaskResources ( & spec , t .TaskMetadata ().Name , pt .TaskRef .Kind , inputs , outputs , getResource )
262
+ rtr , err := ResolvePipelineTaskResources ( pt , & spec , t .TaskMetadata ().Name , pt .TaskRef .Kind , providedResources )
300
263
if err != nil {
301
- return nil , & ResourceNotFoundError { Msg : err . Error ()}
264
+ return nil , xerrors . Errorf ( "couldn't match referenced resources with declared resources: %w" , err )
302
265
}
303
266
rprt .ResolvedTaskResources = rtr
304
267
@@ -314,7 +277,7 @@ func ResolvePipelineRun(
314
277
315
278
// Get all conditions that this pipelineTask will be using, if any
316
279
if len (pt .Conditions ) > 0 {
317
- rcc , err := resolveConditionChecks (& pt , pipelineRun .Status .TaskRuns , rprt .TaskRunName , getTaskRun , getCondition , getResource , providedResources )
280
+ rcc , err := resolveConditionChecks (& pt , pipelineRun .Status .TaskRuns , rprt .TaskRunName , getTaskRun , getCondition , providedResources )
318
281
if err != nil {
319
282
return nil , err
320
283
}
@@ -360,7 +323,6 @@ func GetPipelineConditionStatus(pr *v1alpha1.PipelineRun, state PipelineRunState
360
323
// 2. Any one TaskRun has failed - >Failed. This should change with #1020 and #1023
361
324
// 3. All tasks are done or are skipped (i.e. condition check failed).-> Success
362
325
// 4. A Task or Condition is running right now or there are things left to run -> Running
363
-
364
326
if pr .IsTimedOut () {
365
327
return & apis.Condition {
366
328
Type : apis .ConditionSucceeded ,
@@ -489,10 +451,7 @@ func ValidateFrom(state PipelineRunState) error {
489
451
return nil
490
452
}
491
453
492
- func resolveConditionChecks (pt * v1alpha1.PipelineTask ,
493
- taskRunStatus map [string ]* v1alpha1.PipelineRunTaskRunStatus ,
494
- taskRunName string , getTaskRun resources.GetTaskRun , getCondition GetCondition ,
495
- getResource resources.GetResource , providedResources map [string ]v1alpha1.PipelineResourceRef ) ([]* ResolvedConditionCheck , error ) {
454
+ func resolveConditionChecks (pt * v1alpha1.PipelineTask , taskRunStatus map [string ]* v1alpha1.PipelineRunTaskRunStatus , taskRunName string , getTaskRun resources.GetTaskRun , getCondition GetCondition , providedResources map [string ]* v1alpha1.PipelineResource ) ([]* ResolvedConditionCheck , error ) {
496
455
rccs := []* ResolvedConditionCheck {}
497
456
for _ , ptc := range pt .Conditions {
498
457
cName := ptc .ConditionRef
@@ -510,47 +469,53 @@ func resolveConditionChecks(pt *v1alpha1.PipelineTask,
510
469
return nil , xerrors .Errorf ("error retrieving ConditionCheck %s for taskRun name %s : %w" , conditionCheckName , taskRunName , err )
511
470
}
512
471
}
472
+ conditionResources := map [string ]* v1alpha1.PipelineResource {}
473
+ for _ , declared := range ptc .Resources {
474
+ r , ok := providedResources [declared .Resource ]
475
+ if ! ok {
476
+ return nil , xerrors .Errorf ("resources %s missing for condition %s in pipeline task %s" , declared .Resource , cName , pt .Name )
477
+ }
478
+ conditionResources [declared .Name ] = r
479
+ }
513
480
514
481
rcc := ResolvedConditionCheck {
515
482
Condition : c ,
516
483
ConditionCheckName : conditionCheckName ,
517
484
ConditionCheck : v1alpha1 .NewConditionCheck (cctr ),
518
485
PipelineTaskCondition : & ptc ,
519
- }
520
-
521
- if len (ptc .Resources ) > 0 {
522
- r , err := resolveConditionResources (ptc .Resources , getResource , providedResources )
523
- if err != nil {
524
- return nil , xerrors .Errorf ("cloud not resolve resources for condition %s in pipeline task %s: %w" , cName , pt .Name , err )
525
- }
526
- rcc .ResolvedResources = r
486
+ ResolvedResources : conditionResources ,
527
487
}
528
488
529
489
rccs = append (rccs , & rcc )
530
490
}
531
491
return rccs , nil
532
492
}
533
493
534
- func resolveConditionResources (prc []v1alpha1.PipelineConditionResource ,
535
- getResource resources.GetResource ,
536
- providedResources map [string ]v1alpha1.PipelineResourceRef ,
537
- ) (map [string ]* v1alpha1.PipelineResource , error ) {
538
- rr := make (map [string ]* v1alpha1.PipelineResource )
539
- for _ , r := range prc {
540
- // First get a ref to actual resource name from its bound name
541
- resourceRef , ok := providedResources [r .Resource ]
542
- if ! ok {
543
- return nil , xerrors .Errorf ("resource %s not present in declared resources" , r .Resource )
494
+ // ResolvePipelineTaskResources matches PipelineResources referenced by pt inputs and outputs with the
495
+ // providedResources and returns an instance of ResolvedTaskResources.
496
+ func ResolvePipelineTaskResources (pt v1alpha1.PipelineTask , ts * v1alpha1.TaskSpec , taskName string , kind v1alpha1.TaskKind , providedResources map [string ]* v1alpha1.PipelineResource ) (* resources.ResolvedTaskResources , error ) {
497
+ rtr := resources.ResolvedTaskResources {
498
+ TaskName : taskName ,
499
+ TaskSpec : ts ,
500
+ Kind : kind ,
501
+ Inputs : map [string ]* v1alpha1.PipelineResource {},
502
+ Outputs : map [string ]* v1alpha1.PipelineResource {},
503
+ }
504
+ if pt .Resources != nil {
505
+ for _ , taskInput := range pt .Resources .Inputs {
506
+ resource , ok := providedResources [taskInput .Resource ]
507
+ if ! ok {
508
+ return nil , xerrors .Errorf ("pipelineTask tried to use input resource %s not present in declared resources" , taskInput .Resource )
509
+ }
510
+ rtr .Inputs [taskInput .Name ] = resource
544
511
}
545
-
546
- // Next, fetch the actual resource definition
547
- gotResource , err := getResource (resourceRef .Name )
548
- if err != nil {
549
- return nil , xerrors .Errorf ("could not retrieve resource %s: %w" , r .Name , err )
512
+ for _ , taskOutput := range pt .Resources .Outputs {
513
+ resource , ok := providedResources [taskOutput .Resource ]
514
+ if ! ok {
515
+ return nil , xerrors .Errorf ("pipelineTask tried to use output resource %s not present in declared resources" , taskOutput .Resource )
516
+ }
517
+ rtr .Outputs [taskOutput .Name ] = resource
550
518
}
551
-
552
- // Finally add it to the resolved resources map
553
- rr [r .Name ] = gotResource
554
519
}
555
- return rr , nil
520
+ return & rtr , nil
556
521
}
0 commit comments