From 9ef96e9bb23911f3c8e8ed71dc3af54d93e4f813 Mon Sep 17 00:00:00 2001 From: Jingfang Liu Date: Mon, 22 Oct 2018 10:45:49 -0700 Subject: [PATCH] enable preserving order with generatorOptions --- pkg/commands/kustfile/kustomizationfile.go | 18 ++++++++++-------- .../kustfile/kustomizationfile_test.go | 8 +++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/commands/kustfile/kustomizationfile.go b/pkg/commands/kustfile/kustomizationfile.go index 4ac249160..5f16e7264 100644 --- a/pkg/commands/kustfile/kustomizationfile.go +++ b/pkg/commands/kustfile/kustomizationfile.go @@ -64,7 +64,7 @@ func determineFieldOrder() []string { "PatchesJson6902", "ConfigMapGenerator", "SecretGenerator", - // "GeneratorOptions", + "GeneratorOptions", "Vars", "ImageTags", } @@ -91,12 +91,6 @@ func determineFieldOrder() []string { result = append(result, n) } } - // TODO fix GeneratorOptions. - for k := range m { - if !m[k] && k != "GeneratorOptions" { - log.Fatalf("We're ignoring field '%s'", k) - } - } return result } @@ -281,7 +275,7 @@ func marshalField(field string, kustomization *types.Kustomization) ([]byte, err r := reflect.ValueOf(*kustomization) v := r.FieldByName(strings.Title(field)) - if !v.IsValid() || v.Len() == 0 { + if !v.IsValid() || isEmpty(v) { return []byte{}, nil } @@ -292,3 +286,11 @@ func marshalField(field string, kustomization *types.Kustomization) ([]byte, err return yaml.Marshal(k) } + +func isEmpty(v reflect.Value) bool { + // If v is a pointer type + if v.Type().Kind() == reflect.Ptr { + return v.IsNil() + } + return v.Len() == 0 +} diff --git a/pkg/commands/kustfile/kustomizationfile_test.go b/pkg/commands/kustfile/kustomizationfile_test.go index 201dff566..3b2d40866 100644 --- a/pkg/commands/kustfile/kustomizationfile_test.go +++ b/pkg/commands/kustfile/kustomizationfile_test.go @@ -40,7 +40,7 @@ func TestFieldOrder(t *testing.T) { "PatchesJson6902", "ConfigMapGenerator", "SecretGenerator", - // "GeneratorOptions", + "GeneratorOptions", "Vars", "ImageTags", } @@ -219,6 +219,9 @@ BASES: patchesStrategicMerge: - service.yaml - pod.yaml +# generator options +generatorOptions: + disableHash: true `) expected := []byte(` @@ -254,6 +257,9 @@ bases: patchesStrategicMerge: - service.yaml - pod.yaml +# generator options +generatorOptions: + disableHash: true `) fSys := fs.MakeFakeFS() fSys.WriteTestKustomizationWith(kustomizationContentWithComments)