2019-06-17 18:11:22 -07:00
|
|
|
package testrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-01-24 09:37:12 -08:00
|
|
|
projectPath = "src/github.com/nirmata/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 ""
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//ConvertToUnstructured converts a resource to unstructured format
|
2019-08-21 12:03:53 -07:00
|
|
|
func ConvertToUnstructured(data []byte) (*unstructured.Unstructured, error) {
|
|
|
|
resource := &unstructured.Unstructured{}
|
|
|
|
err := resource.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("failed to unmarshall resource: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resource, nil
|
|
|
|
}
|