mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
fix using reflect.DeepEqual for check kustomization is empty
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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"}},
|
||||
|
||||
Reference in New Issue
Block a user