Skip to content

Commit

Permalink
add check that kustomization is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Dec 28, 2022
1 parent aec3500 commit 7a2d5db
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
5 changes: 5 additions & 0 deletions api/internal/target/kusttarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func (kt *KustTarget) Load() error {

k.FixKustomization()

// check that Kustomization is empty
if err := k.CheckEmpty(); err != nil {
return err
}

errs := k.EnforceFields()
if len(errs) > 0 {
return fmt.Errorf(
Expand Down
1 change: 1 addition & 0 deletions api/internal/target/kusttarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func TestLoad(t *testing.T) {
k: types.Kustomization{
TypeMeta: expectedTypeMeta,
},
errContains: "kustomization.yaml is empty",
},
"nonsenseLatin": {
errContains: "found a tab character that violates indentation",
Expand Down
21 changes: 21 additions & 0 deletions api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem)
return nil
}

func (k *Kustomization) CheckEmpty() error {
// generate empty Kustomization
emptyKustomization := &Kustomization{}
emptyKustomization.FixKustomization()

// compare with yaml string
b, err := yaml.Marshal(k)
if err != nil {
return fmt.Errorf("kustomization marshal error: %w", err)
}
emptyb, err := yaml.Marshal(emptyKustomization)
if err != nil {
return fmt.Errorf("empty kustomization marshal error: %w", err)
}

if string(b) == string(emptyb) {
return fmt.Errorf("kustomization.yaml is empty")
}
return nil
}

func (k *Kustomization) EnforceFields() []string {
var errs []string
if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind {
Expand Down
56 changes: 48 additions & 8 deletions api/types/kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,54 @@ unknown: foo`)
}
}

func TestUnmarshal_InvalidYaml(t *testing.T) {
y := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1
func TestUnmarshal_Failed(t *testing.T) {
tests := []struct {
name string
kustomizationYamls []byte
errMsg string
}{
{
name: "invalid yaml",
kustomizationYamls: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
unknown`)
var k Kustomization
err := k.Unmarshal(y)
if err == nil {
t.Fatalf("expect an error")
unknown`),
errMsg: "yaml: line 4: could not find expected ':'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var k Kustomization
if err := k.Unmarshal(tt.kustomizationYamls); err == nil || err.Error() != tt.errMsg {
t.Errorf("Kustomization.Unmarshal() error = %v, wantErr %v", err, tt.errMsg)
}
})
}
}

func TestKustomization_CheckEmpty(t *testing.T) {
tests := []struct {
name string
kustomization *Kustomization
wantErr bool
}{
{
name: "empty kustomization.yaml",
kustomization: &Kustomization{},
wantErr: true,
},
{
name: "non empty kustomization.yaml",
kustomization: &Kustomization{Resources: []string{"res"}},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := tt.kustomization
k.FixKustomization()
if err := k.CheckEmpty(); (err != nil) != tt.wantErr {
t.Errorf("Kustomization.CheckEmpty() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 7a2d5db

Please sign in to comment.