fix using reflect.DeepEqual for check kustomization is empty

This commit is contained in:
koba1t
2023-04-06 05:21:48 +09:00
parent 2c2b2ab825
commit 928b823d8f
2 changed files with 16 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
@@ -301,21 +302,14 @@ func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem)
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)
}
// k.TypeMeta is metadata. It Isn't related to whether empty or not.
emptyKustomization.TypeMeta = k.TypeMeta
if string(b) == string(emptyb) {
if reflect.DeepEqual(k, emptyKustomization) {
return fmt.Errorf("kustomization.yaml is empty")
}
return nil
}

View File

@@ -295,7 +295,7 @@ func TestUnmarshal_Failed(t *testing.T) {
kustomizationYamls: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
unknown`),
errMsg: "yaml: line 4: could not find expected ':'",
errMsg: "invalid Kustomization: yaml: line 4: could not find expected ':'",
},
}
for _, tt := range tests {
@@ -319,6 +319,16 @@ func TestKustomization_CheckEmpty(t *testing.T) {
kustomization: &Kustomization{},
wantErr: true,
},
{
name: "empty kustomization.yaml",
kustomization: &Kustomization{
TypeMeta: TypeMeta{
Kind: KustomizationKind,
APIVersion: KustomizationVersion,
},
},
wantErr: true,
},
{
name: "non empty kustomization.yaml",
kustomization: &Kustomization{Resources: []string{"res"}},