diff --git a/api/internal/builtins/PatchTransformer.go b/api/internal/builtins/PatchTransformer.go index 8e6eb41128..05d96f23ca 100644 --- a/api/internal/builtins/PatchTransformer.go +++ b/api/internal/builtins/PatchTransformer.go @@ -56,8 +56,9 @@ func (p *PatchTransformerPlugin) Config(h *resmap.PluginHelpers, c []byte) error patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText)) patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText)) - if (errSM == nil && errJson == nil) || - (patchesSM != nil && patchesJson != nil) { + if ((errSM == nil && errJson == nil) || + (patchesSM != nil && patchesJson != nil)) && + (len(patchesSM) > 0 && len(patchesJson) > 0) { return fmt.Errorf( "illegally qualifies as both an SM and JSON patch: %s", p.patchSource) diff --git a/plugin/builtin/patchtransformer/PatchTransformer.go b/plugin/builtin/patchtransformer/PatchTransformer.go index 04f15b7ee0..062e57930a 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer.go +++ b/plugin/builtin/patchtransformer/PatchTransformer.go @@ -59,8 +59,9 @@ func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error { patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText)) patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText)) - if (errSM == nil && errJson == nil) || - (patchesSM != nil && patchesJson != nil) { + if ((errSM == nil && errJson == nil) || + (patchesSM != nil && patchesJson != nil)) && + (len(patchesSM) > 0 && len(patchesJson) > 0) { return fmt.Errorf( "illegally qualifies as both an SM and JSON patch: %s", p.patchSource) diff --git a/plugin/builtin/patchtransformer/PatchTransformer_test.go b/plugin/builtin/patchtransformer/PatchTransformer_test.go index 805e5f99e4..5535b6690a 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer_test.go +++ b/plugin/builtin/patchtransformer/PatchTransformer_test.go @@ -1108,3 +1108,54 @@ spec: name: test-deployment `) } + +func TestPatchTransformerPatchEmpty(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("PatchTransformer") + defer th.Reset() + + th.WriteF("patch.yaml", ` + `) + + th.RunTransformerAndCheckError(` +apiVersion: builtin +kind: PatchTransformer +metadata: + name: notImportantHere +path: patch.yaml +target: + name: myDeploy +`, someDeploymentResources, func(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Fatalf("unexpected error") + } + }) +} + +func TestPatchTransformerPatchEmptyOnlyComments(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("PatchTransformer") + defer th.Reset() + + th.WriteF("patch.yaml", ` +# File with only comments + +# Is a virtually empty yaml +`) + + th.RunTransformerAndCheckError(` +apiVersion: builtin +kind: PatchTransformer +metadata: + name: notImportantHere +path: patch.yaml +target: + name: myDeploy +`, someDeploymentResources, func(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Fatalf("unexpected error") + } + }) +}