-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
resultrefresolution.go
271 lines (242 loc) · 10.2 KB
/
resultrefresolution.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
/*
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 (
"fmt"
"sort"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)
// ResolvedResultRefs represents all of the ResolvedResultRef for a pipeline task
type ResolvedResultRefs []*ResolvedResultRef
// ResolvedResultRef represents a result ref reference that has been fully resolved (value has been populated).
// If the value is from a Result, then the ResultReference will be populated to point to the ResultReference
// which resulted in the value
type ResolvedResultRef struct {
Value v1beta1.ResultValue
ResultReference v1beta1.ResultRef
FromTaskRun string
FromRun string
}
// ResolveResultRef resolves any ResultReference that are found in the target ResolvedPipelineTask
func ResolveResultRef(pipelineRunState PipelineRunState, target *ResolvedPipelineTask) (ResolvedResultRefs, string, error) {
resolvedResultRefs, pt, err := convertToResultRefs(pipelineRunState, target)
if err != nil {
return nil, pt, err
}
return validateArrayResultsIndex(removeDup(resolvedResultRefs))
}
// ResolveResultRefs resolves any ResultReference that are found in the target ResolvedPipelineTask
func ResolveResultRefs(pipelineRunState PipelineRunState, targets PipelineRunState) (ResolvedResultRefs, string, error) {
var allResolvedResultRefs ResolvedResultRefs
for _, target := range targets {
resolvedResultRefs, pt, err := convertToResultRefs(pipelineRunState, target)
if err != nil {
return nil, pt, err
}
allResolvedResultRefs = append(allResolvedResultRefs, resolvedResultRefs...)
}
return validateArrayResultsIndex(removeDup(allResolvedResultRefs))
}
// validateArrayResultsIndex checks if the result array indexing reference is out of bound of the array size
func validateArrayResultsIndex(allResolvedResultRefs ResolvedResultRefs) (ResolvedResultRefs, string, error) {
for _, r := range allResolvedResultRefs {
if r.Value.Type == v1beta1.ParamTypeArray {
if r.ResultReference.ResultsIndex >= len(r.Value.ArrayVal) {
return nil, "", fmt.Errorf("Array Result Index %d for Task %s Result %s is out of bound of size %d", r.ResultReference.ResultsIndex, r.ResultReference.PipelineTask, r.ResultReference.Result, len(r.Value.ArrayVal))
}
}
}
return allResolvedResultRefs, "", nil
}
// extractResultRefs resolves any ResultReference that are found in param or pipeline result
// Returns nil if none are found
func extractResultRefsForParam(pipelineRunState PipelineRunState, param v1beta1.Param) (ResolvedResultRefs, error) {
expressions, ok := v1beta1.GetVarSubstitutionExpressionsForParam(param)
if ok {
return extractResultRefs(expressions, pipelineRunState)
}
return nil, nil
}
func extractResultRefs(expressions []string, pipelineRunState PipelineRunState) (ResolvedResultRefs, error) {
resultRefs := v1beta1.NewResultRefs(expressions)
var resolvedResultRefs ResolvedResultRefs
for _, resultRef := range resultRefs {
resolvedResultRef, _, err := resolveResultRef(pipelineRunState, resultRef)
if err != nil {
return nil, err
}
resolvedResultRefs = append(resolvedResultRefs, resolvedResultRef)
}
return removeDup(resolvedResultRefs), nil
}
func removeDup(refs ResolvedResultRefs) ResolvedResultRefs {
if refs == nil {
return nil
}
resolvedResultRefByRef := make(map[v1beta1.ResultRef]*ResolvedResultRef, len(refs))
for _, resolvedResultRef := range refs {
resolvedResultRefByRef[resolvedResultRef.ResultReference] = resolvedResultRef
}
deduped := make([]*ResolvedResultRef, 0, len(resolvedResultRefByRef))
// Sort the resulting keys to produce a deterministic ordering.
order := make([]v1beta1.ResultRef, 0, len(refs))
for key := range resolvedResultRefByRef {
order = append(order, key)
}
sort.Slice(order, func(i, j int) bool {
if order[i].PipelineTask > order[j].PipelineTask {
return false
}
if order[i].Result > order[j].Result {
return false
}
return true
})
for _, key := range order {
deduped = append(deduped, resolvedResultRefByRef[key])
}
return deduped
}
// convertToResultRefs walks a PipelineTask looking for result references. If any are
// found they are resolved to a value by searching pipelineRunState. The list of resolved
// references are returned. If an error is encountered due to an invalid result reference
// then a nil list and error is returned instead.
func convertToResultRefs(pipelineRunState PipelineRunState, target *ResolvedPipelineTask) (ResolvedResultRefs, string, error) {
var resolvedResultRefs ResolvedResultRefs
for _, ref := range v1beta1.PipelineTaskResultRefs(target.PipelineTask) {
resolved, pt, err := resolveResultRef(pipelineRunState, ref)
if err != nil {
return nil, pt, err
}
resolvedResultRefs = append(resolvedResultRefs, resolved)
}
return resolvedResultRefs, "", nil
}
func resolveResultRef(pipelineState PipelineRunState, resultRef *v1beta1.ResultRef) (*ResolvedResultRef, string, error) {
referencedPipelineTask := pipelineState.ToMap()[resultRef.PipelineTask]
if referencedPipelineTask == nil {
return nil, resultRef.PipelineTask, fmt.Errorf("could not find task %q referenced by result", resultRef.PipelineTask)
}
if !referencedPipelineTask.isSuccessful() {
return nil, resultRef.PipelineTask, fmt.Errorf("task %q referenced by result was not successful", referencedPipelineTask.PipelineTask.Name)
}
var runName, runValue, taskRunName string
var resultValue v1beta1.ResultValue
var err error
if referencedPipelineTask.IsCustomTask() {
runName = referencedPipelineTask.Run.Name
runValue, err = findRunResultForParam(referencedPipelineTask.Run, resultRef)
resultValue = *v1beta1.NewStructuredValues(runValue)
if err != nil {
return nil, resultRef.PipelineTask, err
}
} else {
taskRunName = referencedPipelineTask.TaskRun.Name
resultValue, err = findTaskResultForParam(referencedPipelineTask.TaskRun, resultRef)
if err != nil {
return nil, resultRef.PipelineTask, err
}
}
return &ResolvedResultRef{
Value: resultValue,
FromTaskRun: taskRunName,
FromRun: runName,
ResultReference: *resultRef,
}, "", nil
}
func findRunResultForParam(run *v1alpha1.Run, reference *v1beta1.ResultRef) (string, error) {
results := run.Status.Results
for _, result := range results {
if result.Name == reference.Result {
return result.Value, nil
}
}
return "", fmt.Errorf("Could not find result with name %s for task %s", reference.Result, reference.PipelineTask)
}
func findTaskResultForParam(taskRun *v1beta1.TaskRun, reference *v1beta1.ResultRef) (v1beta1.ResultValue, error) {
results := taskRun.Status.TaskRunStatusFields.TaskRunResults
for _, result := range results {
if result.Name == reference.Result {
return result.Value, nil
}
}
return v1beta1.ResultValue{}, fmt.Errorf("Could not find result with name %s for task %s", reference.Result, reference.PipelineTask)
}
func (rs ResolvedResultRefs) getStringReplacements() map[string]string {
replacements := map[string]string{}
for _, r := range rs {
switch r.Value.Type {
case v1beta1.ParamTypeArray:
for i := 0; i < len(r.Value.ArrayVal); i++ {
for _, target := range r.getReplaceTargetfromArrayIndex(i) {
replacements[target] = r.Value.ArrayVal[i]
}
}
case v1beta1.ParamTypeObject:
for key, element := range r.Value.ObjectVal {
for _, target := range r.getReplaceTargetfromObjectKey(key) {
replacements[target] = element
}
}
default:
for _, target := range r.getReplaceTarget() {
replacements[target] = r.Value.StringVal
}
}
}
return replacements
}
func (rs ResolvedResultRefs) getArrayReplacements() map[string][]string {
replacements := map[string][]string{}
for _, r := range rs {
if r.Value.Type == v1beta1.ParamType(v1beta1.ResultsTypeArray) {
for _, target := range r.getReplaceTarget() {
replacements[target] = r.Value.ArrayVal
}
}
}
return replacements
}
func (rs ResolvedResultRefs) getObjectReplacements() map[string]map[string]string {
replacements := map[string]map[string]string{}
for _, r := range rs {
if r.Value.Type == v1beta1.ParamType(v1beta1.ResultsTypeObject) {
for _, target := range r.getReplaceTarget() {
replacements[target] = r.Value.ObjectVal
}
}
}
return replacements
}
func (r *ResolvedResultRef) getReplaceTarget() []string {
return []string{
fmt.Sprintf("%s.%s.%s.%s", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result),
fmt.Sprintf("%s.%s.%s[%q]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result),
fmt.Sprintf("%s.%s.%s['%s']", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result),
}
}
func (r *ResolvedResultRef) getReplaceTargetfromArrayIndex(idx int) []string {
return []string{
fmt.Sprintf("%s.%s.%s.%s[%d]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result, idx),
fmt.Sprintf("%s.%s.%s[%q][%d]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result, idx),
fmt.Sprintf("%s.%s.%s['%s'][%d]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result, idx),
}
}
func (r *ResolvedResultRef) getReplaceTargetfromObjectKey(key string) []string {
return []string{
fmt.Sprintf("%s.%s.%s.%s.%s", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result, key),
fmt.Sprintf("%s.%s.%s[%q][%s]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result, key),
fmt.Sprintf("%s.%s.%s['%s'][%s]", v1beta1.ResultTaskPart, r.ResultReference.PipelineTask, v1beta1.ResultResultPart, r.ResultReference.Result, key),
}
}