enable preserving order with generatorOptions

This commit is contained in:
Jingfang Liu
2018-10-22 10:45:49 -07:00
parent a8e393496f
commit 9ef96e9bb2
2 changed files with 17 additions and 9 deletions

View File

@@ -64,7 +64,7 @@ func determineFieldOrder() []string {
"PatchesJson6902", "PatchesJson6902",
"ConfigMapGenerator", "ConfigMapGenerator",
"SecretGenerator", "SecretGenerator",
// "GeneratorOptions", "GeneratorOptions",
"Vars", "Vars",
"ImageTags", "ImageTags",
} }
@@ -91,12 +91,6 @@ func determineFieldOrder() []string {
result = append(result, n) 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 return result
} }
@@ -281,7 +275,7 @@ func marshalField(field string, kustomization *types.Kustomization) ([]byte, err
r := reflect.ValueOf(*kustomization) r := reflect.ValueOf(*kustomization)
v := r.FieldByName(strings.Title(field)) v := r.FieldByName(strings.Title(field))
if !v.IsValid() || v.Len() == 0 { if !v.IsValid() || isEmpty(v) {
return []byte{}, nil return []byte{}, nil
} }
@@ -292,3 +286,11 @@ func marshalField(field string, kustomization *types.Kustomization) ([]byte, err
return yaml.Marshal(k) 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
}

View File

@@ -40,7 +40,7 @@ func TestFieldOrder(t *testing.T) {
"PatchesJson6902", "PatchesJson6902",
"ConfigMapGenerator", "ConfigMapGenerator",
"SecretGenerator", "SecretGenerator",
// "GeneratorOptions", "GeneratorOptions",
"Vars", "Vars",
"ImageTags", "ImageTags",
} }
@@ -219,6 +219,9 @@ BASES:
patchesStrategicMerge: patchesStrategicMerge:
- service.yaml - service.yaml
- pod.yaml - pod.yaml
# generator options
generatorOptions:
disableHash: true
`) `)
expected := []byte(` expected := []byte(`
@@ -254,6 +257,9 @@ bases:
patchesStrategicMerge: patchesStrategicMerge:
- service.yaml - service.yaml
- pod.yaml - pod.yaml
# generator options
generatorOptions:
disableHash: true
`) `)
fSys := fs.MakeFakeFS() fSys := fs.MakeFakeFS()
fSys.WriteTestKustomizationWith(kustomizationContentWithComments) fSys.WriteTestKustomizationWith(kustomizationContentWithComments)