Skip to content

Commit

Permalink
fix: disable dynamic treatment of verbatim strings as YAML (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
porridge authored Feb 24, 2025
1 parent b9894e9 commit 13b5b47
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
5 changes: 4 additions & 1 deletion pkg/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,18 @@ func applyPatches(base *object.K8sObject, patches []*types.K8sObjectOverlayPatch
}
for _, p := range patches {
var value interface{}
var tryUnmarshal bool
if p.Verbatim != "" && p.Value == "" {
value = p.Verbatim
tryUnmarshal = false
} else {
var v = &structpb.Value{}
if err := util.UnmarshalWithJSONPB(p.Value, v, false); err != nil {
errs = util.AppendErr(errs, err)
continue
}
value = v.AsInterface()
tryUnmarshal = true
}
if strings.TrimSpace(p.Path) == "" {
scope.V(2).Info("skipping empty path", "value", value)
Expand All @@ -223,7 +226,7 @@ func applyPatches(base *object.K8sObject, patches []*types.K8sObjectOverlayPatch
errs = util.AppendErr(errs, err)
continue
}
err = tpath.WritePathContext(inc, value, false)
err = tpath.WritePathContext(inc, value, false, tryUnmarshal)
if err != nil {
errs = util.AppendErr(errs, err)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ data:
log_timezone = 'Etc/UTC' # comment
more = false
- path: data.yaml_looking_file
verbatim: |
foo: true
bar: baz
`
want := `
apiVersion: v1
Expand All @@ -556,6 +560,9 @@ data:
hba.conf: |-
# PostgreSQL Client Authentication Configuration File
local all all scram-sha-256
yaml_looking_file: |
foo: true
bar: baz
`
rc := &KubernetesResourcesSpec{}
if err := yaml.Unmarshal([]byte(overlays), rc); err != nil {
Expand Down
23 changes: 13 additions & 10 deletions pkg/tpath/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ func GetPathContext(root any, path util.Path, createMissing bool) (*PathContext,
}

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

if !util.IsValueNil(value) {
return setPathContext(nc, value, merge)
return setPathContext(nc, value, merge, tryUnmarshal)
}

//scope.Debug("delete")
Expand Down Expand Up @@ -107,7 +107,7 @@ func WriteNode(root any, path util.Path, value any) error {
if err != nil {
return err
}
return WritePathContext(pc, value, false)
return WritePathContext(pc, value, false, true)
}

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

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

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

// setPathContext writes the given value to the Node in the given PathContext,
// enlarging all PathContext lists to ensure all indexes are valid.
func setPathContext(nc *PathContext, value any, merge bool) error {
processParent, err := setValueContext(nc, value, merge)
func setPathContext(nc *PathContext, value any, merge bool, tryUnmarshal bool) error {
processParent, err := setValueContext(nc, value, merge, tryUnmarshal)
if err != nil || !processParent {
return err
}
Expand All @@ -327,17 +327,20 @@ func setPathContext(nc *PathContext, value any, merge bool) error {
if nc.Parent.Parent == nil {
return nil
}
return setPathContext(nc.Parent, nc.Parent.Node, false) // note: tail recursive
return setPathContext(nc.Parent, nc.Parent.Node, false, tryUnmarshal) // note: tail recursive
}

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

vv, mapFromString := tryToUnmarshalStringToYAML(value)
vv, mapFromString := value, false
if tryUnmarshal {
vv, mapFromString = tryToUnmarshalStringToYAML(value)
}

switch parentNode := nc.Parent.Node.(type) {
case *any:
Expand Down
6 changes: 3 additions & 3 deletions pkg/tpath/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ a: {}
return
}

err := WritePathContext(pc, tt.value, false)
err := WritePathContext(pc, tt.value, false, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -752,7 +752,7 @@ values:
if err != nil {
t.Fatalf("GetPathContext(%q): %v", override.path, err)
}
err = WritePathContext(pc, override.value, false)
err = WritePathContext(pc, override.value, false, true)
if err != nil {
t.Fatalf("WritePathContext(%q): %v", override.path, err)
}
Expand Down Expand Up @@ -828,7 +828,7 @@ values:
return
}

err := WritePathContext(pc, tt.value, false)
err := WritePathContext(pc, tt.value, false, true)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 13b5b47

Please sign in to comment.