/
param_types.go
657 lines (589 loc) · 23.8 KB
/
param_types.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
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 v1beta1
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/substitution"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/strings/slices"
"knative.dev/pkg/apis"
)
// ParamsPrefix is the prefix used in $(...) expressions referring to parameters
const ParamsPrefix = "params"
// ParamSpec defines arbitrary parameters needed beyond typed inputs (such as
// resources). Parameter values are provided by users as inputs on a TaskRun
// or PipelineRun.
type ParamSpec struct {
// Name declares the name by which a parameter is referenced.
Name string `json:"name"`
// Type is the user-specified type of the parameter. The possible types
// are currently "string", "array" and "object", and "string" is the default.
// +optional
Type ParamType `json:"type,omitempty"`
// Description is a user-facing description of the parameter that may be
// used to populate a UI.
// +optional
Description string `json:"description,omitempty"`
// Properties is the JSON Schema properties to support key-value pairs parameter.
// +optional
Properties map[string]PropertySpec `json:"properties,omitempty"`
// Default is the value a parameter takes if no input value is supplied. If
// default is set, a Task may be executed without a supplied value for the
// parameter.
// +optional
Default *ParamValue `json:"default,omitempty"`
// Enum declares a set of allowed param input values for tasks/pipelines that can be validated.
// If Enum is not set, no input validation is performed for the param.
// +optional
Enum []string `json:"enum,omitempty"`
}
// ParamSpecs is a list of ParamSpec
type ParamSpecs []ParamSpec
// PropertySpec defines the struct for object keys
type PropertySpec struct {
Type ParamType `json:"type,omitempty"`
}
// SetDefaults set the default type
func (pp *ParamSpec) SetDefaults(context.Context) {
if pp == nil {
return
}
// Propagate inferred type to the parent ParamSpec's type, and default type to the PropertySpec's type
// The sequence to look at is type in ParamSpec -> properties -> type in default -> array/string/object value in default
// If neither `properties` or `default` section is provided, ParamTypeString will be the default type.
switch {
case pp.Type != "":
// If param type is provided by the author, do nothing but just set default type for PropertySpec in case `properties` section is provided.
pp.setDefaultsForProperties()
case pp.Properties != nil:
pp.Type = ParamTypeObject
// Also set default type for PropertySpec
pp.setDefaultsForProperties()
case pp.Default == nil:
// ParamTypeString is the default value (when no type can be inferred from the default value)
pp.Type = ParamTypeString
case pp.Default.Type != "":
pp.Type = pp.Default.Type
case pp.Default.ArrayVal != nil:
pp.Type = ParamTypeArray
case pp.Default.ObjectVal != nil:
pp.Type = ParamTypeObject
default:
pp.Type = ParamTypeString
}
}
// getNames returns all the names of the declared parameters
func (ps ParamSpecs) getNames() []string {
var names []string
for _, p := range ps {
names = append(names, p.Name)
}
return names
}
// sortByType splits the input params into string params, array params, and object params, in that order
func (ps ParamSpecs) sortByType() (ParamSpecs, ParamSpecs, ParamSpecs) {
var stringParams, arrayParams, objectParams ParamSpecs
for _, p := range ps {
switch p.Type {
case ParamTypeArray:
arrayParams = append(arrayParams, p)
case ParamTypeObject:
objectParams = append(objectParams, p)
case ParamTypeString:
fallthrough
default:
stringParams = append(stringParams, p)
}
}
return stringParams, arrayParams, objectParams
}
// validateNoDuplicateNames returns an error if any of the params have the same name
func (ps ParamSpecs) validateNoDuplicateNames() *apis.FieldError {
var errs *apis.FieldError
names := ps.getNames()
for dup := range findDups(names) {
errs = errs.Also(apis.ErrGeneric("parameter appears more than once", "").ViaFieldKey("params", dup))
}
return errs
}
// validateParamEnum validates feature flag, duplication and allowed types for Param Enum
func (ps ParamSpecs) validateParamEnums(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError
for _, p := range ps {
if len(p.Enum) == 0 {
continue
}
if !config.FromContextOrDefaults(ctx).FeatureFlags.EnableParamEnum {
errs = errs.Also(errs, apis.ErrGeneric(fmt.Sprintf("feature flag `%s` should be set to true to use Enum", config.EnableParamEnum), "").ViaKey(p.Name))
}
if p.Type != ParamTypeString {
errs = errs.Also(apis.ErrGeneric("enum can only be set with string type param", "").ViaKey(p.Name))
}
for dup := range findDups(p.Enum) {
errs = errs.Also(apis.ErrGeneric(fmt.Sprintf("parameter enum value %v appears more than once", dup), "").ViaKey(p.Name))
}
if p.Default != nil && p.Default.StringVal != "" {
if !slices.Contains(p.Enum, p.Default.StringVal) {
errs = errs.Also(apis.ErrGeneric(fmt.Sprintf("param default value %v not in the enum list", p.Default.StringVal), "").ViaKey(p.Name))
}
}
}
return errs
}
// findDups returns the duplicate element in the given slice
func findDups(vals []string) sets.String {
seen := sets.String{}
dups := sets.String{}
for _, val := range vals {
if seen.Has(val) {
dups.Insert(val)
}
seen.Insert(val)
}
return dups
}
// setDefaultsForProperties sets default type for PropertySpec (string) if it's not specified
func (pp *ParamSpec) setDefaultsForProperties() {
for key, propertySpec := range pp.Properties {
if propertySpec.Type == "" {
pp.Properties[key] = PropertySpec{Type: ParamTypeString}
}
}
}
// Param declares an ParamValues to use for the parameter called name.
type Param struct {
Name string `json:"name"`
Value ParamValue `json:"value"`
}
// Params is a list of Param
type Params []Param
// ExtractNames returns a set of unique names
func (ps Params) ExtractNames() sets.String {
names := sets.String{}
for _, p := range ps {
names.Insert(p.Name)
}
return names
}
func (ps Params) extractValues() []string {
pvs := []string{}
for i := range ps {
pvs = append(pvs, ps[i].Value.StringVal)
pvs = append(pvs, ps[i].Value.ArrayVal...)
for _, v := range ps[i].Value.ObjectVal {
pvs = append(pvs, v)
}
}
return pvs
}
// extractParamMapArrVals creates a param map with the key: param.Name and
// val: param.Value.ArrayVal
func (ps Params) extractParamMapArrVals() map[string][]string {
paramsMap := make(map[string][]string)
for _, p := range ps {
paramsMap[p.Name] = p.Value.ArrayVal
}
return paramsMap
}
// ExtractParamArrayLengths extract and return the lengths of all array params
// Example of returned value: {"a-array-params": 2,"b-array-params": 2 }
func (ps Params) ExtractParamArrayLengths() map[string]int {
// Collect all array params
arrayParamsLengths := make(map[string]int)
// Collect array params lengths from params
for _, p := range ps {
if p.Value.Type == ParamTypeArray {
arrayParamsLengths[p.Name] = len(p.Value.ArrayVal)
}
}
return arrayParamsLengths
}
// validateDuplicateParameters checks if a parameter with the same name is defined more than once
func (ps Params) validateDuplicateParameters() (errs *apis.FieldError) {
taskParamNames := sets.NewString()
for i, param := range ps {
if taskParamNames.Has(param.Name) {
errs = errs.Also(apis.ErrGeneric(fmt.Sprintf("parameter names must be unique,"+
" the parameter \"%s\" is also defined at", param.Name), fmt.Sprintf("[%d].name", i)))
}
taskParamNames.Insert(param.Name)
}
return errs
}
// ReplaceVariables applies string, array and object replacements to variables in Params
func (ps Params) ReplaceVariables(stringReplacements map[string]string, arrayReplacements map[string][]string, objectReplacements map[string]map[string]string) Params {
params := ps.DeepCopy()
for i := range params {
params[i].Value.ApplyReplacements(stringReplacements, arrayReplacements, objectReplacements)
}
return params
}
// ExtractDefaultParamArrayLengths extract and return the lengths of all array param defaults
// Example of returned value: {"a-array-params": 2,"b-array-params": 2 }
func (ps ParamSpecs) ExtractDefaultParamArrayLengths() map[string]int {
// Collect all array params
arrayParamsLengths := make(map[string]int)
// Collect array params lengths from defaults
for _, p := range ps {
if p.Default != nil {
if p.Default.Type == ParamTypeArray {
arrayParamsLengths[p.Name] = len(p.Default.ArrayVal)
}
}
}
return arrayParamsLengths
}
// extractArrayIndexingParamRefs takes a string of the form `foo-$(params.array-param[1])-bar` and extracts the portions of the string that reference an element in an array param.
// For example, for the string “foo-$(params.array-param[1])-bar-$(params.other-array-param[2])-$(params.string-param)`,
// it would return ["$(params.array-param[1])", "$(params.other-array-param[2])"].
func extractArrayIndexingParamRefs(paramReference string) []string {
l := []string{}
list := substitution.ExtractArrayIndexingParamsExpressions(paramReference)
for _, val := range list {
indexString := substitution.ExtractIndexString(val)
if indexString != "" {
l = append(l, val)
}
}
return l
}
// extractParamRefsFromSteps get all array indexing references from steps
func extractParamRefsFromSteps(steps []Step) []string {
paramsRefs := []string{}
for _, step := range steps {
paramsRefs = append(paramsRefs, step.Script)
container := step.ToK8sContainer()
paramsRefs = append(paramsRefs, extractParamRefsFromContainer(container)...)
}
return paramsRefs
}
// extractParamRefsFromStepTemplate get all array indexing references from StepsTemplate
func extractParamRefsFromStepTemplate(stepTemplate *StepTemplate) []string {
if stepTemplate == nil {
return nil
}
container := stepTemplate.ToK8sContainer()
return extractParamRefsFromContainer(container)
}
// extractParamRefsFromSidecars get all array indexing references from sidecars
func extractParamRefsFromSidecars(sidecars []Sidecar) []string {
paramsRefs := []string{}
for _, s := range sidecars {
paramsRefs = append(paramsRefs, s.Script)
container := s.ToK8sContainer()
paramsRefs = append(paramsRefs, extractParamRefsFromContainer(container)...)
}
return paramsRefs
}
// extractParamRefsFromVolumes get all array indexing references from volumes
func extractParamRefsFromVolumes(volumes []corev1.Volume) []string {
paramsRefs := []string{}
for i, v := range volumes {
paramsRefs = append(paramsRefs, v.Name)
if v.VolumeSource.ConfigMap != nil {
paramsRefs = append(paramsRefs, v.ConfigMap.Name)
for _, item := range v.ConfigMap.Items {
paramsRefs = append(paramsRefs, item.Key)
paramsRefs = append(paramsRefs, item.Path)
}
}
if v.VolumeSource.Secret != nil {
paramsRefs = append(paramsRefs, v.Secret.SecretName)
for _, item := range v.Secret.Items {
paramsRefs = append(paramsRefs, item.Key)
paramsRefs = append(paramsRefs, item.Path)
}
}
if v.PersistentVolumeClaim != nil {
paramsRefs = append(paramsRefs, v.PersistentVolumeClaim.ClaimName)
}
if v.Projected != nil {
for _, s := range volumes[i].Projected.Sources {
if s.ConfigMap != nil {
paramsRefs = append(paramsRefs, s.ConfigMap.Name)
}
if s.Secret != nil {
paramsRefs = append(paramsRefs, s.Secret.Name)
}
if s.ServiceAccountToken != nil {
paramsRefs = append(paramsRefs, s.ServiceAccountToken.Audience)
}
}
}
if v.CSI != nil {
if v.CSI.NodePublishSecretRef != nil {
paramsRefs = append(paramsRefs, v.CSI.NodePublishSecretRef.Name)
}
if v.CSI.VolumeAttributes != nil {
for _, value := range v.CSI.VolumeAttributes {
paramsRefs = append(paramsRefs, value)
}
}
}
}
return paramsRefs
}
// extractParamRefsFromContainer get all array indexing references from container
func extractParamRefsFromContainer(c *corev1.Container) []string {
paramsRefs := []string{}
paramsRefs = append(paramsRefs, c.Name)
paramsRefs = append(paramsRefs, c.Image)
paramsRefs = append(paramsRefs, string(c.ImagePullPolicy))
paramsRefs = append(paramsRefs, c.Args...)
for ie, e := range c.Env {
paramsRefs = append(paramsRefs, e.Value)
if c.Env[ie].ValueFrom != nil {
if e.ValueFrom.SecretKeyRef != nil {
paramsRefs = append(paramsRefs, e.ValueFrom.SecretKeyRef.LocalObjectReference.Name)
paramsRefs = append(paramsRefs, e.ValueFrom.SecretKeyRef.Key)
}
if e.ValueFrom.ConfigMapKeyRef != nil {
paramsRefs = append(paramsRefs, e.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name)
paramsRefs = append(paramsRefs, e.ValueFrom.ConfigMapKeyRef.Key)
}
}
}
for _, e := range c.EnvFrom {
paramsRefs = append(paramsRefs, e.Prefix)
if e.ConfigMapRef != nil {
paramsRefs = append(paramsRefs, e.ConfigMapRef.LocalObjectReference.Name)
}
if e.SecretRef != nil {
paramsRefs = append(paramsRefs, e.SecretRef.LocalObjectReference.Name)
}
}
paramsRefs = append(paramsRefs, c.WorkingDir)
paramsRefs = append(paramsRefs, c.Command...)
for _, v := range c.VolumeMounts {
paramsRefs = append(paramsRefs, v.Name)
paramsRefs = append(paramsRefs, v.MountPath)
paramsRefs = append(paramsRefs, v.SubPath)
}
return paramsRefs
}
// ParamType indicates the type of an input parameter;
// Used to distinguish between a single string and an array of strings.
type ParamType string
// Valid ParamTypes:
const (
ParamTypeString ParamType = "string"
ParamTypeArray ParamType = "array"
ParamTypeObject ParamType = "object"
)
// AllParamTypes can be used for ParamType validation.
var AllParamTypes = []ParamType{ParamTypeString, ParamTypeArray, ParamTypeObject}
// ParamValues is modeled after IntOrString in kubernetes/apimachinery:
// ParamValue is a type that can hold a single string or string array.
// Used in JSON unmarshalling so that a single JSON field can accept
// either an individual string or an array of strings.
type ParamValue struct {
Type ParamType // Represents the stored type of ParamValues.
StringVal string
// +listType=atomic
ArrayVal []string
ObjectVal map[string]string
}
// ArrayOrString is deprecated, this is to keep backward compatibility
//
// Deprecated: Use ParamValue instead.
type ArrayOrString = ParamValue
// UnmarshalJSON implements the json.Unmarshaller interface.
func (paramValues *ParamValue) UnmarshalJSON(value []byte) error {
// ParamValues is used for Results Value as well, the results can be any kind of
// data so we need to check if it is empty.
if len(value) == 0 {
paramValues.Type = ParamTypeString
return nil
}
if value[0] == '[' {
// We're trying to Unmarshal to []string, but for cases like []int or other types
// of nested array which we don't support yet, we should continue and Unmarshal
// it to String. If the Type being set doesn't match what it actually should be,
// it will be captured by validation in reconciler.
// if failed to unmarshal to array, we will convert the value to string and marshal it to string
var a []string
if err := json.Unmarshal(value, &a); err == nil {
paramValues.Type = ParamTypeArray
paramValues.ArrayVal = a
return nil
}
}
if value[0] == '{' {
// if failed to unmarshal to map, we will convert the value to string and marshal it to string
var m map[string]string
if err := json.Unmarshal(value, &m); err == nil {
paramValues.Type = ParamTypeObject
paramValues.ObjectVal = m
return nil
}
}
// By default we unmarshal to string
paramValues.Type = ParamTypeString
if err := json.Unmarshal(value, ¶mValues.StringVal); err == nil {
return nil
}
paramValues.StringVal = string(value)
return nil
}
// MarshalJSON implements the json.Marshaller interface.
func (paramValues ParamValue) MarshalJSON() ([]byte, error) {
switch paramValues.Type {
case ParamTypeString:
return json.Marshal(paramValues.StringVal)
case ParamTypeArray:
return json.Marshal(paramValues.ArrayVal)
case ParamTypeObject:
return json.Marshal(paramValues.ObjectVal)
default:
return []byte{}, fmt.Errorf("impossible ParamValues.Type: %q", paramValues.Type)
}
}
// ApplyReplacements applyes replacements for ParamValues type
func (paramValues *ParamValue) ApplyReplacements(stringReplacements map[string]string, arrayReplacements map[string][]string, objectReplacements map[string]map[string]string) {
switch paramValues.Type {
case ParamTypeArray:
newArrayVal := []string{}
for _, v := range paramValues.ArrayVal {
newArrayVal = append(newArrayVal, substitution.ApplyArrayReplacements(v, stringReplacements, arrayReplacements)...)
}
paramValues.ArrayVal = newArrayVal
case ParamTypeObject:
newObjectVal := map[string]string{}
for k, v := range paramValues.ObjectVal {
newObjectVal[k] = substitution.ApplyReplacements(v, stringReplacements)
}
paramValues.ObjectVal = newObjectVal
case ParamTypeString:
fallthrough
default:
paramValues.applyOrCorrect(stringReplacements, arrayReplacements, objectReplacements)
}
}
// applyOrCorrect deals with string param whose value can be string literal or a reference to a string/array/object param/result.
// If the value of paramValues is a reference to array or object, the type will be corrected from string to array/object.
func (paramValues *ParamValue) applyOrCorrect(stringReplacements map[string]string, arrayReplacements map[string][]string, objectReplacements map[string]map[string]string) {
stringVal := paramValues.StringVal
// if the stringVal is a string literal or a string that mixed with var references
// just do the normal string replacement
if !exactVariableSubstitutionRegex.MatchString(stringVal) {
paramValues.StringVal = substitution.ApplyReplacements(paramValues.StringVal, stringReplacements)
return
}
// trim the head "$(" and the tail ")" or "[*])"
// i.e. get "params.name" from "$(params.name)" or "$(params.name[*])"
trimedStringVal := substitution.StripStarVarSubExpression(stringVal)
// if the stringVal is a reference to a string param
if _, ok := stringReplacements[trimedStringVal]; ok {
paramValues.StringVal = substitution.ApplyReplacements(paramValues.StringVal, stringReplacements)
}
// if the stringVal is a reference to an array param, we need to change the type other than apply replacement
if _, ok := arrayReplacements[trimedStringVal]; ok {
paramValues.StringVal = ""
paramValues.ArrayVal = substitution.ApplyArrayReplacements(stringVal, stringReplacements, arrayReplacements)
paramValues.Type = ParamTypeArray
}
// if the stringVal is a reference an object param, we need to change the type other than apply replacement
if _, ok := objectReplacements[trimedStringVal]; ok {
paramValues.StringVal = ""
paramValues.ObjectVal = objectReplacements[trimedStringVal]
paramValues.Type = ParamTypeObject
}
}
// NewStructuredValues creates an ParamValues of type ParamTypeString or ParamTypeArray, based on
// how many inputs are given (>1 input will create an array, not string).
func NewStructuredValues(value string, values ...string) *ParamValue {
if len(values) > 0 {
return &ParamValue{
Type: ParamTypeArray,
ArrayVal: append([]string{value}, values...),
}
}
return &ParamValue{
Type: ParamTypeString,
StringVal: value,
}
}
// NewArrayOrString is the deprecated, this is to keep backward compatibility
var NewArrayOrString = NewStructuredValues
// NewObject creates an ParamValues of type ParamTypeObject using the provided key-value pairs
func NewObject(pairs map[string]string) *ParamValue {
return &ParamValue{
Type: ParamTypeObject,
ObjectVal: pairs,
}
}
// ArrayReference returns the name of the parameter from array parameter reference
// returns arrayParam from $(params.arrayParam[*])
func ArrayReference(a string) string {
return strings.TrimSuffix(strings.TrimPrefix(a, "$("+ParamsPrefix+"."), "[*])")
}
// validatePipelineParametersVariablesInTaskParameters validates param value that
// may contain the reference(s) to other params to make sure those references are used appropriately.
func validatePipelineParametersVariablesInTaskParameters(params Params, prefix string, paramNames sets.String, arrayParamNames sets.String, objectParamNameKeys map[string][]string) (errs *apis.FieldError) {
errs = errs.Also(params.validateDuplicateParameters()).ViaField("params")
for _, param := range params {
switch param.Value.Type {
case ParamTypeArray:
for idx, arrayElement := range param.Value.ArrayVal {
errs = errs.Also(validateArrayVariable(arrayElement, prefix, paramNames, arrayParamNames, objectParamNameKeys).ViaFieldIndex("value", idx).ViaFieldKey("params", param.Name))
}
case ParamTypeObject:
for key, val := range param.Value.ObjectVal {
errs = errs.Also(validateStringVariable(val, prefix, paramNames, arrayParamNames, objectParamNameKeys).ViaFieldKey("properties", key).ViaFieldKey("params", param.Name))
}
case ParamTypeString:
fallthrough
default:
errs = errs.Also(validateParamStringValue(param, prefix, paramNames, arrayParamNames, objectParamNameKeys))
}
}
return errs
}
// validateParamStringValue validates the param value field of string type
// that may contain references to other isolated array/object params other than string param.
func validateParamStringValue(param Param, prefix string, paramNames sets.String, arrayVars sets.String, objectParamNameKeys map[string][]string) (errs *apis.FieldError) {
stringValue := param.Value.StringVal
// if the provided param value is an isolated reference to the whole array/object, we just check if the param name exists.
isIsolated, errs := substitution.ValidateWholeArrayOrObjectRefInStringVariable(param.Name, stringValue, prefix, paramNames)
if isIsolated {
return errs
}
// if the provided param value is string literal and/or contains multiple variables
// valid example: "$(params.myString) and another $(params.myObject.key1)"
// invalid example: "$(params.myString) and another $(params.myObject[*])"
return validateStringVariable(stringValue, prefix, paramNames, arrayVars, objectParamNameKeys).ViaFieldKey("params", param.Name)
}
// validateStringVariable validates the normal string fields that can only accept references to string param or individual keys of object param
func validateStringVariable(value, prefix string, stringVars sets.String, arrayVars sets.String, objectParamNameKeys map[string][]string) *apis.FieldError {
errs := substitution.ValidateNoReferencesToUnknownVariables(value, prefix, stringVars)
errs = errs.Also(validateObjectVariable(value, prefix, objectParamNameKeys))
return errs.Also(substitution.ValidateNoReferencesToProhibitedVariables(value, prefix, arrayVars))
}
func validateArrayVariable(value, prefix string, stringVars sets.String, arrayVars sets.String, objectParamNameKeys map[string][]string) *apis.FieldError {
errs := substitution.ValidateNoReferencesToUnknownVariables(value, prefix, stringVars)
errs = errs.Also(validateObjectVariable(value, prefix, objectParamNameKeys))
return errs.Also(substitution.ValidateVariableReferenceIsIsolated(value, prefix, arrayVars))
}
func validateObjectVariable(value, prefix string, objectParamNameKeys map[string][]string) (errs *apis.FieldError) {
objectNames := sets.NewString()
for objectParamName, keys := range objectParamNameKeys {
objectNames.Insert(objectParamName)
errs = errs.Also(substitution.ValidateNoReferencesToUnknownVariables(value, fmt.Sprintf("%s\\.%s", prefix, objectParamName), sets.NewString(keys...)))
}
return errs.Also(substitution.ValidateNoReferencesToEntireProhibitedVariables(value, prefix, objectNames))
}