From 4cfe493262cedf9e788d6ddff0c9ec63650d9b2b Mon Sep 17 00:00:00 2001 From: koba1t Date: Thu, 22 Feb 2024 04:35:54 +0900 Subject: [PATCH] implements to replacements value in the structured data --- api/filters/replacement/replacement_test.go | 40 ++++++++++++++++++++- kyaml/yaml/fns.go | 4 +++ kyaml/yaml/match.go | 9 ++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/api/filters/replacement/replacement_test.go b/api/filters/replacement/replacement_test.go index 7619e5fc9d..931fb2f1d2 100644 --- a/api/filters/replacement/replacement_test.go +++ b/api/filters/replacement/replacement_test.go @@ -1835,6 +1835,44 @@ spec: name: sidecar imagePullPolicy: OnFailure `, + }, + "replacement contain jsonfield": { + input: `apiVersion: v1 +kind: ConfigMap +metadata: + name: target-configmap +data: + config.json: |- + { + "config": { + "id": "42", + "hostname": "REPLACE_TARGET_HOSTNAME" + } + } +`, + replacements: `replacements: +- source: + kind: ConfigMap + name: target-configmap + fieldPath: metadata.name + targets: + - select: + kind: ConfigMap + fieldPaths: + - data.config\.json.config.hostname +`, + expected: `apiVersion: v1 +kind: ConfigMap +metadata: + name: target-configmap +data: + config.json: |- + { + "config": { + "id": "42", + "hostname": "target-configmap" + } + }`, }, "multiple field paths in target": { input: `apiVersion: v1 @@ -2779,7 +2817,7 @@ spec: name: myingress fieldPaths: - spec.tls.0.hosts.0 - - spec.tls.0.secretName + - spec.tls.0.secretName options: create: true `, diff --git a/kyaml/yaml/fns.go b/kyaml/yaml/fns.go index e0802a897f..0bf0c24a8b 100644 --- a/kyaml/yaml/fns.go +++ b/kyaml/yaml/fns.go @@ -830,6 +830,10 @@ func (e *InvalidNodeKindError) Error() string { return msg } +func (e *InvalidNodeKindError) Unwrap() error { + return errors.Errorf("InvalidNodeKindError") +} + func (e *InvalidNodeKindError) ActualNodeKind() Kind { return e.node.YNode().Kind } diff --git a/kyaml/yaml/match.go b/kyaml/yaml/match.go index 8e40d4c2b2..261f1aef9a 100644 --- a/kyaml/yaml/match.go +++ b/kyaml/yaml/match.go @@ -137,10 +137,17 @@ func (p *PathMatcher) visitEveryElem(elem *RNode) error { func (p *PathMatcher) doField(rn *RNode) (*RNode, error) { // lookup the field field, err := rn.Pipe(Get(p.Path[0])) - if err != nil || (!IsCreate(p.Create) && field == nil) { + if err != nil { + if errors.Is(err, &InvalidNodeKindError{}) { + fmt.Println(err) + } return nil, err } + if !IsCreate(p.Create) && field == nil { + return nil, nil + } + if IsCreate(p.Create) && field == nil { var nextPart string if len(p.Path) > 1 {