-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
taskresourceresolution.go
96 lines (81 loc) · 3.43 KB
/
taskresourceresolution.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
/*
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 (
"errors"
"fmt"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resourcev1alpha1 "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ResolvedTaskResources contains all the data that is needed to execute
// the TaskRun: the TaskRun, it's Task and the PipelineResources it needs.
type ResolvedTaskResources struct {
TaskName string
Kind v1beta1.TaskKind
TaskSpec *v1beta1.TaskSpec
// Inputs is a map from the name of the input required by the Task
// to the actual Resource to use for it
Inputs map[string]*resourcev1alpha1.PipelineResource
// Outputs is a map from the name of the output required by the Task
// to the actual Resource to use for it
Outputs map[string]*resourcev1alpha1.PipelineResource
}
// GetResource is a function used to retrieve PipelineResources.
type GetResource func(string) (*resourcev1alpha1.PipelineResource, error)
// ResolveTaskResources looks up PipelineResources referenced by inputs and outputs and returns
// a structure that unites the resolved references and the Task Spec. If referenced PipelineResources
// can't be found, an error is returned.
func ResolveTaskResources(ts *v1beta1.TaskSpec, taskName string, kind v1beta1.TaskKind, inputs []v1beta1.TaskResourceBinding, outputs []v1beta1.TaskResourceBinding, gr GetResource) (*ResolvedTaskResources, error) {
rtr := ResolvedTaskResources{
TaskName: taskName,
TaskSpec: ts,
Kind: kind,
Inputs: map[string]*resourcev1alpha1.PipelineResource{},
Outputs: map[string]*resourcev1alpha1.PipelineResource{},
}
for _, r := range inputs {
rr, err := GetResourceFromBinding(&r.PipelineResourceBinding, gr)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced input PipelineResource: %w", err)
}
rtr.Inputs[r.Name] = rr
}
for _, r := range outputs {
rr, err := GetResourceFromBinding(&r.PipelineResourceBinding, gr)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced output PipelineResource: %w", err)
}
rtr.Outputs[r.Name] = rr
}
return &rtr, nil
}
// GetResourceFromBinding will return an instance of a PipelineResource to use for r, either by getting it with getter or by
// instantiating it from the embedded spec.
func GetResourceFromBinding(r *v1beta1.PipelineResourceBinding, getter GetResource) (*resourcev1alpha1.PipelineResource, error) {
if (r.ResourceRef != nil && r.ResourceRef.Name != "") && r.ResourceSpec != nil {
return nil, errors.New("Both ResourseRef and ResourceSpec are defined. Expected only one")
}
if r.ResourceRef != nil && r.ResourceRef.Name != "" {
return getter(r.ResourceRef.Name)
}
if r.ResourceSpec != nil {
return &resourcev1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: r.Name,
},
Spec: *r.ResourceSpec,
}, nil
}
return nil, errors.New("Neither ResourseRef nor ResourceSpec is defined")
}