Skip to content

Commit 4cfe493

Browse files
committed
implements to replacements value in the structured data
1 parent 671de16 commit 4cfe493

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

api/filters/replacement/replacement_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,44 @@ spec:
18351835
name: sidecar
18361836
imagePullPolicy: OnFailure
18371837
`,
1838+
},
1839+
"replacement contain jsonfield": {
1840+
input: `apiVersion: v1
1841+
kind: ConfigMap
1842+
metadata:
1843+
name: target-configmap
1844+
data:
1845+
config.json: |-
1846+
{
1847+
"config": {
1848+
"id": "42",
1849+
"hostname": "REPLACE_TARGET_HOSTNAME"
1850+
}
1851+
}
1852+
`,
1853+
replacements: `replacements:
1854+
- source:
1855+
kind: ConfigMap
1856+
name: target-configmap
1857+
fieldPath: metadata.name
1858+
targets:
1859+
- select:
1860+
kind: ConfigMap
1861+
fieldPaths:
1862+
- data.config\.json.config.hostname
1863+
`,
1864+
expected: `apiVersion: v1
1865+
kind: ConfigMap
1866+
metadata:
1867+
name: target-configmap
1868+
data:
1869+
config.json: |-
1870+
{
1871+
"config": {
1872+
"id": "42",
1873+
"hostname": "target-configmap"
1874+
}
1875+
}`,
18381876
},
18391877
"multiple field paths in target": {
18401878
input: `apiVersion: v1
@@ -2779,7 +2817,7 @@ spec:
27792817
name: myingress
27802818
fieldPaths:
27812819
- spec.tls.0.hosts.0
2782-
- spec.tls.0.secretName
2820+
- spec.tls.0.secretName
27832821
options:
27842822
create: true
27852823
`,

kyaml/yaml/fns.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,10 @@ func (e *InvalidNodeKindError) Error() string {
830830
return msg
831831
}
832832

833+
func (e *InvalidNodeKindError) Unwrap() error {
834+
return errors.Errorf("InvalidNodeKindError")
835+
}
836+
833837
func (e *InvalidNodeKindError) ActualNodeKind() Kind {
834838
return e.node.YNode().Kind
835839
}

kyaml/yaml/match.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,17 @@ func (p *PathMatcher) visitEveryElem(elem *RNode) error {
137137
func (p *PathMatcher) doField(rn *RNode) (*RNode, error) {
138138
// lookup the field
139139
field, err := rn.Pipe(Get(p.Path[0]))
140-
if err != nil || (!IsCreate(p.Create) && field == nil) {
140+
if err != nil {
141+
if errors.Is(err, &InvalidNodeKindError{}) {
142+
fmt.Println(err)
143+
}
141144
return nil, err
142145
}
143146

147+
if !IsCreate(p.Create) && field == nil {
148+
return nil, nil
149+
}
150+
144151
if IsCreate(p.Create) && field == nil {
145152
var nextPart string
146153
if len(p.Path) > 1 {

0 commit comments

Comments
 (0)