1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

refactor: separate yaml utils package (#3520)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-04-01 11:56:16 +02:00 committed by GitHub
parent 857cd1209c
commit bc6a228f7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 39 deletions

View file

@ -13,7 +13,7 @@ import (
"github.com/kyverno/kyverno/pkg/autogen" "github.com/kyverno/kyverno/pkg/autogen"
client "github.com/kyverno/kyverno/pkg/dclient" client "github.com/kyverno/kyverno/pkg/dclient"
engineutils "github.com/kyverno/kyverno/pkg/engine/utils" engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
"github.com/kyverno/kyverno/pkg/utils" yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
@ -167,7 +167,7 @@ func GetResource(resourceBytes []byte) ([]*unstructured.Unstructured, error) {
resources := make([]*unstructured.Unstructured, 0) resources := make([]*unstructured.Unstructured, 0)
var getErrString string var getErrString string
files, splitDocError := utils.SplitYAMLDocuments(resourceBytes) files, splitDocError := yamlutils.SplitDocuments(resourceBytes)
if splitDocError != nil { if splitDocError != nil {
return nil, splitDocError return nil, splitDocError
} }

View file

@ -1,72 +1,44 @@
package utils package utils
import ( import (
"bufio"
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
v1 "github.com/kyverno/kyverno/api/kyverno/v1" kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
yamlutils "github.com/kyverno/kyverno/pkg/utils/yaml"
"k8s.io/apimachinery/pkg/util/yaml" "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log"
) )
// GetPolicy - extracts policies from YAML bytes // GetPolicy - extracts policies from YAML bytes
func GetPolicy(bytes []byte) (clusterPolicies []v1.PolicyInterface, err error) { func GetPolicy(bytes []byte) (policies []kyverno.PolicyInterface, err error) {
policies, err := SplitYAMLDocuments(bytes) documents, err := yamlutils.SplitDocuments(bytes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, thisPolicyBytes := range documents {
for _, thisPolicyBytes := range policies {
policyBytes, err := yaml.ToJSON(thisPolicyBytes) policyBytes, err := yaml.ToJSON(thisPolicyBytes)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to convert to JSON: %v", err) return nil, fmt.Errorf("failed to convert to JSON: %v", err)
} }
policy := &kyverno.ClusterPolicy{}
policy := &v1.ClusterPolicy{}
if err := json.Unmarshal(policyBytes, policy); err != nil { if err := json.Unmarshal(policyBytes, policy); err != nil {
return nil, fmt.Errorf("failed to decode policy: %v", err) return nil, fmt.Errorf("failed to decode policy: %v", err)
} }
if policy.TypeMeta.Kind == "" { if policy.TypeMeta.Kind == "" {
log.Log.V(3).Info("skipping file as policy.TypeMeta.Kind not found") log.Log.V(3).Info("skipping file as policy.TypeMeta.Kind not found")
continue continue
} }
if policy.TypeMeta.Kind != "ClusterPolicy" && policy.TypeMeta.Kind != "Policy" {
if !(policy.TypeMeta.Kind == "ClusterPolicy" || policy.TypeMeta.Kind == "Policy") { return nil, fmt.Errorf("resource %s/%s is not a Policy or a ClusterPolicy", policy.Kind, policy.Name)
msg := fmt.Sprintf("resource %s/%s is not a Policy or a ClusterPolicy", policy.Kind, policy.Name)
return nil, fmt.Errorf(msg)
} }
if policy.Namespace != "" || (policy.Namespace == "" && policy.Kind == "Policy") { if policy.Namespace != "" || (policy.Namespace == "" && policy.Kind == "Policy") {
if policy.Namespace == "" { if policy.Namespace == "" {
policy.Namespace = "default" policy.Namespace = "default"
} }
policy.Kind = "ClusterPolicy" policy.Kind = "ClusterPolicy"
} }
clusterPolicies = append(clusterPolicies, policy) policies = append(policies, policy)
}
return clusterPolicies, nil
}
// SplitYAMLDocuments 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 SplitYAMLDocuments(yamlBytes []byte) (policies [][]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 policies, fmt.Errorf("unable to read yaml")
}
policies = append(policies, b)
} }
return policies, nil return policies, nil
} }

28
pkg/utils/yaml/utils.go Normal file
View file

@ -0,0 +1,28 @@
package yaml
import (
"bufio"
"bytes"
"fmt"
"io"
"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")
}
documents = append(documents, b)
}
return documents, nil
}