From c2fbb709dab497e91011da8e72956b670d892d94 Mon Sep 17 00:00:00 2001 From: monopole Date: Sat, 9 Jan 2021 06:57:29 -0800 Subject: [PATCH] Don't swallow error in SM patch and use new RNode Map method. --- .../patchstrategicmerge.go | 1 + api/internal/wrappy/wnode.go | 7 +---- api/internal/wrappy/wnode_test.go | 5 ++++ api/resmap/reswrangler_test.go | 27 ++++++++++--------- api/resource/resource.go | 5 +++- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/api/filters/patchstrategicmerge/patchstrategicmerge.go b/api/filters/patchstrategicmerge/patchstrategicmerge.go index 987ea93f1a..a993e1e4c5 100644 --- a/api/filters/patchstrategicmerge/patchstrategicmerge.go +++ b/api/filters/patchstrategicmerge/patchstrategicmerge.go @@ -15,6 +15,7 @@ type Filter struct { var _ kio.Filter = Filter{} +// Filter does a strategic merge patch, which can delete nodes. func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) { var result []*yaml.RNode for i := range nodes { diff --git a/api/internal/wrappy/wnode.go b/api/internal/wrappy/wnode.go index 26b94117d7..207ff3a3f7 100644 --- a/api/internal/wrappy/wnode.go +++ b/api/internal/wrappy/wnode.go @@ -163,12 +163,7 @@ func (wn *WNode) GetString(path string) (string, error) { // Map implements ifc.Kunstructured. func (wn *WNode) Map() map[string]interface{} { - var result map[string]interface{} - if err := wn.node.YNode().Decode(&result); err != nil { - // Log and die since interface doesn't allow error. - log.Fatalf("failed to decode ynode: %v", err) - } - return result + return wn.node.Map() } // MarshalJSON implements ifc.Kunstructured. diff --git a/api/internal/wrappy/wnode_test.go b/api/internal/wrappy/wnode_test.go index c3c7c29f11..7330a3d356 100644 --- a/api/internal/wrappy/wnode_test.go +++ b/api/internal/wrappy/wnode_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" "sigs.k8s.io/kustomize/api/resid" kyaml "sigs.k8s.io/kustomize/kyaml/yaml" @@ -480,6 +481,10 @@ func TestGetSlice(t *testing.T) { } } +func TestMapEmpty(t *testing.T) { + assert.Equal(t, 0, len(NewWNode().Map())) +} + func TestMap(t *testing.T) { wn := NewWNode() if err := wn.UnmarshalJSON([]byte(deploymentLittleJson)); err != nil { diff --git a/api/resmap/reswrangler_test.go b/api/resmap/reswrangler_test.go index 0eec321d77..bc8a20c962 100644 --- a/api/resmap/reswrangler_test.go +++ b/api/resmap/reswrangler_test.go @@ -1100,18 +1100,21 @@ $patch: delete finalMapSize: 0, }, } - for name, test := range tests { - m, err := rmF.NewResMapFromBytes([]byte(target)) - assert.NoError(t, err, name) - idSet := resource.MakeIdSet(m.Resources()) - assert.Equal(t, 1, idSet.Size(), name) - p, err := rf.FromBytes([]byte(test.patch)) - assert.NoError(t, err, name) - assert.NoError(t, m.ApplySmPatch(idSet, p), name) - assert.Equal(t, test.finalMapSize, m.Size(), name) - yml, err := m.AsYaml() - assert.NoError(t, err, name) - assert.Equal(t, test.expected, string(yml), name) + for name := range tests { + tc := tests[name] + t.Run(name, func(t *testing.T) { + m, err := rmF.NewResMapFromBytes([]byte(target)) + assert.NoError(t, err, name) + idSet := resource.MakeIdSet(m.Resources()) + assert.Equal(t, 1, idSet.Size(), name) + p, err := rf.FromBytes([]byte(tc.patch)) + assert.NoError(t, err, name) + assert.NoError(t, m.ApplySmPatch(idSet, p), name) + assert.Equal(t, tc.finalMapSize, m.Size(), name) + yml, err := m.AsYaml() + assert.NoError(t, err, name) + assert.Equal(t, tc.expected, string(yml), name) + }) } } diff --git a/api/resource/resource.go b/api/resource/resource.go index aa881ae16c..0fff835277 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -423,9 +423,12 @@ func (r *Resource) ApplySmPatch(patch *Resource) error { return err } n, ns := r.GetName(), r.GetNamespace() - r.ApplyFilter(patchstrategicmerge.Filter{ + err = r.ApplyFilter(patchstrategicmerge.Filter{ Patch: node, }) + if err != nil { + return err + } if !r.IsEmpty() { r.SetName(n) r.SetNamespace(ns)