mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
empty list or map item return error
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
@@ -108,5 +109,30 @@ func (kf *KunstructuredFactoryImpl) validate(u unstructured.Unstructured) error
|
|||||||
if u.GetName() == "" {
|
if u.GetName() == "" {
|
||||||
return fmt.Errorf("missing metadata.name in object %v", u)
|
return fmt.Errorf("missing metadata.name in object %v", u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result, path := checkItemNil(u.Object); result {
|
||||||
|
return fmt.Errorf("empty item at %v in object %v", path, u)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkItemNil(in interface{}) (bool, string) {
|
||||||
|
if in == nil {
|
||||||
|
return true, ""
|
||||||
|
}
|
||||||
|
switch v := in.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
for key, s := range v {
|
||||||
|
if result, path := checkItemNil(s); result {
|
||||||
|
return result, key + "/" + path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
for index, s := range v {
|
||||||
|
if result, path := checkItemNil(s); result {
|
||||||
|
return result, "[" + strconv.Itoa(index) + "]/" + path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|||||||
@@ -98,6 +98,21 @@ items:
|
|||||||
patch7 := `
|
patch7 := `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: List
|
kind: List
|
||||||
|
`
|
||||||
|
patchList5 := types.PatchStrategicMerge("patch8.yaml")
|
||||||
|
patch8 := `
|
||||||
|
apiVersion: builtin
|
||||||
|
kind: ConfigMapGenerator
|
||||||
|
metadata:
|
||||||
|
name: kube100-site
|
||||||
|
labels:
|
||||||
|
app: web
|
||||||
|
testList:
|
||||||
|
- testA
|
||||||
|
-
|
||||||
|
literals:
|
||||||
|
- FRUIT=apple
|
||||||
|
- VEGETABLE=carrot
|
||||||
`
|
`
|
||||||
testDeploymentSpec := map[string]interface{}{
|
testDeploymentSpec := map[string]interface{}{
|
||||||
"template": map[string]interface{}{
|
"template": map[string]interface{}{
|
||||||
@@ -139,6 +154,7 @@ kind: List
|
|||||||
l.AddFile("/"+string(patchList2), []byte(patch5))
|
l.AddFile("/"+string(patchList2), []byte(patch5))
|
||||||
l.AddFile("/"+string(patchList3), []byte(patch6))
|
l.AddFile("/"+string(patchList3), []byte(patch6))
|
||||||
l.AddFile("/"+string(patchList4), []byte(patch7))
|
l.AddFile("/"+string(patchList4), []byte(patch7))
|
||||||
|
l.AddFile("/"+string(patchList5), []byte(patch8))
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -188,6 +204,12 @@ kind: List
|
|||||||
expectedOut: []*Resource{},
|
expectedOut: []*Resource{},
|
||||||
expectedErr: false,
|
expectedErr: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "listWithEmptyList",
|
||||||
|
input: []types.PatchStrategicMerge{patchList5},
|
||||||
|
expectedOut: []*Resource{},
|
||||||
|
expectedErr: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
rs, err := factory.SliceFromPatches(l, test.input)
|
rs, err := factory.SliceFromPatches(l, test.input)
|
||||||
|
|||||||
Reference in New Issue
Block a user