diff --git a/api/types/kustomization.go b/api/types/kustomization.go index 878f96658..13b7b9579 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -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 } diff --git a/api/types/kustomization_test.go b/api/types/kustomization_test.go index aab4a160e..963f774f6 100644 --- a/api/types/kustomization_test.go +++ b/api/types/kustomization_test.go @@ -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"}},