Skip to content

Commit 16a7ce2

Browse files
authored
Fix sortOptions removal when running edit command (#5689)
* Fix sortOptions and Validators removal when running edit command * fixed linting
1 parent 92e862c commit 16a7ce2

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

kustomize/commands/internal/kustfile/kustomizationfile.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func determineFieldOrder() []string {
4040

4141
ordered := []string{
4242
"MetaData",
43+
"SortOptions",
4344
"Resources",
4445
"Bases",
4546
"NamePrefix",
@@ -65,6 +66,7 @@ func determineFieldOrder() []string {
6566
"Configurations",
6667
"Generators",
6768
"Transformers",
69+
"Validators",
6870
"Components",
6971
"OpenAPI",
7072
"BuildMetadata",

kustomize/commands/internal/kustfile/kustomizationfile_test.go

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package kustfile
55

66
import (
77
"reflect"
8+
"slices"
89
"strings"
910
"testing"
1011

@@ -21,6 +22,7 @@ func TestFieldOrder(t *testing.T) {
2122
"APIVersion",
2223
"Kind",
2324
"MetaData",
25+
"SortOptions",
2426
"Resources",
2527
"Bases",
2628
"NamePrefix",
@@ -46,6 +48,7 @@ func TestFieldOrder(t *testing.T) {
4648
"Configurations",
4749
"Generators",
4850
"Transformers",
51+
"Validators",
4952
"Components",
5053
"OpenAPI",
5154
"BuildMetadata",
@@ -87,6 +90,154 @@ func TestWriteAndRead(t *testing.T) {
8790
}
8891
}
8992

93+
func TestReadAndWrite(t *testing.T) {
94+
kWrite := []byte(completeKustfileInOrder)
95+
96+
fSys := filesys.MakeFsInMemory()
97+
testutils_test.WriteTestKustomizationWith(fSys, kWrite)
98+
mf, err := NewKustomizationFile(fSys)
99+
if err != nil {
100+
t.Fatalf("Unexpected Error: %v", err)
101+
}
102+
103+
kustomization, err := mf.Read()
104+
if err != nil {
105+
t.Fatalf("Unexpected Error: %v", err)
106+
}
107+
108+
if err := mf.Write(kustomization); err != nil {
109+
t.Fatalf("Couldn't write kustomization file: %v\n", err)
110+
}
111+
112+
kRead, err := testutils_test.ReadTestKustomization(fSys)
113+
if err != nil {
114+
t.Fatalf("Unexpected Error: %v", err)
115+
}
116+
117+
if !reflect.DeepEqual(kWrite, kRead) {
118+
t.Fatal("Written kustomization is different from read kustomization")
119+
}
120+
}
121+
122+
func TestReadAndWriteDummy(t *testing.T) {
123+
kWrite := &types.Kustomization{
124+
TypeMeta: types.TypeMeta{
125+
APIVersion: "kustomize.config.k8s.io/v1beta1",
126+
Kind: "Kustomization",
127+
},
128+
MetaData: &types.ObjectMeta{
129+
Name: "name",
130+
Namespace: "namespace",
131+
Labels: map[string]string{"label": "label"},
132+
Annotations: map[string]string{"annotation": "annotation"},
133+
},
134+
OpenAPI: map[string]string{"path": "schema.json"},
135+
NamePrefix: "prefix",
136+
NameSuffix: "suffix",
137+
Namespace: "namespace",
138+
CommonLabels: map[string]string{"commonLabel": "commonLabel"},
139+
Labels: []types.Label{{
140+
Pairs: map[string]string{"label": "label"},
141+
IncludeSelectors: true,
142+
IncludeTemplates: true,
143+
FieldSpecs: []types.FieldSpec{{
144+
Path: "metadata.labels.label",
145+
}},
146+
}},
147+
CommonAnnotations: map[string]string{"commonAnnotation": "commonAnnotation"},
148+
Patches: []types.Patch{{
149+
Path: "path",
150+
}},
151+
Images: []types.Image{{
152+
Name: "name",
153+
NewName: "newName",
154+
TagSuffix: "tagSuffix",
155+
NewTag: "newTag",
156+
Digest: "digest",
157+
}},
158+
Replacements: []types.ReplacementField{{
159+
Path: "path",
160+
}},
161+
Replicas: []types.Replica{{
162+
Name: "name",
163+
Count: 1,
164+
}},
165+
SortOptions: &types.SortOptions{
166+
Order: types.LegacySortOrder,
167+
LegacySortOptions: &types.LegacySortOptions{
168+
OrderFirst: []string{"orderFirst"},
169+
OrderLast: []string{"orderLast"},
170+
},
171+
},
172+
Resources: []string{"resource"},
173+
Components: []string{"component"},
174+
Crds: []string{"crd"},
175+
ConfigMapGenerator: []types.ConfigMapArgs{{
176+
GeneratorArgs: types.GeneratorArgs{
177+
Namespace: "namespace",
178+
Name: "name",
179+
},
180+
}},
181+
SecretGenerator: []types.SecretArgs{{
182+
GeneratorArgs: types.GeneratorArgs{
183+
Namespace: "namespace",
184+
Name: "name",
185+
},
186+
}},
187+
HelmGlobals: &types.HelmGlobals{
188+
ChartHome: "chartHome",
189+
ConfigHome: "configHome",
190+
},
191+
HelmCharts: []types.HelmChart{{
192+
Name: "name",
193+
}},
194+
GeneratorOptions: &types.GeneratorOptions{
195+
Labels: map[string]string{"label": "label"},
196+
},
197+
Configurations: []string{"configuration"},
198+
Generators: []string{"generator"},
199+
Transformers: []string{"transformer"},
200+
Validators: []string{"validator"},
201+
BuildMetadata: []string{"buildMetadata"},
202+
}
203+
204+
// this check is for forward compatibility: if this fails, add a dummy value to the Kustomization above
205+
assertAllNonZeroExcept(t, kWrite, []string{"PatchesStrategicMerge", "PatchesJson6902", "ImageTags", "Vars", "Bases", "HelmChartInflationGenerator"})
206+
207+
fSys := filesys.MakeFsInMemory()
208+
testutils_test.WriteTestKustomization(fSys)
209+
mf, err := NewKustomizationFile(fSys)
210+
if err != nil {
211+
t.Fatalf("Unexpected Error: %v", err)
212+
}
213+
214+
if err := mf.Write(kWrite); err != nil {
215+
t.Fatalf("Couldn't write kustomization file: %v\n", err)
216+
}
217+
218+
kRead, err := mf.Read()
219+
if err != nil {
220+
t.Fatalf("Unexpected Error: %v", err)
221+
}
222+
223+
if !reflect.DeepEqual(kWrite, kRead) {
224+
t.Fatal("Written kustomization is different from read kustomization.")
225+
}
226+
}
227+
228+
func assertAllNonZeroExcept(t *testing.T, val *types.Kustomization, except []string) {
229+
fFor := reflect.ValueOf(val).Elem()
230+
n := fFor.NumField()
231+
for i := 0; i < n; i++ {
232+
key := fFor.Type().Field(i).Name
233+
val := fFor.Field(i)
234+
if val.IsZero() && !slices.Contains(except, key) {
235+
t.Helper()
236+
t.Fatalf("Key %s should not be empty", key)
237+
}
238+
}
239+
}
240+
90241
func TestGetPath(t *testing.T) {
91242
fSys := filesys.MakeEmptyDirInMemory()
92243
testutils_test.WriteTestKustomization(fSys)
@@ -386,3 +537,80 @@ foo:
386537
t.Fatalf("Expect an unknown field error but got: %v", err)
387538
}
388539
}
540+
541+
const completeKustfileInOrder = `
542+
kind: Kustomization
543+
apiVersion: kustomize.config.k8s.io/v1beta1
544+
metadata:
545+
annotations:
546+
annotation: annotation
547+
labels:
548+
label: label
549+
name: name
550+
namespace: namespace
551+
openapi:
552+
path: schema.json
553+
namePrefix: prefix
554+
nameSuffix: suffix
555+
namespace: namespace
556+
commonLabels:
557+
commonLabel: commonLabel
558+
labels:
559+
- fields:
560+
- path: metadata.labels.label
561+
includeSelectors: true
562+
includeTemplates: true
563+
pairs:
564+
label: label
565+
commonAnnotations:
566+
commonAnnotation: commonAnnotation
567+
patches:
568+
- path: path
569+
images:
570+
- digest: digest
571+
name: name
572+
newName: newName
573+
newTag: newTag
574+
tagSuffix: tagSuffix
575+
replacements:
576+
- path: path
577+
replicas:
578+
- count: 1
579+
name: name
580+
sortOptions:
581+
legacySortOptions:
582+
orderFirst:
583+
- orderFirst
584+
orderLast:
585+
- orderLast
586+
order: legacy
587+
resources:
588+
- resource
589+
components:
590+
- component
591+
crds:
592+
- crd
593+
configMapGenerator:
594+
- name: name
595+
namespace: namespace
596+
secretGenerator:
597+
- name: name
598+
namespace: namespace
599+
helmGlobals:
600+
chartHome: chartHome
601+
configHome: configHome
602+
helmCharts:
603+
- name: name
604+
- name: chartName
605+
generatorOptions:
606+
labels:
607+
label: label
608+
configurations:
609+
- configuration
610+
generators:
611+
- generator
612+
transformers:
613+
- transformer
614+
buildMetadata:
615+
- buildMetadata
616+
`

0 commit comments

Comments
 (0)