-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathapply.go
333 lines (298 loc) · 12.9 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
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
/*
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"
"fmt"
"path/filepath"
"strconv"
"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/container"
"github.com/tektoncd/pipeline/pkg/pod"
"github.com/tektoncd/pipeline/pkg/substitution"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
const (
// objectIndividualVariablePattern is the reference pattern for object individual keys params.<object_param_name>.<key_name>
objectIndividualVariablePattern = "params.%s.%s"
)
var (
paramPatterns = []string{
"params.%s",
"params[%q]",
"params['%s']",
// FIXME(vdemeester) Remove that with deprecating v1beta1
"inputs.params.%s",
}
)
// ApplyParameters applies the params from a TaskRun.Input.Parameters to a TaskSpec
func ApplyParameters(ctx context.Context, spec *v1beta1.TaskSpec, tr *v1beta1.TaskRun, defaults ...v1beta1.ParamSpec) *v1beta1.TaskSpec {
// This assumes that the TaskRun inputs have been validated against what the Task 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 defaults {
if p.Default != nil {
switch p.Default.Type {
case v1beta1.ParamTypeArray:
for _, pattern := range paramPatterns {
// array indexing for param is beta feature
if config.CheckAlphaOrBetaAPIFields(ctx) {
for i := 0; i < len(p.Default.ArrayVal); i++ {
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Default.ArrayVal[i]
}
}
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Default.ArrayVal
}
case v1beta1.ParamTypeObject:
for k, v := range p.Default.ObjectVal {
stringReplacements[fmt.Sprintf(objectIndividualVariablePattern, p.Name, k)] = v
}
case v1beta1.ParamTypeString:
fallthrough
default:
for _, pattern := range paramPatterns {
stringReplacements[fmt.Sprintf(pattern, p.Name)] = p.Default.StringVal
}
}
}
}
// Set and overwrite params with the ones from the TaskRun
trStrings, trArrays := paramsFromTaskRun(ctx, tr)
for k, v := range trStrings {
stringReplacements[k] = v
}
for k, v := range trArrays {
arrayReplacements[k] = v
}
return ApplyReplacements(spec, stringReplacements, arrayReplacements)
}
func paramsFromTaskRun(ctx context.Context, tr *v1beta1.TaskRun) (map[string]string, map[string][]string) {
// 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{}
for _, p := range tr.Spec.Params {
switch p.Value.Type {
case v1beta1.ParamTypeArray:
for _, pattern := range paramPatterns {
// array indexing for param is beta feature
if config.CheckAlphaOrBetaAPIFields(ctx) {
for i := 0; i < len(p.Value.ArrayVal); i++ {
stringReplacements[fmt.Sprintf(pattern+"[%d]", p.Name, i)] = p.Value.ArrayVal[i]
}
}
arrayReplacements[fmt.Sprintf(pattern, p.Name)] = p.Value.ArrayVal
}
case v1beta1.ParamTypeObject:
for k, v := range p.Value.ObjectVal {
stringReplacements[fmt.Sprintf(objectIndividualVariablePattern, p.Name, k)] = v
}
case v1beta1.ParamTypeString:
fallthrough
default:
for _, pattern := range paramPatterns {
stringReplacements[fmt.Sprintf(pattern, p.Name)] = p.Value.StringVal
}
}
}
return stringReplacements, arrayReplacements
}
func getContextReplacements(taskName string, tr *v1beta1.TaskRun) map[string]string {
return map[string]string{
"context.taskRun.name": tr.Name,
"context.task.name": taskName,
"context.taskRun.namespace": tr.Namespace,
"context.taskRun.uid": string(tr.ObjectMeta.UID),
"context.task.retry-count": strconv.Itoa(len(tr.Status.RetriesStatus)),
}
}
// ApplyContexts applies the substitution from $(context.(taskRun|task).*) with the specified values.
// Uses "" as a default if a value is not available.
func ApplyContexts(spec *v1beta1.TaskSpec, taskName string, tr *v1beta1.TaskRun) *v1beta1.TaskSpec {
return ApplyReplacements(spec, getContextReplacements(taskName, tr), map[string][]string{})
}
// ApplyWorkspaces applies the substitution from paths that the workspaces in declarations mounted to, the
// volumes that bindings are realized with in the task spec and the PersistentVolumeClaim names for the
// workspaces.
func ApplyWorkspaces(ctx context.Context, spec *v1beta1.TaskSpec, declarations []v1beta1.WorkspaceDeclaration, bindings []v1beta1.WorkspaceBinding, vols map[string]corev1.Volume) *v1beta1.TaskSpec {
stringReplacements := map[string]string{}
bindNames := sets.NewString()
for _, binding := range bindings {
bindNames.Insert(binding.Name)
}
for _, declaration := range declarations {
prefix := fmt.Sprintf("workspaces.%s.", declaration.Name)
if declaration.Optional && !bindNames.Has(declaration.Name) {
stringReplacements[prefix+"bound"] = "false"
stringReplacements[prefix+"path"] = ""
} else {
stringReplacements[prefix+"bound"] = "true"
alphaAPIEnabled := config.FromContextOrDefaults(ctx).FeatureFlags.EnableAPIFields == config.AlphaAPIFields
if alphaAPIEnabled {
spec = applyWorkspaceMountPath(prefix+"path", spec, declaration)
} else {
stringReplacements[prefix+"path"] = declaration.GetMountPath()
}
}
}
for name, vol := range vols {
stringReplacements[fmt.Sprintf("workspaces.%s.volume", name)] = vol.Name
}
for _, binding := range bindings {
if binding.PersistentVolumeClaim != nil {
stringReplacements[fmt.Sprintf("workspaces.%s.claim", binding.Name)] = binding.PersistentVolumeClaim.ClaimName
} else {
stringReplacements[fmt.Sprintf("workspaces.%s.claim", binding.Name)] = ""
}
}
return ApplyReplacements(spec, stringReplacements, map[string][]string{})
}
// applyWorkspaceMountPath accepts a workspace path variable of the form $(workspaces.foo.path) and replaces
// it in the fields of the TaskSpec. A new updated TaskSpec is returned. Steps or Sidecars in the TaskSpec
// that override the mountPath will receive that mountPath in place of the variable's value. Other Steps and
// Sidecars will see either the workspace's declared mountPath or the default of /workspaces/<name>.
func applyWorkspaceMountPath(variable string, spec *v1beta1.TaskSpec, declaration v1beta1.WorkspaceDeclaration) *v1beta1.TaskSpec {
stringReplacements := map[string]string{variable: ""}
emptyArrayReplacements := map[string][]string{}
defaultMountPath := declaration.GetMountPath()
// Replace instances of the workspace path variable that are overridden per-Step
for i := range spec.Steps {
step := &spec.Steps[i]
for _, usage := range step.Workspaces {
if usage.Name == declaration.Name && usage.MountPath != "" {
stringReplacements[variable] = usage.MountPath
container.ApplyStepReplacements(step, stringReplacements, emptyArrayReplacements)
}
}
}
// Replace instances of the workspace path variable that are overridden per-Sidecar
for i := range spec.Sidecars {
sidecar := &spec.Sidecars[i]
for _, usage := range sidecar.Workspaces {
if usage.Name == declaration.Name && usage.MountPath != "" {
stringReplacements[variable] = usage.MountPath
container.ApplySidecarReplacements(sidecar, stringReplacements, emptyArrayReplacements)
}
}
}
// Replace any remaining instances of the workspace path variable, which should fall
// back to the mount path specified in the declaration.
stringReplacements[variable] = defaultMountPath
return ApplyReplacements(spec, stringReplacements, emptyArrayReplacements)
}
// ApplyTaskResults applies the substitution from values in results which are referenced in spec as subitems
// of the replacementStr.
func ApplyTaskResults(spec *v1beta1.TaskSpec) *v1beta1.TaskSpec {
stringReplacements := map[string]string{}
patterns := []string{
"results.%s.path",
"results[%q].path",
"results['%s'].path",
}
for _, result := range spec.Results {
for _, pattern := range patterns {
stringReplacements[fmt.Sprintf(pattern, result.Name)] = filepath.Join(pipeline.DefaultResultPath, result.Name)
}
}
return ApplyReplacements(spec, stringReplacements, map[string][]string{})
}
// ApplyStepExitCodePath replaces the occurrences of exitCode path with the absolute tekton internal path
// Replace $(steps.<step-name>.exitCode.path) with pipeline.StepPath/<step-name>/exitCode
func ApplyStepExitCodePath(spec *v1beta1.TaskSpec) *v1beta1.TaskSpec {
stringReplacements := map[string]string{}
for i, step := range spec.Steps {
stringReplacements[fmt.Sprintf("steps.%s.exitCode.path", pod.StepName(step.Name, i))] =
filepath.Join(pipeline.StepsDir, pod.StepName(step.Name, i), "exitCode")
}
return ApplyReplacements(spec, stringReplacements, map[string][]string{})
}
// ApplyCredentialsPath applies a substitution of the key $(credentials.path) with the path that credentials
// from annotated secrets are written to.
func ApplyCredentialsPath(spec *v1beta1.TaskSpec, path string) *v1beta1.TaskSpec {
stringReplacements := map[string]string{
"credentials.path": path,
}
return ApplyReplacements(spec, stringReplacements, map[string][]string{})
}
// ApplyReplacements replaces placeholders for declared parameters with the specified replacements.
func ApplyReplacements(spec *v1beta1.TaskSpec, stringReplacements map[string]string, arrayReplacements map[string][]string) *v1beta1.TaskSpec {
spec = spec.DeepCopy()
// Apply variable expansion to steps fields.
steps := spec.Steps
for i := range steps {
container.ApplyStepReplacements(&steps[i], stringReplacements, arrayReplacements)
}
// Apply variable expansion to stepTemplate fields.
if spec.StepTemplate != nil {
container.ApplyStepTemplateReplacements(spec.StepTemplate, stringReplacements, arrayReplacements)
}
// Apply variable expansion to the build's volumes
for i, v := range spec.Volumes {
spec.Volumes[i].Name = substitution.ApplyReplacements(v.Name, stringReplacements)
if v.VolumeSource.ConfigMap != nil {
spec.Volumes[i].ConfigMap.Name = substitution.ApplyReplacements(v.ConfigMap.Name, stringReplacements)
for index, item := range v.ConfigMap.Items {
spec.Volumes[i].ConfigMap.Items[index].Key = substitution.ApplyReplacements(item.Key, stringReplacements)
spec.Volumes[i].ConfigMap.Items[index].Path = substitution.ApplyReplacements(item.Path, stringReplacements)
}
}
if v.VolumeSource.Secret != nil {
spec.Volumes[i].Secret.SecretName = substitution.ApplyReplacements(v.Secret.SecretName, stringReplacements)
for index, item := range v.Secret.Items {
spec.Volumes[i].Secret.Items[index].Key = substitution.ApplyReplacements(item.Key, stringReplacements)
spec.Volumes[i].Secret.Items[index].Path = substitution.ApplyReplacements(item.Path, stringReplacements)
}
}
if v.PersistentVolumeClaim != nil {
spec.Volumes[i].PersistentVolumeClaim.ClaimName = substitution.ApplyReplacements(v.PersistentVolumeClaim.ClaimName, stringReplacements)
}
if v.Projected != nil {
for _, s := range spec.Volumes[i].Projected.Sources {
if s.ConfigMap != nil {
s.ConfigMap.Name = substitution.ApplyReplacements(s.ConfigMap.Name, stringReplacements)
}
if s.Secret != nil {
s.Secret.Name = substitution.ApplyReplacements(s.Secret.Name, stringReplacements)
}
if s.ServiceAccountToken != nil {
s.ServiceAccountToken.Audience = substitution.ApplyReplacements(s.ServiceAccountToken.Audience, stringReplacements)
}
}
}
if v.CSI != nil {
if v.CSI.NodePublishSecretRef != nil {
spec.Volumes[i].CSI.NodePublishSecretRef.Name = substitution.ApplyReplacements(v.CSI.NodePublishSecretRef.Name, stringReplacements)
}
if v.CSI.VolumeAttributes != nil {
for key, value := range v.CSI.VolumeAttributes {
spec.Volumes[i].CSI.VolumeAttributes[key] = substitution.ApplyReplacements(value, stringReplacements)
}
}
}
}
for i, v := range spec.Workspaces {
spec.Workspaces[i].MountPath = substitution.ApplyReplacements(v.MountPath, stringReplacements)
}
// Apply variable substitution to the sidecar definitions
sidecars := spec.Sidecars
for i := range sidecars {
container.ApplySidecarReplacements(&sidecars[i], stringReplacements, arrayReplacements)
}
return spec
}