refactor Unmarshal Kustomization struct code

This commit is contained in:
yugo kobayashi
2022-12-13 20:36:26 +00:00
parent faddb49c89
commit 5fed0f76c8
21 changed files with 37 additions and 116 deletions

View File

@@ -227,7 +227,8 @@ suffix: invalid`,
}) })
err := Run("/a", "", "", fSysTest) err := Run("/a", "", "", fSysTest)
require.EqualError(t, err, `unable to localize target "/a": invalid kustomization: json: unknown field "suffix"`) require.EqualError(t, err,
`unable to localize target "/a": invalid kustomization: kustomization unmarshal error: error unmarshaling JSON: while decoding JSON: json: unknown field "suffix"`)
checkFSys(t, fSysExpected, fSysTest) checkFSys(t, fSysExpected, fSysTest)
} }

View File

@@ -139,10 +139,7 @@ commonLabels:
func TestMakeCustomizedResMap(t *testing.T) { func TestMakeCustomizedResMap(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("/whatever", ` th.WriteK("/whatever", `namePrefix: foo-
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo-
nameSuffix: -bar nameSuffix: -bar
namespace: ns1 namespace: ns1
commonLabels: commonLabels:
@@ -301,10 +298,7 @@ metadata:
func TestConfigurationsOverrideDefault(t *testing.T) { func TestConfigurationsOverrideDefault(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("/merge-config", ` th.WriteK("/merge-config", `namePrefix: foo-
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo-
nameSuffix: -bar nameSuffix: -bar
namespace: ns1 namespace: ns1
resources: resources:
@@ -401,10 +395,7 @@ metadata:
func TestDuplicateExternalGeneratorsForbidden(t *testing.T) { func TestDuplicateExternalGeneratorsForbidden(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("/generator", ` th.WriteK("/generator", `generators:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
generators:
- |- - |-
apiVersion: generators.example/v1 apiVersion: generators.example/v1
kind: ManifestGenerator kind: ManifestGenerator
@@ -437,10 +428,7 @@ generators:
func TestDuplicateExternalTransformersForbidden(t *testing.T) { func TestDuplicateExternalTransformersForbidden(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("/transformer", ` th.WriteK("/transformer", `transformers:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
transformers:
- |- - |-
apiVersion: transformers.example.co/v1 apiVersion: transformers.example.co/v1
kind: ValueAnnotator kind: ValueAnnotator

View File

@@ -85,10 +85,7 @@ spec:
// test for https://github.com/kubernetes-sigs/kustomize/issues/3812#issuecomment-862339267 // test for https://github.com/kubernetes-sigs/kustomize/issues/3812#issuecomment-862339267
func TestBasicIO3812(t *testing.T) { func TestBasicIO3812(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK(".", ` th.WriteK(".", `resources:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- service.yaml - service.yaml
`) `)

View File

@@ -6,6 +6,7 @@ package krusty_test
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
@@ -138,10 +139,10 @@ configMapGenerator:
- name: json - name: json
literals: literals:
- 'v2=[{"path": "var/druid/segment-cache"}]' - 'v2=[{"path": "var/druid/segment-cache"}]'
- >- - >-
druid_segmentCache_locations=[{"path": druid_segmentCache_locations=[{"path":
"var/druid/segment-cache", "var/druid/segment-cache",
"maxSize": 32000000000, "maxSize": 32000000000,
"freeSpacePercent": 1.0}] "freeSpacePercent": 1.0}]
secretGenerator: secretGenerator:
- name: bob - name: bob
@@ -201,12 +202,12 @@ metadata:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize": druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize": 32000000000,
32000000000, "freeSpacePercent": 1.0}]' "freeSpacePercent": 1.0}]'
v2: '[{"path": "var/druid/segment-cache"}]' v2: '[{"path": "var/druid/segment-cache"}]'
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: blah-json-5298bc8g99 name: blah-json-m8529t979f
--- ---
apiVersion: v1 apiVersion: v1
data: data:
@@ -228,7 +229,6 @@ type: Opaque
`) `)
} }
// TODO: These should be errors instead.
func TestGeneratorRepeatsInKustomization(t *testing.T) { func TestGeneratorRepeatsInKustomization(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK(".", ` th.WriteK(".", `
@@ -261,24 +261,13 @@ krypton
xenon xenon
radon radon
`) `)
m := th.Run(".", th.MakeDefaultOptions()) err := th.RunWithErr(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, ` if err == nil {
apiVersion: v1 t.Fatalf("expected an error")
data: }
fruit: apple assert.Contains(t, err.Error(),
nobles: |2 "kustomization unmarshal error: 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")
helium
neon
argon
krypton
xenon
radon
vegetable: broccoli
kind: ConfigMap
metadata:
name: blah-bob-db529cg5bk
`)
} }
func TestIssue3393(t *testing.T) { func TestIssue3393(t *testing.T) {
@@ -553,8 +542,6 @@ metadata:
func TestDataEndsWithQuotes(t *testing.T) { func TestDataEndsWithQuotes(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator: configMapGenerator:
- name: test - name: test
literals: literals:

View File

@@ -11,8 +11,6 @@ import (
func writeBaseWithCrd(th kusttest_test.Harness) { func writeBaseWithCrd(th kusttest_test.Harness) {
th.WriteK("base", ` th.WriteK("base", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
crds: crds:
- mycrd.json - mycrd.json
@@ -259,8 +257,6 @@ func TestCrdWithOverlay(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
writeBaseWithCrd(th) writeBaseWithCrd(th)
th.WriteK("overlay", ` th.WriteK("overlay", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: prod- namePrefix: prod-
resources: resources:
- ../base - ../base
@@ -307,8 +303,6 @@ spec:
func TestCrdWithContainers(t *testing.T) { func TestCrdWithContainers(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("crd/containers", ` th.WriteK("crd/containers", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- crd.yaml - crd.yaml
images: images:

View File

@@ -26,8 +26,6 @@ func findSecret(m resmap.ResMap, prefix string) *resource.Resource {
func TestDisableNameSuffixHash(t *testing.T) { func TestDisableNameSuffixHash(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
const kustomizationContent = ` const kustomizationContent = `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo- namePrefix: foo-
nameSuffix: -bar nameSuffix: -bar
namespace: ns1 namespace: ns1
@@ -101,8 +99,6 @@ metadata:
func TestDisableNameSuffixHashPerObject(t *testing.T) { func TestDisableNameSuffixHashPerObject(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
const kustomizationContent = ` const kustomizationContent = `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
generatorOptions: generatorOptions:
disableNameSuffixHash: false disableNameSuffixHash: false
secretGenerator: secretGenerator:

View File

@@ -13,8 +13,6 @@ import (
func TestSimpleBase(t *testing.T) { func TestSimpleBase(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("app/base", ` th.WriteK("app/base", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: team-foo- namePrefix: team-foo-
commonLabels: commonLabels:
app: mynginx app: mynginx

View File

@@ -48,8 +48,6 @@ type: Opaque
func TestGeneratorOptionsWithBases(t *testing.T) { func TestGeneratorOptionsWithBases(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK("base", ` th.WriteK("base", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
generatorOptions: generatorOptions:
disableNameSuffixHash: true disableNameSuffixHash: true
labels: labels:
@@ -60,8 +58,6 @@ configMapGenerator:
- foo=bar - foo=bar
`) `)
th.WriteK("overlay", ` th.WriteK("overlay", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- ../base - ../base
generatorOptions: generatorOptions:

View File

@@ -86,7 +86,7 @@ patches:
- path: patch.yaml - path: patch.yaml
target: target:
kind: Deployment kind: Deployment
options: options:
allowNameChange: true allowNameChange: true
`) `)
options := th.MakeDefaultOptions() options := th.MakeDefaultOptions()
@@ -721,8 +721,6 @@ spec:
name: myvol name: myvol
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo- namePrefix: foo-
resources: resources:
- resources.yaml - resources.yaml
@@ -798,8 +796,6 @@ spec:
name: myvol name: myvol
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- statefulset.yaml - statefulset.yaml
patches: patches:

View File

@@ -234,8 +234,6 @@ spec:
func TestPathWithCronJobV1(t *testing.T) { func TestPathWithCronJobV1(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- resources.yaml - resources.yaml
patches: patches:

View File

@@ -28,8 +28,6 @@ spec:
imagePullSecrets: []`) imagePullSecrets: []`)
th.WriteK("/app", ` th.WriteK("/app", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata: metadata:
annotations: annotations:
config.kubernetes.io/local-config: "true" config.kubernetes.io/local-config: "true"
@@ -37,7 +35,7 @@ metadata:
foo: bar foo: bar
name: test_kustomization name: test_kustomization
resources: resources:
- resources.yaml - resources.yaml
`) `)
m := th.Run("/app", th.MakeDefaultOptions()) m := th.Run("/app", th.MakeDefaultOptions())

View File

@@ -14,9 +14,7 @@ import (
// See https://github.com/kubernetes-sigs/kustomize/issues/4124 for details. // See https://github.com/kubernetes-sigs/kustomize/issues/4124 for details.
func TestSKipLocalConfigAfterTransform(t *testing.T) { func TestSKipLocalConfigAfterTransform(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK(".", `apiVersion: kustomize.config.k8s.io/v1beta1 th.WriteK(".", `resources:
kind: Kustomization
resources:
- pod.yaml - pod.yaml
- deployment.yaml - deployment.yaml
transformers: transformers:

View File

@@ -20,6 +20,7 @@ spec:
- port: 7002 - port: 7002
` `
// This test may failed when running on package tests using the go command because `v444.333.222` is set on makefile.
func TestAddManagedbyLabel(t *testing.T) { func TestAddManagedbyLabel(t *testing.T) {
tests := []struct { tests := []struct {
kustFile string kustFile string
@@ -28,8 +29,6 @@ func TestAddManagedbyLabel(t *testing.T) {
}{ }{
{ {
kustFile: ` kustFile: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- service.yaml - service.yaml
`, `,
@@ -38,8 +37,6 @@ resources:
}, },
{ {
kustFile: ` kustFile: `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- service.yaml - service.yaml
buildMetadata: [managedByLabel] buildMetadata: [managedByLabel]

View File

@@ -12,8 +12,6 @@ import (
func TestNamespacedGenerator(t *testing.T) { func TestNamespacedGenerator(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator: configMapGenerator:
- name: the-non-default-namespace-map - name: the-non-default-namespace-map
namespace: non-default namespace: non-default

View File

@@ -217,8 +217,6 @@ fieldSpecs:
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- workloads.yaml - workloads.yaml
transformers: transformers:

View File

@@ -27,8 +27,6 @@ spec:
- port: 7002 - port: 7002
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- service.yaml - service.yaml
buildMetadata: [originAnnotations] buildMetadata: [originAnnotations]
@@ -175,10 +173,10 @@ configMapGenerator:
- name: json - name: json
literals: literals:
- 'v2=[{"path": "var/druid/segment-cache"}]' - 'v2=[{"path": "var/druid/segment-cache"}]'
- >- - >-
druid_segmentCache_locations=[{"path": druid_segmentCache_locations=[{"path":
"var/druid/segment-cache", "var/druid/segment-cache",
"maxSize": 32000000000, "maxSize": 32000000000,
"freeSpacePercent": 1.0}] "freeSpacePercent": 1.0}]
secretGenerator: secretGenerator:
- name: bob - name: bob
@@ -245,8 +243,8 @@ metadata:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize": druid_segmentCache_locations: '[{"path": "var/druid/segment-cache", "maxSize": 32000000000,
32000000000, "freeSpacePercent": 1.0}]' "freeSpacePercent": 1.0}]'
v2: '[{"path": "var/druid/segment-cache"}]' v2: '[{"path": "var/druid/segment-cache"}]'
kind: ConfigMap kind: ConfigMap
metadata: metadata:
@@ -256,7 +254,7 @@ metadata:
configuredBy: configuredBy:
apiVersion: builtin apiVersion: builtin
kind: ConfigMapGenerator kind: ConfigMapGenerator
name: blah-json-5298bc8g99 name: blah-json-m8529t979f
--- ---
apiVersion: v1 apiVersion: v1
data: data:

View File

@@ -84,8 +84,6 @@ spec:
maxUnavailable: 100% maxUnavailable: 100%
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patches: patches:
- path: pdb-patch.yaml - path: pdb-patch.yaml
target: target:

View File

@@ -55,8 +55,6 @@ spec:
- port: 7002 - port: 7002
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- service.yaml - service.yaml
buildMetadata: [transformerAnnotations] buildMetadata: [transformerAnnotations]
@@ -93,8 +91,6 @@ spec:
- port: 7002 - port: 7002
`) `)
th.WriteK(".", ` th.WriteK(".", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- service.yaml - service.yaml
buildMetadata: [originAnnotations, transformerAnnotations] buildMetadata: [originAnnotations, transformerAnnotations]

View File

@@ -4,8 +4,6 @@
package types package types
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/filesys"
@@ -318,17 +316,8 @@ func (k *Kustomization) EnforceFields() []string {
// Unmarshal replace k with the content in YAML input y // Unmarshal replace k with the content in YAML input y
func (k *Kustomization) Unmarshal(y []byte) error { func (k *Kustomization) Unmarshal(y []byte) error {
j, err := yaml.YAMLToJSON(y) if err := yaml.UnmarshalStrict(y, &k); err != nil {
if err != nil { return fmt.Errorf("kustomization unmarshal error: %w", err)
return err
} }
dec := json.NewDecoder(bytes.NewReader(j))
dec.DisallowUnknownFields()
var nk Kustomization
err = dec.Decode(&nk)
if err != nil {
return err
}
*k = nk
return nil return nil
} }

View File

@@ -278,7 +278,7 @@ unknown: foo`)
if err == nil { if err == nil {
t.Fatalf("expect an error") t.Fatalf("expect an error")
} }
expect := "json: unknown field \"unknown\"" expect := "kustomization unmarshal error: error unmarshaling JSON: while decoding JSON: json: unknown field \"unknown\""
if err.Error() != expect { if err.Error() != expect {
t.Fatalf("expect %v but got: %v", expect, err.Error()) t.Fatalf("expect %v but got: %v", expect, err.Error())
} }

View File

@@ -383,7 +383,7 @@ foo:
} }
_, err = mf.Read() _, err = mf.Read()
if err == nil || err.Error() != "json: unknown field \"foo\"" { if err == nil || err.Error() != "kustomization unmarshal error: error unmarshaling JSON: while decoding JSON: json: unknown field \"foo\"" {
t.Fatalf("Expect an unknown field error but got: %v", err) t.Fatalf("Expect an unknown field error but got: %v", err)
} }
} }