From a4b889de63fe27052e92cc30fbe53959e524100f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 27 Oct 2023 13:08:39 +0200 Subject: [PATCH] feat: add ext/yaml package (#8760) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- cmd/cli/kubectl-kyverno/exception/load.go | 2 +- cmd/cli/kubectl-kyverno/policy/load.go | 3 ++- cmd/cli/kubectl-kyverno/resource/resource.go | 2 +- ext/yaml/document.go | 3 +++ ext/yaml/empty.go | 16 ++++++++++++++++ pkg/utils/yaml/utils.go => ext/yaml/split.go | 16 ++-------------- .../yaml/utils_test.go => ext/yaml/split_test.go | 0 pkg/utils/yaml/loadpolicy.go | 3 ++- 8 files changed, 27 insertions(+), 18 deletions(-) create mode 100644 ext/yaml/document.go create mode 100644 ext/yaml/empty.go rename pkg/utils/yaml/utils.go => ext/yaml/split.go (60%) rename pkg/utils/yaml/utils_test.go => ext/yaml/split_test.go (100%) diff --git a/cmd/cli/kubectl-kyverno/exception/load.go b/cmd/cli/kubectl-kyverno/exception/load.go index bf6f33d763..4412fd978a 100644 --- a/cmd/cli/kubectl-kyverno/exception/load.go +++ b/cmd/cli/kubectl-kyverno/exception/load.go @@ -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" ) diff --git a/cmd/cli/kubectl-kyverno/policy/load.go b/cmd/cli/kubectl-kyverno/policy/load.go index 02e340a64a..b6b7883901 100644 --- a/cmd/cli/kubectl-kyverno/policy/load.go +++ b/cmd/cli/kubectl-kyverno/policy/load.go @@ -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 } diff --git a/cmd/cli/kubectl-kyverno/resource/resource.go b/cmd/cli/kubectl-kyverno/resource/resource.go index 11b6e36bc7..cd5ccd2da4 100644 --- a/cmd/cli/kubectl-kyverno/resource/resource.go +++ b/cmd/cli/kubectl-kyverno/resource/resource.go @@ -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" ) diff --git a/ext/yaml/document.go b/ext/yaml/document.go new file mode 100644 index 0000000000..8c165fdf7b --- /dev/null +++ b/ext/yaml/document.go @@ -0,0 +1,3 @@ +package yaml + +type document = []byte diff --git a/ext/yaml/empty.go b/ext/yaml/empty.go new file mode 100644 index 0000000000..d68765d449 --- /dev/null +++ b/ext/yaml/empty.go @@ -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 +} diff --git a/pkg/utils/yaml/utils.go b/ext/yaml/split.go similarity index 60% rename from pkg/utils/yaml/utils.go rename to ext/yaml/split.go index 4f930ce368..8c5da252ac 100644 --- a/pkg/utils/yaml/utils.go +++ b/ext/yaml/split.go @@ -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 -} diff --git a/pkg/utils/yaml/utils_test.go b/ext/yaml/split_test.go similarity index 100% rename from pkg/utils/yaml/utils_test.go rename to ext/yaml/split_test.go diff --git a/pkg/utils/yaml/loadpolicy.go b/pkg/utils/yaml/loadpolicy.go index 16ac1b70c1..e66c4e606f 100644 --- a/pkg/utils/yaml/loadpolicy.go +++ b/pkg/utils/yaml/loadpolicy.go @@ -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 }