Skip to content

Commit 5fed0f7

Browse files
committed
refactor Unmarshal Kustomization struct code
1 parent faddb49 commit 5fed0f7

21 files changed

+37
-116
lines changed

api/internal/localizer/localizer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ suffix: invalid`,
227227
})
228228

229229
err := Run("/a", "", "", fSysTest)
230-
require.EqualError(t, err, `unable to localize target "/a": invalid kustomization: json: unknown field "suffix"`)
230+
require.EqualError(t, err,
231+
`unable to localize target "/a": invalid kustomization: kustomization unmarshal error: error unmarshaling JSON: while decoding JSON: json: unknown field "suffix"`)
231232

232233
checkFSys(t, fSysExpected, fSysTest)
233234
}

api/internal/target/kusttarget_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ commonLabels:
139139

140140
func TestMakeCustomizedResMap(t *testing.T) {
141141
th := kusttest_test.MakeHarness(t)
142-
th.WriteK("/whatever", `
143-
apiVersion: kustomize.config.k8s.io/v1beta1
144-
kind: Kustomization
145-
namePrefix: foo-
142+
th.WriteK("/whatever", `namePrefix: foo-
146143
nameSuffix: -bar
147144
namespace: ns1
148145
commonLabels:
@@ -301,10 +298,7 @@ metadata:
301298

302299
func TestConfigurationsOverrideDefault(t *testing.T) {
303300
th := kusttest_test.MakeHarness(t)
304-
th.WriteK("/merge-config", `
305-
apiVersion: kustomize.config.k8s.io/v1beta1
306-
kind: Kustomization
307-
namePrefix: foo-
301+
th.WriteK("/merge-config", `namePrefix: foo-
308302
nameSuffix: -bar
309303
namespace: ns1
310304
resources:
@@ -401,10 +395,7 @@ metadata:
401395

402396
func TestDuplicateExternalGeneratorsForbidden(t *testing.T) {
403397
th := kusttest_test.MakeHarness(t)
404-
th.WriteK("/generator", `
405-
apiVersion: kustomize.config.k8s.io/v1beta1
406-
kind: Kustomization
407-
generators:
398+
th.WriteK("/generator", `generators:
408399
- |-
409400
apiVersion: generators.example/v1
410401
kind: ManifestGenerator
@@ -437,10 +428,7 @@ generators:
437428

438429
func TestDuplicateExternalTransformersForbidden(t *testing.T) {
439430
th := kusttest_test.MakeHarness(t)
440-
th.WriteK("/transformer", `
441-
apiVersion: kustomize.config.k8s.io/v1beta1
442-
kind: Kustomization
443-
transformers:
431+
th.WriteK("/transformer", `transformers:
444432
- |-
445433
apiVersion: transformers.example.co/v1
446434
kind: ValueAnnotator

api/krusty/basic_io_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ spec:
8585
// test for https://github.com/kubernetes-sigs/kustomize/issues/3812#issuecomment-862339267
8686
func TestBasicIO3812(t *testing.T) {
8787
th := kusttest_test.MakeHarness(t)
88-
th.WriteK(".", `
89-
apiVersion: kustomize.config.k8s.io/v1beta1
90-
kind: Kustomization
91-
resources:
88+
th.WriteK(".", `resources:
9289
- service.yaml
9390
`)
9491

api/krusty/configmaps_test.go

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

9+
"github.com/stretchr/testify/assert"
910
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
1011
)
1112

@@ -138,10 +139,10 @@ configMapGenerator:
138139
- name: json
139140
literals:
140141
- 'v2=[{"path": "var/druid/segment-cache"}]'
141-
- >-
142-
druid_segmentCache_locations=[{"path":
143-
"var/druid/segment-cache",
144-
"maxSize": 32000000000,
142+
- >-
143+
druid_segmentCache_locations=[{"path":
144+
"var/druid/segment-cache",
145+
"maxSize": 32000000000,
145146
"freeSpacePercent": 1.0}]
146147
secretGenerator:
147148
- name: bob
@@ -201,12 +202,12 @@ metadata:
201202
---
202203
apiVersion: v1
203204
data:
204-
druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize":
205-
32000000000, "freeSpacePercent": 1.0}]'
205+
druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize": 32000000000,
206+
"freeSpacePercent": 1.0}]'
206207
v2: '[{"path": "var/druid/segment-cache"}]'
207208
kind: ConfigMap
208209
metadata:
209-
name: blah-json-5298bc8g99
210+
name: blah-json-m8529t979f
210211
---
211212
apiVersion: v1
212213
data:
@@ -228,7 +229,6 @@ type: Opaque
228229
`)
229230
}
230231

231-
// TODO: These should be errors instead.
232232
func TestGeneratorRepeatsInKustomization(t *testing.T) {
233233
th := kusttest_test.MakeHarness(t)
234234
th.WriteK(".", `
@@ -261,24 +261,13 @@ krypton
261261
xenon
262262
radon
263263
`)
264-
m := th.Run(".", th.MakeDefaultOptions())
265-
th.AssertActualEqualsExpected(m, `
266-
apiVersion: v1
267-
data:
268-
fruit: apple
269-
nobles: |2
270-
271-
helium
272-
neon
273-
argon
274-
krypton
275-
xenon
276-
radon
277-
vegetable: broccoli
278-
kind: ConfigMap
279-
metadata:
280-
name: blah-bob-db529cg5bk
281-
`)
264+
err := th.RunWithErr(".", th.MakeDefaultOptions())
265+
if err == nil {
266+
t.Fatalf("expected an error")
267+
}
268+
assert.Contains(t, err.Error(),
269+
"kustomization unmarshal error: 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")
282271
}
283272

284273
func TestIssue3393(t *testing.T) {
@@ -553,8 +542,6 @@ metadata:
553542
func TestDataEndsWithQuotes(t *testing.T) {
554543
th := kusttest_test.MakeHarness(t)
555544
th.WriteK(".", `
556-
apiVersion: kustomize.config.k8s.io/v1beta1
557-
kind: Kustomization
558545
configMapGenerator:
559546
- name: test
560547
literals:

api/krusty/crd_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111

1212
func writeBaseWithCrd(th kusttest_test.Harness) {
1313
th.WriteK("base", `
14-
apiVersion: kustomize.config.k8s.io/v1beta1
15-
kind: Kustomization
1614
crds:
1715
- mycrd.json
1816
@@ -259,8 +257,6 @@ func TestCrdWithOverlay(t *testing.T) {
259257
th := kusttest_test.MakeHarness(t)
260258
writeBaseWithCrd(th)
261259
th.WriteK("overlay", `
262-
apiVersion: kustomize.config.k8s.io/v1beta1
263-
kind: Kustomization
264260
namePrefix: prod-
265261
resources:
266262
- ../base
@@ -307,8 +303,6 @@ spec:
307303
func TestCrdWithContainers(t *testing.T) {
308304
th := kusttest_test.MakeHarness(t)
309305
th.WriteK("crd/containers", `
310-
apiVersion: kustomize.config.k8s.io/v1beta1
311-
kind: Kustomization
312306
resources:
313307
- crd.yaml
314308
images:

api/krusty/disablenamesuffix_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ func findSecret(m resmap.ResMap, prefix string) *resource.Resource {
2626
func TestDisableNameSuffixHash(t *testing.T) {
2727
th := kusttest_test.MakeHarness(t)
2828
const kustomizationContent = `
29-
apiVersion: kustomize.config.k8s.io/v1beta1
30-
kind: Kustomization
3129
namePrefix: foo-
3230
nameSuffix: -bar
3331
namespace: ns1
@@ -101,8 +99,6 @@ metadata:
10199
func TestDisableNameSuffixHashPerObject(t *testing.T) {
102100
th := kusttest_test.MakeHarness(t)
103101
const kustomizationContent = `
104-
apiVersion: kustomize.config.k8s.io/v1beta1
105-
kind: Kustomization
106102
generatorOptions:
107103
disableNameSuffixHash: false
108104
secretGenerator:

api/krusty/generatormergeandreplace_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
func TestSimpleBase(t *testing.T) {
1414
th := kusttest_test.MakeHarness(t)
1515
th.WriteK("app/base", `
16-
apiVersion: kustomize.config.k8s.io/v1beta1
17-
kind: Kustomization
1816
namePrefix: team-foo-
1917
commonLabels:
2018
app: mynginx

api/krusty/generatoroptions_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ type: Opaque
4848
func TestGeneratorOptionsWithBases(t *testing.T) {
4949
th := kusttest_test.MakeHarness(t)
5050
th.WriteK("base", `
51-
apiVersion: kustomize.config.k8s.io/v1beta1
52-
kind: Kustomization
5351
generatorOptions:
5452
disableNameSuffixHash: true
5553
labels:
@@ -60,8 +58,6 @@ configMapGenerator:
6058
- foo=bar
6159
`)
6260
th.WriteK("overlay", `
63-
apiVersion: kustomize.config.k8s.io/v1beta1
64-
kind: Kustomization
6561
resources:
6662
- ../base
6763
generatorOptions:

api/krusty/gvknpatch_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ patches:
8686
- path: patch.yaml
8787
target:
8888
kind: Deployment
89-
options:
89+
options:
9090
allowNameChange: true
9191
`)
9292
options := th.MakeDefaultOptions()
@@ -721,8 +721,6 @@ spec:
721721
name: myvol
722722
`)
723723
th.WriteK(".", `
724-
apiVersion: kustomize.config.k8s.io/v1beta1
725-
kind: Kustomization
726724
namePrefix: foo-
727725
resources:
728726
- resources.yaml
@@ -798,8 +796,6 @@ spec:
798796
name: myvol
799797
`)
800798
th.WriteK(".", `
801-
apiVersion: kustomize.config.k8s.io/v1beta1
802-
kind: Kustomization
803799
resources:
804800
- statefulset.yaml
805801
patches:

api/krusty/inlinepatch_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,6 @@ spec:
234234
func TestPathWithCronJobV1(t *testing.T) {
235235
th := kusttest_test.MakeHarness(t)
236236
th.WriteK(".", `
237-
apiVersion: kustomize.config.k8s.io/v1beta1
238-
kind: Kustomization
239237
resources:
240238
- resources.yaml
241239
patches:

0 commit comments

Comments
 (0)