mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-09-18 13:22:17 +00:00
Merge pull request #4949 from koba1t/fix/add_check_kustomization_is_empty
add check that kustomization is empty
This commit is contained in:
@@ -73,6 +73,11 @@ func (kt *KustTarget) Load() error {
|
|||||||
|
|
||||||
k.FixKustomization()
|
k.FixKustomization()
|
||||||
|
|
||||||
|
// check that Kustomization is empty
|
||||||
|
if err := k.CheckEmpty(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
errs := k.EnforceFields()
|
errs := k.EnforceFields()
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ func TestLoad(t *testing.T) {
|
|||||||
k: types.Kustomization{
|
k: types.Kustomization{
|
||||||
TypeMeta: expectedTypeMeta,
|
TypeMeta: expectedTypeMeta,
|
||||||
},
|
},
|
||||||
|
errContains: "kustomization.yaml is empty",
|
||||||
},
|
},
|
||||||
"nonsenseLatin": {
|
"nonsenseLatin": {
|
||||||
errContains: "found a tab character that violates indentation",
|
errContains: "found a tab character that violates indentation",
|
||||||
|
|||||||
@@ -9,9 +9,20 @@ import (
|
|||||||
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const expectedResources = `apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: myService
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 7002
|
||||||
|
`
|
||||||
|
|
||||||
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
|
func TestIssue596AllowDirectoriesThatAreSubstringsOfEachOther(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
th.WriteK("base", "")
|
th.WriteF("base/service.yaml", expectedResources)
|
||||||
|
th.WriteK("base", `resources:
|
||||||
|
- service.yaml`)
|
||||||
th.WriteK("overlays/aws", `
|
th.WriteK("overlays/aws", `
|
||||||
resources:
|
resources:
|
||||||
- ../../base
|
- ../../base
|
||||||
@@ -25,5 +36,5 @@ resources:
|
|||||||
- ../aws-nonprod
|
- ../aws-nonprod
|
||||||
`)
|
`)
|
||||||
m := th.Run("overlays/aws-sandbox2.us-east-1", th.MakeDefaultOptions())
|
m := th.Run("overlays/aws-sandbox2.us-east-1", th.MakeDefaultOptions())
|
||||||
th.AssertActualEqualsExpected(m, "")
|
th.AssertActualEqualsExpected(m, expectedResources)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||||
@@ -300,6 +301,20 @@ func (k *Kustomization) FixKustomizationPreMarshalling(fSys filesys.FileSystem)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (k *Kustomization) CheckEmpty() error {
|
||||||
|
// generate empty Kustomization
|
||||||
|
emptyKustomization := &Kustomization{}
|
||||||
|
|
||||||
|
// k.TypeMeta is metadata. It Isn't related to whether empty or not.
|
||||||
|
emptyKustomization.TypeMeta = k.TypeMeta
|
||||||
|
|
||||||
|
if reflect.DeepEqual(k, emptyKustomization) {
|
||||||
|
return fmt.Errorf("kustomization.yaml is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (k *Kustomization) EnforceFields() []string {
|
func (k *Kustomization) EnforceFields() []string {
|
||||||
var errs []string
|
var errs []string
|
||||||
if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind {
|
if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind {
|
||||||
|
|||||||
@@ -284,14 +284,64 @@ unknown: foo`)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal_InvalidYaml(t *testing.T) {
|
func TestUnmarshal_Failed(t *testing.T) {
|
||||||
y := []byte(`
|
tests := []struct {
|
||||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
name string
|
||||||
|
kustomizationYamls []byte
|
||||||
|
errMsg string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "invalid yaml",
|
||||||
|
kustomizationYamls: []byte(`apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
kind: Kustomization
|
kind: Kustomization
|
||||||
unknown`)
|
unknown`),
|
||||||
var k Kustomization
|
errMsg: "invalid Kustomization: yaml: line 4: could not find expected ':'",
|
||||||
err := k.Unmarshal(y)
|
},
|
||||||
if err == nil {
|
}
|
||||||
t.Fatalf("expect an error")
|
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: "empty kustomization.yaml",
|
||||||
|
kustomization: &Kustomization{
|
||||||
|
TypeMeta: TypeMeta{
|
||||||
|
Kind: KustomizationKind,
|
||||||
|
APIVersion: KustomizationVersion,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user