From dd520f8889a52ddd592bb4773fe512b990fea698 Mon Sep 17 00:00:00 2001 From: Katrina Verey Date: Mon, 13 Mar 2023 16:46:47 -0400 Subject: [PATCH] Revert strict decoding of Kustomization due to regression in anchor handling (#5073) * Revert strict decoding of Kustomization due to regression in anchor handling * Empty commit --- api/internal/localizer/localizer_test.go | 2 +- api/krusty/configmaps_test.go | 29 ++++++++++++++----- api/types/kustomization.go | 16 +++++++++- api/types/kustomization_test.go | 2 +- .../kustfile/kustomizationfile_test.go | 2 +- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/api/internal/localizer/localizer_test.go b/api/internal/localizer/localizer_test.go index 9a7135d20e..c0645addb0 100644 --- a/api/internal/localizer/localizer_test.go +++ b/api/internal/localizer/localizer_test.go @@ -263,7 +263,7 @@ suffix: invalid`, _, err := Run("/a", "", "", fSysTest) require.EqualError(t, err, - `unable to localize target "/a": invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field "suffix"`) + `unable to localize target "/a": invalid Kustomization: json: unknown field "suffix"`) checkFSys(t, fSysExpected, fSysTest) } diff --git a/api/krusty/configmaps_test.go b/api/krusty/configmaps_test.go index 251d6d8251..5c949668cb 100644 --- a/api/krusty/configmaps_test.go +++ b/api/krusty/configmaps_test.go @@ -6,7 +6,6 @@ package krusty_test import ( "testing" - "github.com/stretchr/testify/assert" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) @@ -229,6 +228,9 @@ type: Opaque `) } +// TODO: This should be an error instead. However, we can't strict unmarshal until we have a yaml +// lib that support case-insensitive keys and anchors. +// See https://github.com/kubernetes-sigs/kustomize/issues/5061 func TestGeneratorRepeatsInKustomization(t *testing.T) { th := kusttest_test.MakeHarness(t) th.WriteK(".", ` @@ -261,13 +263,24 @@ krypton xenon radon `) - err := th.RunWithErr(".", th.MakeDefaultOptions()) - if err == nil { - t.Fatalf("expected an error") - } - assert.Contains(t, err.Error(), - "invalid Kustomization: error converting YAML to JSON: yaml: unmarshal errors:\n"+ - " line 13: key \"literals\" already set in map\n line 18: key \"files\" already set in map") + m := th.Run(".", th.MakeDefaultOptions()) + th.AssertActualEqualsExpected(m, ` +apiVersion: v1 +data: + fruit: apple + nobles: |2 + + helium + neon + argon + krypton + xenon + radon + vegetable: broccoli +kind: ConfigMap +metadata: + name: blah-bob-db529cg5bk +`) } func TestIssue3393(t *testing.T) { diff --git a/api/types/kustomization.go b/api/types/kustomization.go index e0dcdcf218..add70dc011 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -4,6 +4,8 @@ package types import ( + "bytes" + "encoding/json" "fmt" "sigs.k8s.io/kustomize/kyaml/errors" @@ -313,8 +315,20 @@ func (k *Kustomization) EnforceFields() []string { // Unmarshal replace k with the content in YAML input y func (k *Kustomization) Unmarshal(y []byte) error { - if err := yaml.UnmarshalStrict(y, &k); err != nil { + // TODO: switch to strict decoding to catch duplicate keys. + // We can't do so until there is a yaml decoder that supports anchors AND case-insensitive keys. + // See https://github.com/kubernetes-sigs/kustomize/issues/5061 + j, err := yaml.YAMLToJSON(y) + if err != nil { return errors.WrapPrefixf(err, "invalid Kustomization") } + dec := json.NewDecoder(bytes.NewReader(j)) + dec.DisallowUnknownFields() + var nk Kustomization + err = dec.Decode(&nk) + if err != nil { + return errors.WrapPrefixf(err, "invalid Kustomization") + } + *k = nk return nil } diff --git a/api/types/kustomization_test.go b/api/types/kustomization_test.go index ff6e9a6857..b5529ef8bc 100644 --- a/api/types/kustomization_test.go +++ b/api/types/kustomization_test.go @@ -278,7 +278,7 @@ unknown: foo`) if err == nil { t.Fatalf("expect an error") } - expect := "invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field \"unknown\"" + expect := "invalid Kustomization: json: unknown field \"unknown\"" if err.Error() != expect { t.Fatalf("expect %v but got: %v", expect, err.Error()) } diff --git a/kustomize/commands/internal/kustfile/kustomizationfile_test.go b/kustomize/commands/internal/kustfile/kustomizationfile_test.go index 3b0ff6db8d..87d2f0fc67 100644 --- a/kustomize/commands/internal/kustfile/kustomizationfile_test.go +++ b/kustomize/commands/internal/kustfile/kustomizationfile_test.go @@ -382,7 +382,7 @@ foo: } _, err = mf.Read() - if err == nil || err.Error() != "invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field \"foo\"" { + if err == nil || err.Error() != "invalid Kustomization: json: unknown field \"foo\"" { t.Fatalf("Expect an unknown field error but got: %v", err) } }