1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/testrunner/utils.go

57 lines
1.2 KiB
Go
Raw Normal View History

2019-06-17 18:11:22 -07:00
package testrunner
import (
"io/ioutil"
"os"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2020-03-17 16:25:34 -07:00
"sigs.k8s.io/controller-runtime/pkg/log"
2019-06-17 18:11:22 -07:00
)
var (
2020-10-07 15:09:52 -07:00
projectPath = envOr("PROJECT_PATH", "src/github.com/kyverno/kyverno")
2019-06-17 18:11:22 -07:00
)
// LoadFile loads file in byte buffer
func LoadFile(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}
return ioutil.ReadFile(path)
}
var kindToResource = map[string]string{
"ConfigMap": "configmaps",
"Endpoints": "endpoints",
"Namespace": "namespaces",
"Secret": "secrets",
2019-10-08 10:57:24 -07:00
"Service": "services",
2019-06-17 18:11:22 -07:00
"Deployment": "deployments",
"NetworkPolicy": "networkpolicies",
}
func getResourceFromKind(kind string) string {
if resource, ok := kindToResource[kind]; ok {
return resource
}
return ""
}
//ConvertToUnstructured converts a resource to unstructured format
func ConvertToUnstructured(data []byte) (*unstructured.Unstructured, error) {
resource := &unstructured.Unstructured{}
err := resource.UnmarshalJSON(data)
if err != nil {
2020-03-17 16:25:34 -07:00
log.Log.Error(err, "failed to unmarshal resource")
return nil, err
}
return resource, nil
}
func envOr(name, def string) string {
if v, ok := os.LookupEnv(name); ok {
return v
}
return def
}