diff --git a/k8sdeps/kunstruct/factory.go b/k8sdeps/kunstruct/factory.go index e140b4fd0..d8871a0e1 100644 --- a/k8sdeps/kunstruct/factory.go +++ b/k8sdeps/kunstruct/factory.go @@ -7,6 +7,7 @@ import ( "bytes" "fmt" "io" + "strconv" "strings" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -108,5 +109,30 @@ func (kf *KunstructuredFactoryImpl) validate(u unstructured.Unstructured) error if u.GetName() == "" { return fmt.Errorf("missing metadata.name in object %v", u) } + + if result, path := checkListItemNil(u.Object); result { + return fmt.Errorf("empty item at %v in object %v", path, u) + } return nil } + +func checkListItemNil(in interface{}) (bool, string) { + switch v := in.(type) { + case map[string]interface{}: + for key, s := range v { + if result, path := checkListItemNil(s); result { + return result, key + "/" + path + } + } + case []interface{}: + for index, s := range v { + if s == nil { + return true, "" + } + if result, path := checkListItemNil(s); result { + return result, "[" + strconv.Itoa(index) + "]/" + path + } + } + } + return false, "" +} diff --git a/k8sdeps/kunstruct/factory_test.go b/k8sdeps/kunstruct/factory_test.go index 25a54ad24..d0279d34e 100644 --- a/k8sdeps/kunstruct/factory_test.go +++ b/k8sdeps/kunstruct/factory_test.go @@ -138,6 +138,22 @@ kind: Namespace metadata: annotations: foo: bar +`), + expectedOut: nil, + expectedErr: true, + }, + { + name: "nil value in list", + input: []byte(` +apiVersion: builtin +kind: ConfigMapGenerator +metadata: + name: kube100-site + labels: + app: web + testList: + - testA + - `), expectedOut: nil, expectedErr: true,