1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

feat: add ext/yaml package (#8760)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-10-27 13:08:39 +02:00 committed by GitHub
parent 619c3baab2
commit a4b889de63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 18 deletions

View file

@ -8,7 +8,7 @@ import (
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/data"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/convert"
resourceloader "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/loader"
yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
yamlutils "github.com/kyverno/kyverno/ext/yaml"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
)

View file

@ -17,6 +17,7 @@ import (
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/convert"
resourceloader "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource/loader"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/source"
extyaml "github.com/kyverno/kyverno/ext/yaml"
"github.com/kyverno/kyverno/pkg/utils/git"
yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
"k8s.io/api/admissionregistration/v1alpha1"
@ -92,7 +93,7 @@ func LoadWithLoader(loader loader, fs billy.Filesystem, resourcePath string, pat
}
func kubectlValidateLoader(content []byte) ([]kyvernov1.PolicyInterface, []v1alpha1.ValidatingAdmissionPolicy, error) {
documents, err := yamlutils.SplitDocuments(content)
documents, err := extyaml.SplitDocuments(content)
if err != nil {
return nil, nil, err
}

View file

@ -11,9 +11,9 @@ import (
"github.com/go-git/go-billy/v5"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/source"
yamlutils "github.com/kyverno/kyverno/ext/yaml"
"github.com/kyverno/kyverno/pkg/client/clientset/versioned/scheme"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)

3
ext/yaml/document.go Normal file
View file

@ -0,0 +1,3 @@
package yaml
type document = []byte

16
ext/yaml/empty.go Normal file
View file

@ -0,0 +1,16 @@
package yaml
import (
"strings"
)
// IsEmptyDocument checks if a yaml document is empty (contains only comments)
func IsEmptyDocument(document document) bool {
for _, line := range strings.Split(string(document), "\n") {
line := strings.TrimSpace(line)
if line != "" && !strings.HasPrefix(line, "#") {
return false
}
}
return true
}

View file

@ -5,14 +5,13 @@ import (
"bytes"
"fmt"
"io"
"strings"
"k8s.io/apimachinery/pkg/util/yaml"
)
// SplitDocuments reads the YAML bytes per-document, unmarshals the TypeMeta information from each document
// and returns a map between the GroupVersionKind of the document and the document bytes
func SplitDocuments(yamlBytes []byte) (documents [][]byte, error error) {
func SplitDocuments(yamlBytes document) (documents []document, error error) {
buf := bytes.NewBuffer(yamlBytes)
reader := yaml.NewYAMLReader(bufio.NewReader(buf))
for {
@ -23,20 +22,9 @@ func SplitDocuments(yamlBytes []byte) (documents [][]byte, error error) {
} else if err != nil {
return documents, fmt.Errorf("unable to read yaml")
}
if !IsEmptyYamlDocument(b) {
if !IsEmptyDocument(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
}

View file

@ -6,6 +6,7 @@ import (
"strings"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
extyaml "github.com/kyverno/kyverno/ext/yaml"
log "github.com/kyverno/kyverno/pkg/logging"
"k8s.io/api/admissionregistration/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@ -15,7 +16,7 @@ import (
// GetPolicy extracts policies from YAML bytes
func GetPolicy(bytes []byte) (policies []kyvernov1.PolicyInterface, validatingAdmissionPolicies []v1alpha1.ValidatingAdmissionPolicy, err error) {
documents, err := SplitDocuments(bytes)
documents, err := extyaml.SplitDocuments(bytes)
if err != nil {
return nil, nil, err
}