empty list or map item return error

This commit is contained in:
nimohunter
2019-09-09 13:42:18 +08:00
parent 143c5dd21d
commit 9e226001e3
2 changed files with 48 additions and 0 deletions

View File

@@ -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 := checkItemNil(u.Object); result {
return fmt.Errorf("empty item at %v in object %v", path, u)
}
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, ""
}