1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-30 19:35:06 +00:00

feat: add yaml util to check empty document (#7276)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-05-24 11:58:43 +02:00 committed by GitHub
parent 44a53d3bd7
commit 492a6761f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"strings"
"k8s.io/apimachinery/pkg/util/yaml"
)
@ -22,7 +23,20 @@ func SplitDocuments(yamlBytes []byte) (documents [][]byte, error error) {
} else if err != nil {
return documents, fmt.Errorf("unable to read yaml")
}
documents = append(documents, b)
if !IsEmptyYamlDocument(b) {
documents = append(documents, b)
}
}
return documents, nil
}
// IsEmptyYamlDocument checks if a yaml document is empty (contains only comments)
func IsEmptyYamlDocument(document []byte) bool {
for _, line := range strings.Split(string(document), "\n") {
line := strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
return false
}
}
return true
}