-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathapply.go
103 lines (85 loc) · 3.98 KB
/
apply.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
/*
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"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)
// ApplyParameters applies the params from a PipelineRun.Params to a PipelineSpec.
func ApplyParameters(p *v1alpha1.PipelineSpec, pr *v1alpha1.PipelineRun) *v1alpha1.PipelineSpec {
// This assumes that the PipelineRun inputs have been validated against what the Pipeline requests.
// stringReplacements is used for standard single-string stringReplacements, while arrayReplacements contains arrays
// that need to be further processed.
stringReplacements := map[string]string{}
arrayReplacements := map[string][]string{}
// Set all the default stringReplacements
for _, p := range p.Params {
if p.Default != nil {
if p.Default.Type == v1alpha1.ParamTypeString {
stringReplacements[fmt.Sprintf("params.%s", p.Name)] = p.Default.StringVal
} else {
arrayReplacements[fmt.Sprintf("params.%s", p.Name)] = p.Default.ArrayVal
}
}
}
// Set and overwrite params with the ones from the PipelineRun
for _, p := range pr.Spec.Params {
if p.Value.Type == v1alpha1.ParamTypeString {
stringReplacements[fmt.Sprintf("params.%s", p.Name)] = p.Value.StringVal
} else {
arrayReplacements[fmt.Sprintf("params.%s", p.Name)] = p.Value.ArrayVal
}
}
return ApplyReplacements(p, stringReplacements, arrayReplacements)
}
// ApplyTaskResults applies the ResolvedResultRef to each PipelineTask.Params in targets
func ApplyTaskResults(targets PipelineRunState, resolvedResultRefs ResolvedResultRefs) {
stringReplacements := map[string]string{}
for _, resolvedResultRef := range resolvedResultRefs {
replaceTarget := fmt.Sprintf("%s.%s.%s.%s", v1beta1.ResultTaskPart, resolvedResultRef.ResultReference.PipelineTask, v1beta1.ResultResultPart, resolvedResultRef.ResultReference.Result)
stringReplacements[replaceTarget] = resolvedResultRef.Value.StringVal
}
for _, resolvedPipelineRunTask := range targets {
// also make substitution for resolved condition checks
for _, resolvedConditionCheck := range resolvedPipelineRunTask.ResolvedConditionChecks {
pipelineTaskCondition := resolvedConditionCheck.PipelineTaskCondition.DeepCopy()
pipelineTaskCondition.Params = replaceParamValues(pipelineTaskCondition.Params, stringReplacements, nil)
resolvedConditionCheck.PipelineTaskCondition = pipelineTaskCondition
}
if resolvedPipelineRunTask.PipelineTask != nil {
pipelineTask := resolvedPipelineRunTask.PipelineTask.DeepCopy()
pipelineTask.Params = replaceParamValues(pipelineTask.Params, stringReplacements, nil)
resolvedPipelineRunTask.PipelineTask = pipelineTask
}
}
}
// ApplyReplacements replaces placeholders for declared parameters with the specified replacements.
func ApplyReplacements(p *v1alpha1.PipelineSpec, replacements map[string]string, arrayReplacements map[string][]string) *v1alpha1.PipelineSpec {
p = p.DeepCopy()
tasks := p.Tasks
for i := range tasks {
tasks[i].Params = replaceParamValues(tasks[i].Params, replacements, arrayReplacements)
for j := range tasks[i].Conditions {
c := tasks[i].Conditions[j]
c.Params = replaceParamValues(c.Params, replacements, arrayReplacements)
}
}
return p
}
func replaceParamValues(params []v1alpha1.Param, stringReplacements map[string]string, arrayReplacements map[string][]string) []v1alpha1.Param {
for i := range params {
params[i].Value.ApplyReplacements(stringReplacements, arrayReplacements)
}
return params
}