Skip to content

Commit 2dc0d0d

Browse files
committed
implements to replacements value in the structured data
1 parent 3908634 commit 2dc0d0d

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

api/filters/replacement/replacement_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2779,7 +2779,7 @@ spec:
27792779
name: myingress
27802780
fieldPaths:
27812781
- spec.tls.0.hosts.0
2782-
- spec.tls.0.secretName
2782+
- spec.tls.0.secretName
27832783
options:
27842784
create: true
27852785
`,
@@ -4498,3 +4498,74 @@ metadata:
44984498
})
44994499
}
45004500
}
4501+
4502+
func TestValueInlineStructuredData(t *testing.T) {
4503+
testCases := map[string]struct {
4504+
input string
4505+
replacements string
4506+
expected string
4507+
expectedErr string
4508+
}{
4509+
"replacement contain jsonfield": {
4510+
input: `apiVersion: v1
4511+
kind: ConfigMap
4512+
metadata:
4513+
name: target-configmap
4514+
data:
4515+
config.json: |-
4516+
{
4517+
"config": {
4518+
"id": "42",
4519+
"hostname": "REPLACE_TARGET_HOSTNAME"
4520+
}
4521+
}
4522+
`,
4523+
replacements: `replacements:
4524+
- source:
4525+
kind: ConfigMap
4526+
name: target-configmap
4527+
fieldPath: metadata.name
4528+
targets:
4529+
- select:
4530+
kind: ConfigMap
4531+
fieldPaths:
4532+
- data.config\.json.config.hostname
4533+
`,
4534+
expected: `apiVersion: v1
4535+
kind: ConfigMap
4536+
metadata:
4537+
name: target-configmap
4538+
data:
4539+
config.json: |-
4540+
{
4541+
"config": {
4542+
"id": "42",
4543+
"hostname": "target-configmap"
4544+
}
4545+
}`,
4546+
},
4547+
}
4548+
4549+
for tn, tc := range testCases {
4550+
t.Run(tn, func(t *testing.T) {
4551+
f := Filter{}
4552+
err := yaml.Unmarshal([]byte(tc.replacements), &f)
4553+
if !assert.NoError(t, err) {
4554+
t.FailNow()
4555+
}
4556+
actual, err := filtertest.RunFilterE(t, tc.input, f)
4557+
if err != nil {
4558+
if tc.expectedErr == "" {
4559+
t.Errorf("unexpected error: %s\n", err.Error())
4560+
t.FailNow()
4561+
}
4562+
if !assert.Contains(t, err.Error(), tc.expectedErr) {
4563+
t.FailNow()
4564+
}
4565+
}
4566+
if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) {
4567+
t.FailNow()
4568+
}
4569+
})
4570+
}
4571+
}

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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,20 @@ 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+
// check error is an invalid kind error
142+
invalidKindErr := &InvalidNodeKindError{}
143+
if errors.As(err, &invalidKindErr) {
144+
// if the field is valid json or yaml, continue to lookup the next part of the path
145+
fmt.Print("-----------------------------\nOUTPUT: ", err)
146+
}
141147
return nil, err
142148
}
143149

150+
if !IsCreate(p.Create) && field == nil {
151+
return nil, nil
152+
}
153+
144154
if IsCreate(p.Create) && field == nil {
145155
var nextPart string
146156
if len(p.Path) > 1 {

0 commit comments

Comments
 (0)