Skip to content

Commit 13b5b47

Browse files
authored
fix: disable dynamic treatment of verbatim strings as YAML (#27)
1 parent b9894e9 commit 13b5b47

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

pkg/patch/patch.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,18 @@ func applyPatches(base *object.K8sObject, patches []*types.K8sObjectOverlayPatch
203203
}
204204
for _, p := range patches {
205205
var value interface{}
206+
var tryUnmarshal bool
206207
if p.Verbatim != "" && p.Value == "" {
207208
value = p.Verbatim
209+
tryUnmarshal = false
208210
} else {
209211
var v = &structpb.Value{}
210212
if err := util.UnmarshalWithJSONPB(p.Value, v, false); err != nil {
211213
errs = util.AppendErr(errs, err)
212214
continue
213215
}
214216
value = v.AsInterface()
217+
tryUnmarshal = true
215218
}
216219
if strings.TrimSpace(p.Path) == "" {
217220
scope.V(2).Info("skipping empty path", "value", value)
@@ -223,7 +226,7 @@ func applyPatches(base *object.K8sObject, patches []*types.K8sObjectOverlayPatch
223226
errs = util.AppendErr(errs, err)
224227
continue
225228
}
226-
err = tpath.WritePathContext(inc, value, false)
229+
err = tpath.WritePathContext(inc, value, false, tryUnmarshal)
227230
if err != nil {
228231
errs = util.AppendErr(errs, err)
229232
}

pkg/patch/patch_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,10 @@ data:
539539
540540
log_timezone = 'Etc/UTC' # comment
541541
more = false
542+
- path: data.yaml_looking_file
543+
verbatim: |
544+
foo: true
545+
bar: baz
542546
`
543547
want := `
544548
apiVersion: v1
@@ -556,6 +560,9 @@ data:
556560
hba.conf: |-
557561
# PostgreSQL Client Authentication Configuration File
558562
local all all scram-sha-256
563+
yaml_looking_file: |
564+
foo: true
565+
bar: baz
559566
`
560567
rc := &KubernetesResourcesSpec{}
561568
if err := yaml.Unmarshal([]byte(overlays), rc); err != nil {

pkg/tpath/tree.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ func GetPathContext(root any, path util.Path, createMissing bool) (*PathContext,
7272
}
7373

7474
// WritePathContext writes the given value to the Node in the given PathContext.
75-
func WritePathContext(nc *PathContext, value any, merge bool) error {
75+
func WritePathContext(nc *PathContext, value any, merge bool, tryUnmarshal bool) error {
7676
//scope.Debugf("WritePathContext PathContext=%s, value=%v", nc, value)
7777

7878
if !util.IsValueNil(value) {
79-
return setPathContext(nc, value, merge)
79+
return setPathContext(nc, value, merge, tryUnmarshal)
8080
}
8181

8282
//scope.Debug("delete")
@@ -107,7 +107,7 @@ func WriteNode(root any, path util.Path, value any) error {
107107
if err != nil {
108108
return err
109109
}
110-
return WritePathContext(pc, value, false)
110+
return WritePathContext(pc, value, false, true)
111111
}
112112

113113
// MergeNode merges value to the tree in root at the given path, creating any required missing internal nodes in path.
@@ -116,7 +116,7 @@ func MergeNode(root any, path util.Path, value any) error {
116116
if err != nil {
117117
return err
118118
}
119-
return WritePathContext(pc, value, true)
119+
return WritePathContext(pc, value, true, true)
120120
}
121121

122122
// Find returns the value at path from the given tree, or false if the path does not exist.
@@ -137,7 +137,7 @@ func Delete(root map[string]any, path util.Path) (bool, error) {
137137
if err != nil {
138138
return false, err
139139
}
140-
return true, WritePathContext(pc, nil, false)
140+
return true, WritePathContext(pc, nil, false, true)
141141
}
142142

143143
// getPathContext is the internal implementation of GetPathContext.
@@ -317,8 +317,8 @@ func getPathContext(nc *PathContext, fullPath, remainPath util.Path, createMissi
317317

318318
// setPathContext writes the given value to the Node in the given PathContext,
319319
// enlarging all PathContext lists to ensure all indexes are valid.
320-
func setPathContext(nc *PathContext, value any, merge bool) error {
321-
processParent, err := setValueContext(nc, value, merge)
320+
func setPathContext(nc *PathContext, value any, merge bool, tryUnmarshal bool) error {
321+
processParent, err := setValueContext(nc, value, merge, tryUnmarshal)
322322
if err != nil || !processParent {
323323
return err
324324
}
@@ -327,17 +327,20 @@ func setPathContext(nc *PathContext, value any, merge bool) error {
327327
if nc.Parent.Parent == nil {
328328
return nil
329329
}
330-
return setPathContext(nc.Parent, nc.Parent.Node, false) // note: tail recursive
330+
return setPathContext(nc.Parent, nc.Parent.Node, false, tryUnmarshal) // note: tail recursive
331331
}
332332

333333
// setValueContext writes the given value to the Node in the given PathContext.
334334
// If setting the value requires growing the final slice, grows it.
335-
func setValueContext(nc *PathContext, value any, merge bool) (bool, error) {
335+
func setValueContext(nc *PathContext, value any, merge bool, tryUnmarshal bool) (bool, error) {
336336
if nc.Parent == nil {
337337
return false, nil
338338
}
339339

340-
vv, mapFromString := tryToUnmarshalStringToYAML(value)
340+
vv, mapFromString := value, false
341+
if tryUnmarshal {
342+
vv, mapFromString = tryToUnmarshalStringToYAML(value)
343+
}
341344

342345
switch parentNode := nc.Parent.Node.(type) {
343346
case *any:

pkg/tpath/tree_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ a: {}
350350
return
351351
}
352352

353-
err := WritePathContext(pc, tt.value, false)
353+
err := WritePathContext(pc, tt.value, false, true)
354354
if err != nil {
355355
t.Fatal(err)
356356
}
@@ -752,7 +752,7 @@ values:
752752
if err != nil {
753753
t.Fatalf("GetPathContext(%q): %v", override.path, err)
754754
}
755-
err = WritePathContext(pc, override.value, false)
755+
err = WritePathContext(pc, override.value, false, true)
756756
if err != nil {
757757
t.Fatalf("WritePathContext(%q): %v", override.path, err)
758758
}
@@ -828,7 +828,7 @@ values:
828828
return
829829
}
830830

831-
err := WritePathContext(pc, tt.value, false)
831+
err := WritePathContext(pc, tt.value, false, true)
832832
if err != nil {
833833
t.Fatal(err)
834834
}

0 commit comments

Comments
 (0)