Skip to content

Commit

Permalink
enable preserving order with generatorOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Oct 22, 2018
1 parent a8e3934 commit 9ef96e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 10 additions & 8 deletions pkg/commands/kustfile/kustomizationfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func determineFieldOrder() []string {
"PatchesJson6902",
"ConfigMapGenerator",
"SecretGenerator",
// "GeneratorOptions",
"GeneratorOptions",
"Vars",
"ImageTags",
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
8 changes: 7 additions & 1 deletion pkg/commands/kustfile/kustomizationfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestFieldOrder(t *testing.T) {
"PatchesJson6902",
"ConfigMapGenerator",
"SecretGenerator",
// "GeneratorOptions",
"GeneratorOptions",
"Vars",
"ImageTags",
}
Expand Down Expand Up @@ -219,6 +219,9 @@ BASES:
patchesStrategicMerge:
- service.yaml
- pod.yaml
# generator options
generatorOptions:
disableHash: true
`)

expected := []byte(`
Expand Down Expand Up @@ -254,6 +257,9 @@ bases:
patchesStrategicMerge:
- service.yaml
- pod.yaml
# generator options
generatorOptions:
disableHash: true
`)
fSys := fs.MakeFakeFS()
fSys.WriteTestKustomizationWith(kustomizationContentWithComments)
Expand Down

0 comments on commit 9ef96e9

Please sign in to comment.