mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Test field output ordering.
This commit is contained in:
@@ -21,23 +21,37 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/constants"
|
"sigs.k8s.io/kustomize/pkg/constants"
|
||||||
"sigs.k8s.io/kustomize/pkg/fs"
|
"sigs.k8s.io/kustomize/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var fieldMarshallingOrder = determineFieldOrder()
|
||||||
// These field names are the exact kustomization fields
|
|
||||||
kustomizationFields = []string{
|
// determineFieldOrder returns a slice of Kustomization field
|
||||||
"APIVersion",
|
// names in the preferred order for serialization to a file.
|
||||||
"Kind",
|
// The field list is checked against the actual struct type
|
||||||
|
// to confirm that all fields are present, and no unknown
|
||||||
|
// fields are specified. Deprecated fields are removed from
|
||||||
|
// the list, meaning they will drop to the bottom on output
|
||||||
|
// (if present). The ordering and/or deprecation of fields
|
||||||
|
// in nested structs is not determined or considered.
|
||||||
|
func determineFieldOrder() []string {
|
||||||
|
m := make(map[string]bool)
|
||||||
|
s := reflect.ValueOf(&types.Kustomization{}).Elem()
|
||||||
|
typeOfT := s.Type()
|
||||||
|
for i := 0; i < s.NumField(); i++ {
|
||||||
|
m[string(typeOfT.Field(i).Name)] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
ordered := []string{
|
||||||
"Resources",
|
"Resources",
|
||||||
"Bases",
|
"Bases",
|
||||||
"NamePrefix",
|
"NamePrefix",
|
||||||
@@ -50,10 +64,41 @@ var (
|
|||||||
"PatchesJson6902",
|
"PatchesJson6902",
|
||||||
"ConfigMapGenerator",
|
"ConfigMapGenerator",
|
||||||
"SecretGenerator",
|
"SecretGenerator",
|
||||||
|
// "GeneratorOptions",
|
||||||
"Vars",
|
"Vars",
|
||||||
"ImageTags",
|
"ImageTags",
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
// Add deprecated fields here.
|
||||||
|
deprecated := map[string]bool{
|
||||||
|
"Patches": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Account for the inlined TypeMeta fields.
|
||||||
|
var result []string
|
||||||
|
result = append(result, "APIVersion", "Kind")
|
||||||
|
m["TypeMeta"] = true
|
||||||
|
|
||||||
|
// Make sure all these fields are recognized.
|
||||||
|
for _, n := range ordered {
|
||||||
|
if _, ok := m[n]; ok {
|
||||||
|
m[n] = true
|
||||||
|
} else {
|
||||||
|
log.Fatalf("%s is not a recognized field.", n)
|
||||||
|
}
|
||||||
|
// Keep if not deprecated.
|
||||||
|
if _, f := deprecated[n]; !f {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// commentedField records the comment associated with a kustomization field
|
// commentedField records the comment associated with a kustomization field
|
||||||
// field has to be a recognized kustomization field
|
// field has to be a recognized kustomization field
|
||||||
@@ -171,6 +216,7 @@ func (mf *kustomizationFile) parseCommentedFields(content []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// marshal converts a kustomization to a byte stream.
|
||||||
func (mf *kustomizationFile) marshal(kustomization *types.Kustomization) ([]byte, error) {
|
func (mf *kustomizationFile) marshal(kustomization *types.Kustomization) ([]byte, error) {
|
||||||
var output []byte
|
var output []byte
|
||||||
for _, comment := range mf.originalFields {
|
for _, comment := range mf.originalFields {
|
||||||
@@ -181,7 +227,7 @@ func (mf *kustomizationFile) marshal(kustomization *types.Kustomization) ([]byte
|
|||||||
}
|
}
|
||||||
output = append(output, content...)
|
output = append(output, content...)
|
||||||
}
|
}
|
||||||
for _, field := range kustomizationFields {
|
for _, field := range fieldMarshallingOrder {
|
||||||
if mf.hasField(field) {
|
if mf.hasField(field) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -218,7 +264,7 @@ func isCommentOrBlankLine(line []byte) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func findMatchedField(line []byte) (bool, string) {
|
func findMatchedField(line []byte) (bool, string) {
|
||||||
for _, field := range kustomizationFields {
|
for _, field := range fieldMarshallingOrder {
|
||||||
// (?i) is for case insensitive regexp matching
|
// (?i) is for case insensitive regexp matching
|
||||||
r := regexp.MustCompile("^(" + "(?i)" + field + "):")
|
r := regexp.MustCompile("^(" + "(?i)" + field + "):")
|
||||||
if r.Match(line) {
|
if r.Match(line) {
|
||||||
|
|||||||
@@ -25,6 +25,36 @@ import (
|
|||||||
"sigs.k8s.io/kustomize/pkg/types"
|
"sigs.k8s.io/kustomize/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestFieldOrder(t *testing.T) {
|
||||||
|
expected := []string{
|
||||||
|
"APIVersion",
|
||||||
|
"Kind",
|
||||||
|
"Resources",
|
||||||
|
"Bases",
|
||||||
|
"NamePrefix",
|
||||||
|
"Namespace",
|
||||||
|
"Crds",
|
||||||
|
"CommonLabels",
|
||||||
|
"CommonAnnotations",
|
||||||
|
"PatchesStrategicMerge",
|
||||||
|
"PatchesJson6902",
|
||||||
|
"ConfigMapGenerator",
|
||||||
|
"SecretGenerator",
|
||||||
|
// "GeneratorOptions",
|
||||||
|
"Vars",
|
||||||
|
"ImageTags",
|
||||||
|
}
|
||||||
|
actual := determineFieldOrder()
|
||||||
|
if len(expected) != len(actual) {
|
||||||
|
t.Fatalf("Incorrect field count.")
|
||||||
|
}
|
||||||
|
for i, n := range expected {
|
||||||
|
if n != actual[i] {
|
||||||
|
t.Fatalf("Bad field order.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteAndRead(t *testing.T) {
|
func TestWriteAndRead(t *testing.T) {
|
||||||
kustomization := &types.Kustomization{
|
kustomization := &types.Kustomization{
|
||||||
NamePrefix: "prefix",
|
NamePrefix: "prefix",
|
||||||
|
|||||||
Reference in New Issue
Block a user