Skip to content

Commit dd520f8

Browse files
authored
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
1 parent bf6e6ad commit dd520f8

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

api/internal/localizer/localizer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ suffix: invalid`,
263263

264264
_, err := Run("/a", "", "", fSysTest)
265265
require.EqualError(t, err,
266-
`unable to localize target "/a": invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field "suffix"`)
266+
`unable to localize target "/a": invalid Kustomization: json: unknown field "suffix"`)
267267

268268
checkFSys(t, fSysExpected, fSysTest)
269269
}

api/krusty/configmaps_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package krusty_test
66
import (
77
"testing"
88

9-
"github.com/stretchr/testify/assert"
109
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
1110
)
1211

@@ -229,6 +228,9 @@ type: Opaque
229228
`)
230229
}
231230

231+
// TODO: This should be an error instead. However, we can't strict unmarshal until we have a yaml
232+
// lib that support case-insensitive keys and anchors.
233+
// See https://github.com/kubernetes-sigs/kustomize/issues/5061
232234
func TestGeneratorRepeatsInKustomization(t *testing.T) {
233235
th := kusttest_test.MakeHarness(t)
234236
th.WriteK(".", `
@@ -261,13 +263,24 @@ krypton
261263
xenon
262264
radon
263265
`)
264-
err := th.RunWithErr(".", th.MakeDefaultOptions())
265-
if err == nil {
266-
t.Fatalf("expected an error")
267-
}
268-
assert.Contains(t, err.Error(),
269-
"invalid Kustomization: error converting YAML to JSON: yaml: unmarshal errors:\n"+
270-
" line 13: key \"literals\" already set in map\n line 18: key \"files\" already set in map")
266+
m := th.Run(".", th.MakeDefaultOptions())
267+
th.AssertActualEqualsExpected(m, `
268+
apiVersion: v1
269+
data:
270+
fruit: apple
271+
nobles: |2
272+
273+
helium
274+
neon
275+
argon
276+
krypton
277+
xenon
278+
radon
279+
vegetable: broccoli
280+
kind: ConfigMap
281+
metadata:
282+
name: blah-bob-db529cg5bk
283+
`)
271284
}
272285

273286
func TestIssue3393(t *testing.T) {

api/types/kustomization.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package types
55

66
import (
7+
"bytes"
8+
"encoding/json"
79
"fmt"
810

911
"sigs.k8s.io/kustomize/kyaml/errors"
@@ -313,8 +315,20 @@ func (k *Kustomization) EnforceFields() []string {
313315

314316
// Unmarshal replace k with the content in YAML input y
315317
func (k *Kustomization) Unmarshal(y []byte) error {
316-
if err := yaml.UnmarshalStrict(y, &k); err != nil {
318+
// TODO: switch to strict decoding to catch duplicate keys.
319+
// We can't do so until there is a yaml decoder that supports anchors AND case-insensitive keys.
320+
// See https://github.com/kubernetes-sigs/kustomize/issues/5061
321+
j, err := yaml.YAMLToJSON(y)
322+
if err != nil {
317323
return errors.WrapPrefixf(err, "invalid Kustomization")
318324
}
325+
dec := json.NewDecoder(bytes.NewReader(j))
326+
dec.DisallowUnknownFields()
327+
var nk Kustomization
328+
err = dec.Decode(&nk)
329+
if err != nil {
330+
return errors.WrapPrefixf(err, "invalid Kustomization")
331+
}
332+
*k = nk
319333
return nil
320334
}

api/types/kustomization_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ unknown: foo`)
278278
if err == nil {
279279
t.Fatalf("expect an error")
280280
}
281-
expect := "invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field \"unknown\""
281+
expect := "invalid Kustomization: json: unknown field \"unknown\""
282282
if err.Error() != expect {
283283
t.Fatalf("expect %v but got: %v", expect, err.Error())
284284
}

kustomize/commands/internal/kustfile/kustomizationfile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ foo:
382382
}
383383

384384
_, err = mf.Read()
385-
if err == nil || err.Error() != "invalid Kustomization: error unmarshaling JSON: while decoding JSON: json: unknown field \"foo\"" {
385+
if err == nil || err.Error() != "invalid Kustomization: json: unknown field \"foo\"" {
386386
t.Fatalf("Expect an unknown field error but got: %v", err)
387387
}
388388
}

0 commit comments

Comments
 (0)