Skip to content

Commit df5c3ab

Browse files
committed
Maintain fields of TransformerConfig in sorted order.
Not needed for execution, just makes logging and tests deterministic. Related to #428
1 parent 5b95db2 commit df5c3ab

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

pkg/crds/crds.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,11 @@ func registerCRD(loader ifc.Loader, path string) (*transformerconfig.Transformer
5252
var types map[string]common.OpenAPIDefinition
5353
if content[0] == '{' {
5454
err = json.Unmarshal(content, &types)
55-
if err != nil {
56-
return nil, err
57-
}
5855
} else {
5956
err = yaml.Unmarshal(content, &types)
60-
if err != nil {
61-
return nil, err
62-
}
57+
}
58+
if err != nil {
59+
return nil, err
6360
}
6461

6562
crds := getCRDs(types)

pkg/crds/crds_test.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package crds
1818

1919
import (
2020
"reflect"
21-
"sort"
2221
"testing"
2322

2423
"sigs.k8s.io/kustomize/pkg/gvk"
@@ -178,17 +177,7 @@ func TestRegisterCRD(t *testing.T) {
178177
NameReference: refpathconfigs,
179178
}
180179

181-
ldr := makeLoader(t)
182-
183-
pathconfig, _ := registerCRD(ldr, "/testpath/crd.json")
184-
185-
sort.Slice(expected.NameReference[:], func(i, j int) bool {
186-
return expected.NameReference[i].Gvk.String() < expected.NameReference[j].Gvk.String()
187-
})
188-
189-
sort.Slice(pathconfig.NameReference[:], func(i, j int) bool {
190-
return pathconfig.NameReference[i].Gvk.String() < pathconfig.NameReference[j].Gvk.String()
191-
})
180+
pathconfig, _ := registerCRD(makeLoader(t), "/testpath/crd.json")
192181

193182
if !reflect.DeepEqual(pathconfig, expected) {
194183
t.Fatalf("expected\n %v\n but got\n %v\n", expected, pathconfig)

pkg/transformerconfig/transformerconfig.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,47 @@ package transformerconfig
2020

2121
import (
2222
"log"
23+
"sort"
2324

2425
"github.com/ghodss/yaml"
2526
"sigs.k8s.io/kustomize/pkg/ifc"
2627
"sigs.k8s.io/kustomize/pkg/transformerconfig/defaultconfig"
2728
)
2829

30+
type rpcSlice []ReferencePathConfig
31+
32+
func (s rpcSlice) Len() int { return len(s) }
33+
func (s rpcSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
34+
func (s rpcSlice) Less(i, j int) bool {
35+
return s[i].Gvk.IsLessThan(s[j].Gvk)
36+
}
37+
38+
type pcSlice []PathConfig
39+
40+
func (s pcSlice) Len() int { return len(s) }
41+
func (s pcSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
42+
func (s pcSlice) Less(i, j int) bool {
43+
return s[i].Gvk.IsLessThan(s[j].Gvk)
44+
}
45+
2946
// TransformerConfig represents the path configurations for different transformations
3047
type TransformerConfig struct {
31-
NamePrefix []PathConfig `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
32-
NameSpace []PathConfig `json:"namespace,omitempty" yaml:"namespace,omitempty"`
33-
CommonLabels []PathConfig `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
34-
CommonAnnotations []PathConfig `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
35-
NameReference []ReferencePathConfig `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
36-
VarReference []PathConfig `json:"varReference,omitempty" yaml:"varReference,omitempty"`
48+
NamePrefix pcSlice `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
49+
NameSpace pcSlice `json:"namespace,omitempty" yaml:"namespace,omitempty"`
50+
CommonLabels pcSlice `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
51+
CommonAnnotations pcSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
52+
NameReference rpcSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
53+
VarReference pcSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
54+
}
55+
56+
// sortFields provides determinism in logging, tests, etc.
57+
func (t *TransformerConfig) sortFields() {
58+
sort.Sort(t.NamePrefix)
59+
sort.Sort(t.NameSpace)
60+
sort.Sort(t.CommonLabels)
61+
sort.Sort(t.CommonAnnotations)
62+
sort.Sort(t.NameReference)
63+
sort.Sort(t.VarReference)
3764
}
3865

3966
// AddPrefixPathConfig adds a PathConfig to NamePrefix
@@ -65,6 +92,7 @@ func (t *TransformerConfig) Merge(input *TransformerConfig) *TransformerConfig {
6592
merged.CommonLabels = append(t.CommonLabels, input.CommonLabels...)
6693
merged.VarReference = append(t.VarReference, input.VarReference...)
6794
merged.NameReference = mergeNameReferencePathConfigs(t.NameReference, input.NameReference)
95+
merged.sortFields()
6896
return merged
6997
}
7098

@@ -92,6 +120,7 @@ func MakeTransformerConfigFromBytes(data []byte) (*TransformerConfig, error) {
92120
if err != nil {
93121
return nil, err
94122
}
123+
t.sortFields()
95124
return &t, nil
96125
}
97126

0 commit comments

Comments
 (0)