Skip to content

Commit

Permalink
Revert strict decoding of Kustomization due to regression in anchor h…
Browse files Browse the repository at this point in the history
…andling (#5073)

* Revert strict decoding of Kustomization due to regression in anchor handling

* Empty commit
  • Loading branch information
KnVerey committed Mar 13, 2023
1 parent bf6e6ad commit dd520f8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/internal/localizer/localizer_test.go
Expand Up @@ -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)
}
Expand Down
29 changes: 21 additions & 8 deletions api/krusty/configmaps_test.go
Expand Up @@ -6,7 +6,6 @@ package krusty_test
import (
"testing"

"github.com/stretchr/testify/assert"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)

Expand Down Expand Up @@ -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(".", `
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion api/types/kustomization.go
Expand Up @@ -4,6 +4,8 @@
package types

import (
"bytes"
"encoding/json"
"fmt"

"sigs.k8s.io/kustomize/kyaml/errors"
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion api/types/kustomization_test.go
Expand Up @@ -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())
}
Expand Down
Expand Up @@ -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)
}
}

0 comments on commit dd520f8

Please sign in to comment.