Skip to content

Commit

Permalink
Fix sortOptions and Validators removal when running edit command
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-f committed May 9, 2024
1 parent e244b83 commit c1a30e6
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kustomize/commands/internal/kustfile/kustomizationfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func determineFieldOrder() []string {

ordered := []string{
"MetaData",
"SortOptions",
"Resources",
"Bases",
"NamePrefix",
Expand All @@ -65,6 +66,7 @@ func determineFieldOrder() []string {
"Configurations",
"Generators",
"Transformers",
"Validators",
"Components",
"OpenAPI",
"BuildMetadata",
Expand Down
227 changes: 227 additions & 0 deletions kustomize/commands/internal/kustfile/kustomizationfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package kustfile

import (
"reflect"
"slices"
"strings"
"testing"

Expand All @@ -21,6 +22,7 @@ func TestFieldOrder(t *testing.T) {
"APIVersion",
"Kind",
"MetaData",
"SortOptions",
"Resources",
"Bases",
"NamePrefix",
Expand All @@ -46,6 +48,7 @@ func TestFieldOrder(t *testing.T) {
"Configurations",
"Generators",
"Transformers",
"Validators",
"Components",
"OpenAPI",
"BuildMetadata",
Expand Down Expand Up @@ -87,6 +90,153 @@ func TestWriteAndRead(t *testing.T) {
}
}

func TestReadAndWrite(t *testing.T) {
kWrite := []byte(completeKustfileInOrder)

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kWrite)
mf, err := NewKustomizationFile(fSys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}

kustomization, err := mf.Read()
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}

if err := mf.Write(kustomization); err != nil {
t.Fatalf("Couldn't write kustomization file: %v\n", err)
}

kRead, err := testutils_test.ReadTestKustomization(fSys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}

if !reflect.DeepEqual(kWrite, kRead) {
t.Fatal("Written kustomization is different from read kustomization")
}
}

func TestReadAndWriteDummy(t *testing.T) {
kWrite := &types.Kustomization{
TypeMeta: types.TypeMeta{
APIVersion: "kustomize.config.k8s.io/v1beta1",
Kind: "Kustomization",
},
MetaData: &types.ObjectMeta{
Name: "name",
Namespace: "namespace",
Labels: map[string]string{"label": "label"},
Annotations: map[string]string{"annotation": "annotation"},
},
OpenAPI: map[string]string{"path": "schema.json"},
NamePrefix: "prefix",
NameSuffix: "suffix",
Namespace: "namespace",
CommonLabels: map[string]string{"commonLabel": "commonLabel"},
Labels: []types.Label{{
Pairs: map[string]string{"label": "label"},
IncludeSelectors: true,
IncludeTemplates: true,
FieldSpecs: []types.FieldSpec{{
Path: "metadata.labels.label",
}},
}},
CommonAnnotations: map[string]string{"commonAnnotation": "commonAnnotation"},
Patches: []types.Patch{{
Path: "path",
}},
Images: []types.Image{{
Name: "name",
NewName: "newName",
TagSuffix: "tagSuffix",
NewTag: "newTag",
Digest: "digest",
}},
Replacements: []types.ReplacementField{{
Path: "path",
}},
Replicas: []types.Replica{{
Name: "name",
Count: 1,
}},
SortOptions: &types.SortOptions{
Order: types.LegacySortOrder,
LegacySortOptions: &types.LegacySortOptions{
OrderFirst: []string{"orderFirst"},
OrderLast: []string{"orderLast"},
},
},
Resources: []string{"resource"},
Components: []string{"component"},
Crds: []string{"crd"},
ConfigMapGenerator: []types.ConfigMapArgs{{
GeneratorArgs: types.GeneratorArgs{
Namespace: "namespace",
Name: "name",
},
}},
SecretGenerator: []types.SecretArgs{{
GeneratorArgs: types.GeneratorArgs{
Namespace: "namespace",
Name: "name",
},
}},
HelmGlobals: &types.HelmGlobals{
ChartHome: "chartHome",
ConfigHome: "configHome",
},
HelmCharts: []types.HelmChart{{
Name: "name",
}},
GeneratorOptions: &types.GeneratorOptions{
Labels: map[string]string{"label": "label"},
},
Configurations: []string{"configuration"},
Generators: []string{"generator"},
Transformers: []string{"transformer"},
Validators: []string{"validator"},
BuildMetadata: []string{"buildMetadata"},
}

// this check is for forward compability: if this fails, add a dummy value to the Kustomization above
assertAllNonZeroExcept(t, kWrite, []string{"PatchesStrategicMerge", "PatchesJson6902", "ImageTags", "Vars", "Bases", "HelmChartInflationGenerator"})

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomization(fSys)
mf, err := NewKustomizationFile(fSys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}

if err := mf.Write(kWrite); err != nil {
t.Fatalf("Couldn't write kustomization file: %v\n", err)
}

kRead, err := mf.Read()
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}

if !reflect.DeepEqual(kWrite, kRead) {
t.Fatal("Written kustomization is different from read kustomization.")
}
}

func assertAllNonZeroExcept(t *testing.T, val *types.Kustomization, except []string) {
fFor := reflect.ValueOf(val).Elem()
n := fFor.NumField()
for i := 0; i < n; i++ {
key := fFor.Type().Field(i).Name
val := fFor.Field(i)
if val.IsZero() && !slices.Contains(except, key) {
t.Fatalf("Key %s should not be empty", key)
}
}
}

func TestGetPath(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
testutils_test.WriteTestKustomization(fSys)
Expand Down Expand Up @@ -386,3 +536,80 @@ foo:
t.Fatalf("Expect an unknown field error but got: %v", err)
}
}

const completeKustfileInOrder = `
kind: Kustomization
apiVersion: kustomize.config.k8s.io/v1beta1
metadata:
annotations:
annotation: annotation
labels:
label: label
name: name
namespace: namespace
openapi:
path: schema.json
namePrefix: prefix
nameSuffix: suffix
namespace: namespace
commonLabels:
commonLabel: commonLabel
labels:
- fields:
- path: metadata.labels.label
includeSelectors: true
includeTemplates: true
pairs:
label: label
commonAnnotations:
commonAnnotation: commonAnnotation
patches:
- path: path
images:
- digest: digest
name: name
newName: newName
newTag: newTag
tagSuffix: tagSuffix
replacements:
- path: path
replicas:
- count: 1
name: name
sortOptions:
legacySortOptions:
orderFirst:
- orderFirst
orderLast:
- orderLast
order: legacy
resources:
- resource
components:
- component
crds:
- crd
configMapGenerator:
- name: name
namespace: namespace
secretGenerator:
- name: name
namespace: namespace
helmGlobals:
chartHome: chartHome
configHome: configHome
helmCharts:
- name: name
- name: chartName
generatorOptions:
labels:
label: label
configurations:
- configuration
generators:
- generator
transformers:
- transformer
buildMetadata:
- buildMetadata
`

0 comments on commit c1a30e6

Please sign in to comment.