From 05d017ebba73e39939ebbaf58d7f88d4f5b38483 Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Mon, 24 Feb 2025 10:52:57 +0100 Subject: [PATCH 1/2] test case --- pkg/patch/patch_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/patch/patch_test.go b/pkg/patch/patch_test.go index 4dcf349..8ec3297 100644 --- a/pkg/patch/patch_test.go +++ b/pkg/patch/patch_test.go @@ -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 @@ -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 { From d37858c3c384945d3707ddbcbf1c6beaf7c9c6ac Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Mon, 24 Feb 2025 10:55:34 +0100 Subject: [PATCH 2/2] fix --- pkg/patch/patch.go | 5 ++++- pkg/tpath/tree.go | 23 +++++++++++++---------- pkg/tpath/tree_test.go | 6 +++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/patch/patch.go b/pkg/patch/patch.go index 6bbf020..5e55e0a 100644 --- a/pkg/patch/patch.go +++ b/pkg/patch/patch.go @@ -203,8 +203,10 @@ 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 { @@ -212,6 +214,7 @@ func applyPatches(base *object.K8sObject, patches []*types.K8sObjectOverlayPatch continue } value = v.AsInterface() + tryUnmarshal = true } if strings.TrimSpace(p.Path) == "" { scope.V(2).Info("skipping empty path", "value", value) @@ -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) } diff --git a/pkg/tpath/tree.go b/pkg/tpath/tree.go index e4cbc29..d8387b1 100644 --- a/pkg/tpath/tree.go +++ b/pkg/tpath/tree.go @@ -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") @@ -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. @@ -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. @@ -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. @@ -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 } @@ -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: diff --git a/pkg/tpath/tree_test.go b/pkg/tpath/tree_test.go index 35cbde6..29f83b7 100644 --- a/pkg/tpath/tree_test.go +++ b/pkg/tpath/tree_test.go @@ -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) } @@ -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) } @@ -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) }