mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
* feat: context function to request resources from api server Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de> * add chainsaw test Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de> * replace dclient with dynamic client Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de> * update test case Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de> --------- Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de> Co-authored-by: shuting <shuting@nirmata.com>
41 lines
1 KiB
Go
41 lines
1 KiB
Go
package utils
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/google/cel-go/common/types/ref"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
)
|
|
|
|
func ConvertToNative[T any](value ref.Val) (T, error) {
|
|
// try to convert value to native type
|
|
response, err := value.ConvertToNative(reflect.TypeFor[T]())
|
|
// if it failed return default value for T and error
|
|
if err != nil {
|
|
var t T
|
|
return t, err
|
|
}
|
|
// return the converted value
|
|
return response.(T), nil
|
|
}
|
|
|
|
func ConvertObjectToUnstructured(obj any) (*unstructured.Unstructured, error) {
|
|
if obj == nil || reflect.ValueOf(obj).IsNil() {
|
|
return &unstructured.Unstructured{Object: nil}, nil
|
|
}
|
|
ret, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &unstructured.Unstructured{Object: ret}, nil
|
|
}
|
|
|
|
func ConvertBytesToUnstructuredList(b []byte) (*unstructured.UnstructuredList, error) {
|
|
list := &unstructured.UnstructuredList{}
|
|
err := list.UnmarshalJSON(b)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|