2019-06-17 18:11:22 -07:00
|
|
|
package testrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-10-13 10:48:45 -07:00
|
|
|
"path/filepath"
|
2019-06-17 18:11:22 -07:00
|
|
|
|
2022-10-02 20:45:03 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2019-06-17 18:11:22 -07:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoadFile loads file in byte buffer
|
|
|
|
func LoadFile(path string) ([]byte, error) {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-13 10:48:45 -07:00
|
|
|
path = filepath.Clean(path)
|
2021-10-14 00:05:13 +02:00
|
|
|
// We accept the risk of including a user provided file here.
|
2022-09-30 15:25:19 +08:00
|
|
|
return os.ReadFile(path) // #nosec G304
|
2019-06-17 18:11:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02: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 {
|
2022-10-02 20:45:03 +01:00
|
|
|
logging.Error(err, "failed to unmarshal resource")
|
2019-08-21 12:03:53 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resource, nil
|
|
|
|
}
|