diff --git a/fixtures/multi_valid.yaml b/fixtures/multi_valid.yaml index a336388..2e8cd70 100644 --- a/fixtures/multi_valid.yaml +++ b/fixtures/multi_valid.yaml @@ -179,3 +179,10 @@ spec: --- # an empty resource with comments --- +apiVersion: v1 +kind: List +items: + - apiVersion: v1 + kind: Namespace + metadata: + name: b diff --git a/kubeval/kubeval.go b/kubeval/kubeval.go index d4c25b5..10e0eef 100644 --- a/kubeval/kubeval.go +++ b/kubeval/kubeval.go @@ -277,24 +277,34 @@ func ValidateWithCache(input []byte, schemaCache map[string]*gojsonschema.Schema return results, nil } - list := struct { - Version string - Kind string - Items []interface{} - }{} - - unmarshalErr := yaml.Unmarshal(input, &list) - isYamlList := unmarshalErr == nil && list.Items != nil - - var bits [][]byte - if isYamlList { - bits = make([][]byte, len(list.Items)) - for i, item := range list.Items { - b, _ := yaml.Marshal(item) - bits[i] = b + splitBits := bytes.Split(input, []byte(detectLineBreak(input)+"---"+detectLineBreak(input))) + bits := make([][]byte, len(splitBits)) + j := 0 + + // split any list into its elements and add them to "bits" + for _, element := range splitBits { + + list := struct { + Version string + Kind string + Items []interface{} + }{} + + unmarshalErr := yaml.Unmarshal(element, &list) + isYamlList := unmarshalErr == nil && list.Items != nil + + if isYamlList { + listBits := make([][]byte, len(list.Items)) + for i, item := range list.Items { + b, _ := yaml.Marshal(item) + listBits[i] = b + } + bits = append(bits, listBits...) + j += len(list.Items) + } else { + bits[j] = element + j++ } - } else { - bits = bytes.Split(input, []byte(detectLineBreak(input)+"---"+detectLineBreak(input))) } var errors *multierror.Error