mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
calling GetCRD's from validate
This commit is contained in:
parent
e5f5ef9db1
commit
ccfd4adad3
2 changed files with 7 additions and 31 deletions
|
@ -14,8 +14,6 @@ import (
|
||||||
yaml_v2 "sigs.k8s.io/yaml"
|
yaml_v2 "sigs.k8s.io/yaml"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
jsonpatch "github.com/evanphx/json-patch"
|
jsonpatch "github.com/evanphx/json-patch"
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
v1 "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
||||||
|
@ -216,23 +214,10 @@ func MutatePolicy(policy *v1.ClusterPolicy, logger logr.Logger) (*v1.ClusterPoli
|
||||||
return &p, nil
|
return &p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ConvertFileToUnstructed - converting file to unstructured
|
|
||||||
func ConvertFileToUnstructed(crdPaths []string) (crds []*unstructured.Unstructured, err error) {
|
|
||||||
crds, err = GetCRDs(crdPaths)
|
|
||||||
if err != nil {
|
|
||||||
if !sanitizedError.IsErrorSanitized(err) {
|
|
||||||
return nil, sanitizedError.NewWithError((fmt.Sprintf("failed to parse %v path/s.", crdPaths)), err)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return crds, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetCRDs - Extracting the crds from multiple YAML
|
// GetCRDs - Extracting the crds from multiple YAML
|
||||||
func GetCRDs(paths []string) (unstructuredCrds []*unstructured.Unstructured, err error) {
|
func GetCRDs(paths []string) (unstructuredCrds []*unstructured.Unstructured, err error) {
|
||||||
log := log.Log
|
log := log.Log
|
||||||
|
unstructuredCrds = make([]*unstructured.Unstructured, 0)
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
|
|
||||||
|
@ -265,35 +250,23 @@ func GetCRDs(paths []string) (unstructuredCrds []*unstructured.Unstructured, err
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to extract crds: %s\n", err)
|
fmt.Printf("failed to extract crds: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
unstructuredCrds = append(unstructuredCrds, getCRDs...)
|
unstructuredCrds = append(unstructuredCrds, getCRDs...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return unstructuredCrds, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCRD - Extracts crds from a YAML
|
// GetCRD - Extracts crds from a YAML
|
||||||
func GetCRD(path string) (unstructuredCrds []*unstructured.Unstructured, err error) {
|
func GetCRD(path string) (unstructuredCrds []*unstructured.Unstructured, err error) {
|
||||||
log := log.Log
|
log := log.Log
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
|
unstructuredCrds = make([]*unstructured.Unstructured, 0)
|
||||||
fileDesc, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err, "failed to describe file", "file", path)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if fileDesc.IsDir() {
|
|
||||||
return nil, errors.New("path should be a file")
|
|
||||||
}
|
|
||||||
|
|
||||||
yamlbytes, err := ioutil.ReadFile(path)
|
yamlbytes, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err, "failed to read file", "file", path)
|
log.Error(err, "failed to read file", "file", path)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var u unstructured.Unstructured
|
|
||||||
buf := bytes.NewBuffer(yamlbytes)
|
buf := bytes.NewBuffer(yamlbytes)
|
||||||
reader := yaml.NewYAMLReader(bufio.NewReader(buf))
|
reader := yaml.NewYAMLReader(bufio.NewReader(buf))
|
||||||
|
|
||||||
|
@ -305,6 +278,7 @@ func GetCRD(path string) (unstructuredCrds []*unstructured.Unstructured, err err
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Error(err, "unable to read yaml")
|
log.Error(err, "unable to read yaml")
|
||||||
}
|
}
|
||||||
|
var u unstructured.Unstructured
|
||||||
err = yaml_v2.Unmarshal(b, &u)
|
err = yaml_v2.Unmarshal(b, &u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err, "failed to convert file into unstructured object", "file", path)
|
log.Error(err, "failed to convert file into unstructured object", "file", path)
|
||||||
|
@ -312,5 +286,6 @@ func GetCRD(path string) (unstructuredCrds []*unstructured.Unstructured, err err
|
||||||
}
|
}
|
||||||
unstructuredCrds = append(unstructuredCrds, &u)
|
unstructuredCrds = append(unstructuredCrds, &u)
|
||||||
}
|
}
|
||||||
|
|
||||||
return unstructuredCrds, nil
|
return unstructuredCrds, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,8 @@ func Command() *cobra.Command {
|
||||||
|
|
||||||
// if CRD's are passed, add these to OpenAPIController
|
// if CRD's are passed, add these to OpenAPIController
|
||||||
if len(crdPaths) > 0 {
|
if len(crdPaths) > 0 {
|
||||||
crds, err := common.ConvertFileToUnstructed(crdPaths)
|
crds, err := common.GetCRDs(crdPaths)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err, "crd is invalid", "file", crdPaths)
|
log.Error(err, "crd is invalid", "file", crdPaths)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue