Repository: kyverno/chainsaw
Pull request #229 (2023-10-27 11:24:21)

chore: use ext/yaml package
Use ext/yaml package.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) Report
> Merging [#229](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) (f289a6d) into [main](https://app.codecov.io/gh/kyverno/chainsaw/commit/db48b7ada152f1de3310cb919d9fca600a95643c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) (db48b7a) will **increase** coverage by `0.13%`.
> Report is 1 commits behind head on main.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main     #229      +/-   ##
==========================================
+ Coverage   56.29%   56.43%   +0.13%     
==========================================
  Files          15       15              
  Lines         627      629       +2     
==========================================
+ Hits          353      355       +2     
  Misses        238      238              
  Partials       36       36              
```


| [Files](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno) | Coverage Δ | |
|---|---|---|
| [pkg/commands/test/command.go](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno#diff-cGtnL2NvbW1hbmRzL3Rlc3QvY29tbWFuZC5nbw==) | `64.17% <100.00%> (+0.54%)` | :arrow_up: |
| [pkg/config/load.go](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno#diff-cGtnL2NvbmZpZy9sb2FkLmdv) | `77.08% <100.00%> (ø)` | |
| [pkg/step/load.go](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno#diff-cGtnL3N0ZXAvbG9hZC5nbw==) | `71.79% <100.00%> (ø)` | |
| [pkg/test/load.go](https://app.codecov.io/gh/kyverno/chainsaw/pull/229?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno#diff-cGtnL3Rlc3QvbG9hZC5nbw==) | `71.79% <100.00%> (ø)` | |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kyverno)


Patch:
diff --git a/go.mod b/go.mod
index 6f6282f3..87a19f31 100644
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.21.1
 require (
 	github.com/dustinkirkland/golang-petname v0.0.0-20231002161417-6a283f1aaaf2
 	github.com/kudobuilder/kuttl v0.15.0
-	github.com/kyverno/kyverno v1.5.0-rc1.0.20231027095036-619c3baab285
+	github.com/kyverno/kyverno v1.5.0-rc1.0.20231027110839-a4b889de63fe
 	github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
 	github.com/spf13/cobra v1.7.0
 	github.com/spf13/pflag v1.0.5
diff --git a/go.sum b/go.sum
index 346ebb79..8bce913e 100644
--- a/go.sum
+++ b/go.sum
@@ -136,6 +136,8 @@ github.com/kudobuilder/kuttl v0.15.0 h1:OmCbpY3ebn+myJaatD+IT1AL1OTz34+Cv6ettiXd
 github.com/kudobuilder/kuttl v0.15.0/go.mod h1:UtAOt+GE8HQZFveEN8p1fk3j+H1Cp5Fk1eMdQkXPqH0=
 github.com/kyverno/kyverno v1.5.0-rc1.0.20231027095036-619c3baab285 h1:StZzhouVDcNQnRqYsgqfzQfcw+ic/gs4LfyIaF8sUOw=
 github.com/kyverno/kyverno v1.5.0-rc1.0.20231027095036-619c3baab285/go.mod h1:v4oHHgiXQB+ppsvmLwI0GckkXuFSmBJQ4YOb9LrMTH8=
+github.com/kyverno/kyverno v1.5.0-rc1.0.20231027110839-a4b889de63fe h1:DbuYlDzR6kgDZkUL6SlD0PteMshIU2pW54yJciDuuAk=
+github.com/kyverno/kyverno v1.5.0-rc1.0.20231027110839-a4b889de63fe/go.mod h1:v4oHHgiXQB+ppsvmLwI0GckkXuFSmBJQ4YOb9LrMTH8=
 github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
 github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
diff --git a/pkg/config/load.go b/pkg/config/load.go
index c6aab70b..ffef403e 100644
--- a/pkg/config/load.go
+++ b/pkg/config/load.go
@@ -10,7 +10,7 @@ import (
 	"github.com/kyverno/chainsaw/pkg/data"
 	"github.com/kyverno/chainsaw/pkg/utils/convert"
 	"github.com/kyverno/chainsaw/pkg/utils/loader"
-	yamlutils "github.com/kyverno/chainsaw/pkg/utils/yaml"
+	"github.com/kyverno/kyverno/ext/yaml"
 	"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
 )
 
@@ -47,7 +47,7 @@ func LoadBytes(content []byte) (*v1alpha1.Configuration, error) {
 }
 
 func Parse(content []byte) ([]*v1alpha1.Configuration, error) {
-	documents, err := yamlutils.SplitDocuments(content)
+	documents, err := yaml.SplitDocuments(content)
 	if err != nil {
 		return nil, err
 	}
diff --git a/pkg/resource/load.go b/pkg/resource/load.go
index 7c7b1e1e..e669ba32 100644
--- a/pkg/resource/load.go
+++ b/pkg/resource/load.go
@@ -5,7 +5,7 @@ import (
 	"os"
 	"path/filepath"
 
-	yamlutils "github.com/kyverno/chainsaw/pkg/utils/yaml"
+	extyaml "github.com/kyverno/kyverno/ext/yaml"
 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/apimachinery/pkg/util/yaml"
 )
@@ -26,7 +26,7 @@ func Load(path string) ([]unstructured.Unstructured, error) {
 }
 
 func Parse(content []byte) ([]unstructured.Unstructured, error) {
-	documents, err := yamlutils.SplitDocuments(content)
+	documents, err := extyaml.SplitDocuments(content)
 	if err != nil {
 		return nil, err
 	}
diff --git a/pkg/step/load.go b/pkg/step/load.go
index df8d8263..02fa9d75 100644
--- a/pkg/step/load.go
+++ b/pkg/step/load.go
@@ -9,7 +9,7 @@ import (
 	"github.com/kyverno/chainsaw/pkg/data"
 	"github.com/kyverno/chainsaw/pkg/utils/convert"
 	"github.com/kyverno/chainsaw/pkg/utils/loader"
-	yamlutils "github.com/kyverno/chainsaw/pkg/utils/yaml"
+	"github.com/kyverno/kyverno/ext/yaml"
 	"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
 )
 
@@ -31,7 +31,7 @@ func Load(path string) ([]*v1alpha1.TestStep, error) {
 }
 
 func Parse(content []byte) ([]*v1alpha1.TestStep, error) {
-	documents, err := yamlutils.SplitDocuments(content)
+	documents, err := yaml.SplitDocuments(content)
 	if err != nil {
 		return nil, err
 	}
diff --git a/pkg/utils/yaml/load.go b/pkg/utils/yaml/load.go
deleted file mode 100644
index 4f930ce3..00000000
--- a/pkg/utils/yaml/load.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package yaml
-
-import (
-	"bufio"
-	"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) {
-	buf := bytes.NewBuffer(yamlBytes)
-	reader := yaml.NewYAMLReader(bufio.NewReader(buf))
-	for {
-		// Read one YAML document at a time, until io.EOF is returned
-		b, err := reader.Read()
-		if err == io.EOF || len(b) == 0 {
-			break
-		} else if err != nil {
-			return documents, fmt.Errorf("unable to read yaml")
-		}
-		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
-}


Test patch:
diff --git a/pkg/test/load.go b/pkg/test/load.go
index 0ed5a584..a37df6ad 100644
--- a/pkg/test/load.go
+++ b/pkg/test/load.go
@@ -9,7 +9,7 @@ import (
 	"github.com/kyverno/chainsaw/pkg/data"
 	"github.com/kyverno/chainsaw/pkg/utils/convert"
 	"github.com/kyverno/chainsaw/pkg/utils/loader"
-	yamlutils "github.com/kyverno/chainsaw/pkg/utils/yaml"
+	"github.com/kyverno/kyverno/ext/yaml"
 	"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
 )
 
@@ -31,7 +31,7 @@ func Load(path string) ([]*v1alpha1.Test, error) {
 }
 
 func Parse(content []byte) ([]*v1alpha1.Test, error) {
-	documents, err := yamlutils.SplitDocuments(content)
+	documents, err := yaml.SplitDocuments(content)
 	if err != nil {
 		return nil, err
 	}