Skip to content

Commit eff7f25

Browse files
committed
implements to replacements value in the structured data
1 parent 53fa728 commit eff7f25

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
`,
@@ -2855,3 +2855,74 @@ spec:
28552855
})
28562856
}
28572857
}
2858+
2859+
func TestValueInlineStructuredData(t *testing.T) {
2860+
testCases := map[string]struct {
2861+
input string
2862+
replacements string
2863+
expected string
2864+
expectedErr string
2865+
}{
2866+
"replacement contain jsonfield": {
2867+
input: `apiVersion: v1
2868+
kind: ConfigMap
2869+
metadata:
2870+
name: target-configmap
2871+
data:
2872+
config.json: |-
2873+
{
2874+
"config": {
2875+
"id": "42",
2876+
"hostname": "REPLACE_TARGET_HOSTNAME"
2877+
}
2878+
}
2879+
`,
2880+
replacements: `replacements:
2881+
- source:
2882+
kind: ConfigMap
2883+
name: target-configmap
2884+
fieldPath: metadata.name
2885+
targets:
2886+
- select:
2887+
kind: ConfigMap
2888+
fieldPaths:
2889+
- data.config\.json.config.hostname
2890+
`,
2891+
expected: `apiVersion: v1
2892+
kind: ConfigMap
2893+
metadata:
2894+
name: target-configmap
2895+
data:
2896+
config.json: |-
2897+
{
2898+
"config": {
2899+
"id": "42",
2900+
"hostname": "target-configmap"
2901+
}
2902+
}`,
2903+
},
2904+
}
2905+
2906+
for tn, tc := range testCases {
2907+
t.Run(tn, func(t *testing.T) {
2908+
f := Filter{}
2909+
err := yaml.Unmarshal([]byte(tc.replacements), &f)
2910+
if !assert.NoError(t, err) {
2911+
t.FailNow()
2912+
}
2913+
actual, err := filtertest.RunFilterE(t, tc.input, f)
2914+
if err != nil {
2915+
if tc.expectedErr == "" {
2916+
t.Errorf("unexpected error: %s\n", err.Error())
2917+
t.FailNow()
2918+
}
2919+
if !assert.Contains(t, err.Error(), tc.expectedErr) {
2920+
t.FailNow()
2921+
}
2922+
}
2923+
if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(actual)) {
2924+
t.FailNow()
2925+
}
2926+
})
2927+
}
2928+
}

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)