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
This commit is contained in:
Katrina Verey
2023-03-13 16:46:47 -04:00
committed by GitHub
parent bf6e6ad33b
commit dd520f8889
5 changed files with 39 additions and 12 deletions

View File

@@ -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)
}

View File

@@ -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) {

View File

@@ -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
}

View File

@@ -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())
}

View File

@@ -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)
}
}