1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-21 07:12:42 +00:00

feat: simplify resource cel lib (#12426)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2025-03-17 17:31:01 +01:00 committed by GitHub
parent e785ee4882
commit 6c8446b831
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 2 additions and 40 deletions

View file

@ -11,20 +11,7 @@ type impl struct {
}
func (c *impl) get_configmap_string_string(args ...ref.Val) ref.Val {
if self, err := utils.ConvertToNative[Context](args[0]); err != nil {
return types.WrapErr(err)
} else if namespace, err := utils.ConvertToNative[string](args[1]); err != nil {
return types.WrapErr(err)
} else if name, err := utils.ConvertToNative[string](args[2]); err != nil {
return types.WrapErr(err)
} else {
cm, err := self.GetConfigMap(namespace, name)
if err != nil {
// Errors are not expected here since Parse is a more lenient parser than ParseRequestURI.
return types.NewErr("failed to get configmap: %v", err)
}
return c.NativeToValue(cm.UnstructuredContent())
}
return c.get_resource_string_string_string_string(args[0], types.String("v1"), types.String("configmaps"), args[1], args[2])
}
func (c *impl) get_imagedata_string(ctx ref.Val, image ref.Val) ref.Val {

View file

@ -32,7 +32,7 @@ func Test_impl_get_configmap_string_string(t *testing.T) {
called := false
data := map[string]any{
"resource": Context{&MockCtx{
GetConfigMapFunc: func(string, string) (*unstructured.Unstructured, error) {
GetResourceFunc: func(string, string, string, string) (*unstructured.Unstructured, error) {
called = true
return &unstructured.Unstructured{}, nil
},

View file

@ -7,17 +7,12 @@ import (
// MOCK FOR TESTING
type MockCtx struct {
GetConfigMapFunc func(string, string) (*unstructured.Unstructured, error)
GetGlobalReferenceFunc func(string, string) (any, error)
GetImageDataFunc func(string) (map[string]interface{}, error)
ListResourcesFunc func(string, string, string) (*unstructured.UnstructuredList, error)
GetResourceFunc func(string, string, string, string) (*unstructured.Unstructured, error)
}
func (mock *MockCtx) GetConfigMap(ns string, n string) (*unstructured.Unstructured, error) {
return mock.GetConfigMapFunc(ns, n)
}
func (mock *MockCtx) GetGlobalReference(n, p string) (any, error) {
return mock.GetGlobalReferenceFunc(n, p)
}

View file

@ -13,7 +13,6 @@ var (
)
type ContextInterface interface {
GetConfigMap(string, string) (*unstructured.Unstructured, error)
GetImageData(string) (map[string]interface{}, error)
ListResources(apiVersion, resource, namespace string) (*unstructured.UnstructuredList, error)
GetResource(apiVersion, resource, namespace, name string) (*unstructured.Unstructured, error)

View file

@ -15,13 +15,11 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)
type Context = ContextInterface
type contextProvider struct {
client kubernetes.Interface
dclient dynamic.Interface
imagedata imagedataloader.Fetcher
gctxStore gctxstore.Store
@ -37,25 +35,12 @@ func NewContextProvider(
return nil, err
}
return &contextProvider{
client: client.GetKubeClient(),
dclient: client.GetDynamicInterface(),
imagedata: idl,
gctxStore: gctxStore,
}, nil
}
func (cp *contextProvider) GetConfigMap(namespace string, name string) (*unstructured.Unstructured, error) {
cm, err := cp.client.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}
out, err := kubeutils.ObjToUnstructured(cm)
if err != nil {
return nil, err
}
return out, nil
}
func (cp *contextProvider) GetGlobalReference(name, projection string) (any, error) {
ent, ok := cp.gctxStore.Get(name)
if !ok {

View file

@ -39,10 +39,6 @@ func (cp *FakeContextProvider) AddResource(gvr schema.GroupVersionResource, obj
return nil
}
func (cp *FakeContextProvider) GetConfigMap(ns, n string) (*unstructured.Unstructured, error) {
return cp.GetResource("v1", "configmaps", ns, n)
}
func (cp *FakeContextProvider) GetGlobalReference(string, string) (any, error) {
panic("not implemented")
}