Skip to content

Commit

Permalink
fix: List combined with other manifests
Browse files Browse the repository at this point in the history
Fixes #251
  • Loading branch information
carlossg committed Mar 30, 2021
1 parent c63749c commit 0dfbdd9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
7 changes: 7 additions & 0 deletions fixtures/multi_valid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,10 @@ spec:
---
# an empty resource with comments
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Namespace
metadata:
name: b
44 changes: 27 additions & 17 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0dfbdd9

Please sign in to comment.