mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #440 from monopole/fix428
Maintain fields of TransformerConfig in sorted order.
This commit is contained in:
@@ -52,14 +52,11 @@ func registerCRD(loader ifc.Loader, path string) (*transformerconfig.Transformer
|
|||||||
var types map[string]common.OpenAPIDefinition
|
var types map[string]common.OpenAPIDefinition
|
||||||
if content[0] == '{' {
|
if content[0] == '{' {
|
||||||
err = json.Unmarshal(content, &types)
|
err = json.Unmarshal(content, &types)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
err = yaml.Unmarshal(content, &types)
|
err = yaml.Unmarshal(content, &types)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
crds := getCRDs(types)
|
crds := getCRDs(types)
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package crds
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/pkg/gvk"
|
"sigs.k8s.io/kustomize/pkg/gvk"
|
||||||
@@ -178,17 +177,7 @@ func TestRegisterCRD(t *testing.T) {
|
|||||||
NameReference: refpathconfigs,
|
NameReference: refpathconfigs,
|
||||||
}
|
}
|
||||||
|
|
||||||
ldr := makeLoader(t)
|
pathconfig, _ := registerCRD(makeLoader(t), "/testpath/crd.json")
|
||||||
|
|
||||||
pathconfig, _ := registerCRD(ldr, "/testpath/crd.json")
|
|
||||||
|
|
||||||
sort.Slice(expected.NameReference[:], func(i, j int) bool {
|
|
||||||
return expected.NameReference[i].Gvk.String() < expected.NameReference[j].Gvk.String()
|
|
||||||
})
|
|
||||||
|
|
||||||
sort.Slice(pathconfig.NameReference[:], func(i, j int) bool {
|
|
||||||
return pathconfig.NameReference[i].Gvk.String() < pathconfig.NameReference[j].Gvk.String()
|
|
||||||
})
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(pathconfig, expected) {
|
if !reflect.DeepEqual(pathconfig, expected) {
|
||||||
t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig)
|
t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig)
|
||||||
|
|||||||
@@ -20,20 +20,47 @@ package transformerconfig
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
"sigs.k8s.io/kustomize/pkg/ifc"
|
"sigs.k8s.io/kustomize/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/pkg/transformerconfig/defaultconfig"
|
"sigs.k8s.io/kustomize/pkg/transformerconfig/defaultconfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type rpcSlice []ReferencePathConfig
|
||||||
|
|
||||||
|
func (s rpcSlice) Len() int { return len(s) }
|
||||||
|
func (s rpcSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
func (s rpcSlice) Less(i, j int) bool {
|
||||||
|
return s[i].Gvk.IsLessThan(s[j].Gvk)
|
||||||
|
}
|
||||||
|
|
||||||
|
type pcSlice []PathConfig
|
||||||
|
|
||||||
|
func (s pcSlice) Len() int { return len(s) }
|
||||||
|
func (s pcSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
|
func (s pcSlice) Less(i, j int) bool {
|
||||||
|
return s[i].Gvk.IsLessThan(s[j].Gvk)
|
||||||
|
}
|
||||||
|
|
||||||
// TransformerConfig represents the path configurations for different transformations
|
// TransformerConfig represents the path configurations for different transformations
|
||||||
type TransformerConfig struct {
|
type TransformerConfig struct {
|
||||||
NamePrefix []PathConfig `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
|
NamePrefix pcSlice `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
|
||||||
NameSpace []PathConfig `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
NameSpace pcSlice `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
||||||
CommonLabels []PathConfig `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
|
CommonLabels pcSlice `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
|
||||||
CommonAnnotations []PathConfig `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
|
CommonAnnotations pcSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
|
||||||
NameReference []ReferencePathConfig `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
|
NameReference rpcSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
|
||||||
VarReference []PathConfig `json:"varReference,omitempty" yaml:"varReference,omitempty"`
|
VarReference pcSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// sortFields provides determinism in logging, tests, etc.
|
||||||
|
func (t *TransformerConfig) sortFields() {
|
||||||
|
sort.Sort(t.NamePrefix)
|
||||||
|
sort.Sort(t.NameSpace)
|
||||||
|
sort.Sort(t.CommonLabels)
|
||||||
|
sort.Sort(t.CommonAnnotations)
|
||||||
|
sort.Sort(t.NameReference)
|
||||||
|
sort.Sort(t.VarReference)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPrefixPathConfig adds a PathConfig to NamePrefix
|
// AddPrefixPathConfig adds a PathConfig to NamePrefix
|
||||||
@@ -65,6 +92,7 @@ func (t *TransformerConfig) Merge(input *TransformerConfig) *TransformerConfig {
|
|||||||
merged.CommonLabels = append(t.CommonLabels, input.CommonLabels...)
|
merged.CommonLabels = append(t.CommonLabels, input.CommonLabels...)
|
||||||
merged.VarReference = append(t.VarReference, input.VarReference...)
|
merged.VarReference = append(t.VarReference, input.VarReference...)
|
||||||
merged.NameReference = mergeNameReferencePathConfigs(t.NameReference, input.NameReference)
|
merged.NameReference = mergeNameReferencePathConfigs(t.NameReference, input.NameReference)
|
||||||
|
merged.sortFields()
|
||||||
return merged
|
return merged
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,6 +120,7 @@ func MakeTransformerConfigFromBytes(data []byte) (*TransformerConfig, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
t.sortFields()
|
||||||
return &t, nil
|
return &t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user