-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathreplacement.go
More file actions
465 lines (405 loc) · 14.1 KB
/
replacement.go
File metadata and controls
465 lines (405 loc) · 14.1 KB
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
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package replacement
import (
"encoding/json"
"fmt"
"strings"
"sigs.k8s.io/kustomize/api/internal/utils"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/errors"
kyaml_utils "sigs.k8s.io/kustomize/kyaml/utils"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
type Filter struct {
Replacements []types.Replacement `json:"replacements,omitempty" yaml:"replacements,omitempty"`
}
// Filter replaces values of targets with values from sources
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
for i, r := range f.Replacements {
if (r.SourceValue == nil && r.Source == nil) || r.Targets == nil {
return nil, fmt.Errorf("replacements must specify a source and at least one target")
}
value, err := getReplacement(nodes, &f.Replacements[i])
if err != nil {
return nil, err
}
nodes, err = applyReplacement(nodes, value, r.Targets)
if err != nil {
return nil, err
}
}
return nodes, nil
}
func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, error) {
if r.SourceValue != nil && r.Source != nil {
return nil, fmt.Errorf("value and resource selectors are mutually exclusive")
}
if r.SourceValue != nil {
return yaml.NewScalarRNode(*r.SourceValue), nil
}
source, err := selectSourceNode(nodes, r.Source)
if err != nil {
return nil, err
}
if r.Source.FieldPath == "" {
r.Source.FieldPath = types.DefaultReplacementFieldPath
}
fieldPath := kyaml_utils.SmarterPathSplitter(r.Source.FieldPath, ".")
rn, err := source.Pipe(yaml.Lookup(fieldPath...))
if err != nil {
return nil, fmt.Errorf("error looking up replacement source: %w", err)
}
if rn.IsNilOrEmpty() {
return nil, fmt.Errorf("fieldPath `%s` is missing for replacement source %s", r.Source.FieldPath, r.Source.ResId)
}
return getRefinedValue(r.Source.Options, rn)
}
// selectSourceNode finds the node that matches the selector, returning
// an error if multiple or none are found
func selectSourceNode(nodes []*yaml.RNode, selector *types.SourceSelector) (*yaml.RNode, error) {
var matches []*yaml.RNode
for _, n := range nodes {
ids, err := utils.MakeResIds(n)
if err != nil {
return nil, fmt.Errorf("error getting node IDs: %w", err)
}
for _, id := range ids {
if id.IsSelectedBy(selector.ResId) {
if len(matches) > 0 {
return nil, fmt.Errorf(
"multiple matches for selector %s", selector)
}
matches = append(matches, n)
break
}
}
}
if len(matches) == 0 {
return nil, fmt.Errorf("nothing selected by %s", selector)
}
return matches[0], nil
}
func getRefinedValue(options *types.FieldOptions, rn *yaml.RNode) (*yaml.RNode, error) {
if options == nil || options.Delimiter == "" {
return rn, nil
}
if rn.YNode().Kind != yaml.ScalarNode {
return nil, fmt.Errorf("delimiter option can only be used with scalar nodes")
}
value := strings.Split(yaml.GetValue(rn), options.Delimiter)
if options.Index >= len(value) || options.Index < 0 {
return nil, fmt.Errorf("options.index %d is out of bounds for value %s", options.Index, yaml.GetValue(rn))
}
n := rn.Copy()
n.YNode().Value = value[options.Index]
return n, nil
}
func applyReplacement(nodes []*yaml.RNode, value *yaml.RNode, targetSelectors []*types.TargetSelector) ([]*yaml.RNode, error) {
for _, selector := range targetSelectors {
if selector.Select == nil {
return nil, errors.Errorf("target must specify resources to select")
}
if len(selector.FieldPaths) == 0 {
selector.FieldPaths = []string{types.DefaultReplacementFieldPath}
}
tsr, err := types.NewTargetSelectorRegex(selector)
if err != nil {
return nil, fmt.Errorf("error creating target selector: %w", err)
}
for _, possibleTarget := range nodes {
ids, err := utils.MakeResIds(possibleTarget)
if err != nil {
return nil, err
}
// filter targets by label and annotation selectors
selectByAnnoAndLabel, err := selectByAnnoAndLabel(possibleTarget, selector)
if err != nil {
return nil, err
}
if !selectByAnnoAndLabel {
continue
}
if tsr.RejectsAny(ids) {
continue
}
// filter targets by matching resource IDs
for _, id := range ids {
if tsr.Selects(id) {
err := copyValueToTarget(possibleTarget, value, selector)
if err != nil {
return nil, err
}
break
}
}
}
}
return nodes, nil
}
func selectByAnnoAndLabel(n *yaml.RNode, t *types.TargetSelector) (bool, error) {
if matchesSelect, err := matchesAnnoAndLabelSelector(n, t.Select); !matchesSelect || err != nil {
return false, err
}
for _, reject := range t.Reject {
if reject.AnnotationSelector == "" && reject.LabelSelector == "" {
continue
}
if m, err := matchesAnnoAndLabelSelector(n, reject); m || err != nil {
return false, err
}
}
return true, nil
}
func matchesAnnoAndLabelSelector(n *yaml.RNode, selector *types.Selector) (bool, error) {
r := resource.Resource{RNode: *n}
annoMatch, err := r.MatchesAnnotationSelector(selector.AnnotationSelector)
if err != nil {
return false, err
}
labelMatch, err := r.MatchesLabelSelector(selector.LabelSelector)
if err != nil {
return false, err
}
return annoMatch && labelMatch, nil
}
func copyValueToTarget(target *yaml.RNode, value *yaml.RNode, selector *types.TargetSelector) error {
for _, fp := range selector.FieldPaths {
createKind := yaml.Kind(0) // do not create
if selector.Options != nil && selector.Options.Create {
createKind = value.YNode().Kind
}
// Check if this fieldPath contains structured data access
if err := setValueInStructuredData(target, value, fp, createKind); err == nil {
// Successfully handled as structured data
continue
}
// Fall back to normal path handling
targetFieldList, err := target.Pipe(&yaml.PathMatcher{
Path: kyaml_utils.SmarterPathSplitter(fp, "."),
Create: createKind})
if err != nil {
return errors.WrapPrefixf(err, "%s", fieldRetrievalError(fp, createKind != 0))
}
targetFields, err := targetFieldList.Elements()
if err != nil {
return errors.WrapPrefixf(err, "%s", fieldRetrievalError(fp, createKind != 0))
}
if len(targetFields) == 0 {
return errors.Errorf("%s", fieldRetrievalError(fp, createKind != 0))
}
for _, t := range targetFields {
if err := setFieldValue(selector.Options, t, value); err != nil {
return fmt.Errorf("%w", err)
}
}
}
return nil
}
func fieldRetrievalError(fieldPath string, isCreate bool) string {
if isCreate {
return fmt.Sprintf("unable to find or create field %q in replacement target", fieldPath)
}
return fmt.Sprintf("unable to find field %q in replacement target", fieldPath)
}
func setFieldValue(options *types.FieldOptions, targetField *yaml.RNode, value *yaml.RNode) error {
value = value.Copy()
// Handle prefix/suffix matching for sequence nodes (e.g., container args)
// Only apply prefix/suffix matching if the target field is a sequence and
// prefix/suffix options are specified
if targetField.YNode().Kind == yaml.SequenceNode && options != nil && (options.Prefix != "" || options.Suffix != "") {
return setFieldValueInSequence(options, targetField, value)
}
// Handle delimiter option for scalar nodes
if options != nil && options.Delimiter != "" {
if targetField.YNode().Kind != yaml.ScalarNode {
return fmt.Errorf("delimiter option can only be used with scalar nodes")
}
tv := strings.Split(targetField.YNode().Value, options.Delimiter)
v := yaml.GetValue(value)
// TODO: Add a way to remove an element
switch {
case options.Index < 0: // prefix
tv = append([]string{v}, tv...)
case options.Index >= len(tv): // suffix
tv = append(tv, v)
default: // replace an element
tv[options.Index] = v
}
value.YNode().Value = strings.Join(tv, options.Delimiter)
}
// Handle prefix/suffix matching for scalar nodes
if options != nil && (options.Prefix != "" || options.Suffix != "") {
if targetField.YNode().Kind != yaml.ScalarNode {
return fmt.Errorf("prefix/suffix options can only be used with scalar or sequence nodes")
}
targetValue := targetField.YNode().Value
newValue := yaml.GetValue(value)
// Only modify if the target matches both prefix and suffix
if !matchesPrefixAndSuffix(targetValue, options.Prefix, options.Suffix) {
return nil
}
// Preserve prefix and suffix in the result
result := options.Prefix + newValue + options.Suffix
value.YNode().Value = result
}
if targetField.YNode().Kind == yaml.ScalarNode {
// For scalar, only copy the value (leave any type intact to auto-convert int->string or string->int)
targetField.YNode().Value = value.YNode().Value
} else {
targetField.SetYNode(value.YNode())
}
return nil
}
// setFieldValueInSequence handles setting values in sequence nodes (string arrays)
// when prefix and/or suffix options are specified
func setFieldValueInSequence(options *types.FieldOptions, targetField *yaml.RNode, value *yaml.RNode) error {
elements, err := targetField.Elements()
if err != nil {
return err
}
newValue := yaml.GetValue(value)
for _, elem := range elements {
if elem.YNode().Kind != yaml.ScalarNode {
continue
}
elemValue := elem.YNode().Value
if matchesPrefixAndSuffix(elemValue, options.Prefix, options.Suffix) {
// Replace the value portion while preserving prefix and suffix
result := options.Prefix + newValue + options.Suffix
elem.YNode().Value = result
}
}
return nil
}
// matchesPrefixAndSuffix checks if a string has the specified prefix and suffix
func matchesPrefixAndSuffix(s, prefix, suffix string) bool {
if prefix != "" && !strings.HasPrefix(s, prefix) {
return false
}
if suffix != "" && !strings.HasSuffix(s, suffix) {
return false
}
return true
}
// setValueInStructuredData handles setting values within structured data (JSON/YAML) in scalar fields
func setValueInStructuredData(target *yaml.RNode, value *yaml.RNode, fieldPath string, createKind yaml.Kind) error {
pathParts := kyaml_utils.SmarterPathSplitter(fieldPath, ".")
if len(pathParts) < 2 {
return fmt.Errorf("not a structured data path")
}
// Find the potential scalar field that might contain structured data
var scalarFieldPath []string
var structuredDataPath []string
var foundScalar = false
// Try to find where the scalar field ends and structured data begins
for i := 1; i <= len(pathParts); i++ {
potentialScalarPath := pathParts[:i]
scalarField, err := target.Pipe(yaml.Lookup(potentialScalarPath...))
if err != nil {
continue
}
if scalarField != nil && scalarField.YNode().Kind == yaml.ScalarNode && i < len(pathParts) {
// Try to parse the scalar value as structured data
scalarValue := scalarField.YNode().Value
var parsedNode yaml.Node
if err := yaml.Unmarshal([]byte(scalarValue), &parsedNode); err == nil {
// Successfully parsed - this is structured data
scalarFieldPath = potentialScalarPath
structuredDataPath = pathParts[i:]
foundScalar = true
break
}
}
}
if !foundScalar {
return fmt.Errorf("no structured data found in path")
}
// Get the scalar field containing structured data
scalarField, err := target.Pipe(yaml.Lookup(scalarFieldPath...))
if err != nil {
return fmt.Errorf("%w", err)
}
// Parse the structured data
scalarValue := scalarField.YNode().Value
var parsedNode yaml.Node
if err := yaml.Unmarshal([]byte(scalarValue), &parsedNode); err != nil {
return fmt.Errorf("%w", err)
}
structuredData := yaml.NewRNode(&parsedNode)
// Navigate to the target location within the structured data
targetInStructured, err := structuredData.Pipe(&yaml.PathMatcher{
Path: structuredDataPath,
Create: createKind,
})
if err != nil {
return fmt.Errorf("%w", err)
}
targetFields, err := targetInStructured.Elements()
if err != nil {
return fmt.Errorf("%w", err)
}
if len(targetFields) == 0 {
return fmt.Errorf("unable to find field in structured data")
}
// Set the value in the structured data
for _, t := range targetFields {
if t.YNode().Kind == yaml.ScalarNode {
t.YNode().Value = value.YNode().Value
} else {
t.SetYNode(value.YNode())
}
}
// Serialize the modified structured data back to the scalar field
// Try to detect if original was JSON or YAML and preserve formatting
serializedData, err := serializeStructuredData(structuredData, scalarValue)
if err != nil {
return fmt.Errorf("%w", err)
}
// Update the original scalar field
scalarField.YNode().Value = serializedData
return nil
}
// serializeStructuredData handles the serialization of structured data back to string format
// preserving the original format (JSON vs YAML) and style (pretty vs compact)
func serializeStructuredData(structuredData *yaml.RNode, originalValue string) (string, error) {
firstChar := rune(strings.TrimSpace(originalValue)[0])
if firstChar == '{' || firstChar == '[' {
return serializeAsJSON(structuredData, originalValue)
}
// Fallback to YAML format
return serializeAsYAML(structuredData)
}
// serializeAsJSON converts structured data back to JSON format
func serializeAsJSON(structuredData *yaml.RNode, originalValue string) (string, error) {
modifiedData, err := structuredData.String()
if err != nil {
return "", fmt.Errorf("failed to serialize structured data: %w", err)
}
// Parse the YAML output as JSON
var jsonData interface{}
if err := yaml.Unmarshal([]byte(modifiedData), &jsonData); err != nil {
return "", fmt.Errorf("failed to unmarshal YAML data: %w", err)
}
// Check if original was pretty-printed by looking for newlines and indentation
if strings.Contains(originalValue, "\n") && strings.Contains(originalValue, " ") {
// Pretty-print the JSON to match original formatting
if prettyJSON, err := json.MarshalIndent(jsonData, "", " "); err == nil {
return string(prettyJSON), nil
}
}
// Compact JSON
if compactJSON, err := json.Marshal(jsonData); err == nil {
return string(compactJSON), nil
}
return "", fmt.Errorf("failed to marshal JSON data")
}
// serializeAsYAML converts structured data back to YAML format
func serializeAsYAML(structuredData *yaml.RNode) (string, error) {
modifiedData, err := structuredData.String()
if err != nil {
return "", fmt.Errorf("failed to serialize YAML data: %w", err)
}
return strings.TrimSpace(modifiedData), nil
}