Skip to content

Commit

Permalink
Merge pull request #284 from carlossg/issue-251
Browse files Browse the repository at this point in the history
fix: List combined with other manifests
  • Loading branch information
carlossg committed Mar 30, 2021
2 parents c63749c + 0dfbdd9 commit 6eebd38
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 6eebd38

Please sign in to comment.