1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-27 18:13:17 +00:00
kyverno/pkg/cel/libs/context/mock.go
Vishal Choudhary d812982b2e
feat: webhook handlers for image verification (#12318)
* feat: webhook support for image verification

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* feat: add validation

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: add tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: tests

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: ci

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: codegen

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: trim prefix

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: only use matched policies

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: conflicts

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

* fix: remove commented code

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
2025-03-11 07:38:11 +00:00

68 lines
2 KiB
Go

package context
import (
"github.com/kyverno/kyverno/pkg/globalcontext/store"
"github.com/kyverno/kyverno/pkg/imageverification/imagedataloader"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// MOCK FOR TESTING
type MockCtx struct {
GetConfigMapFunc func(string, string) (*unstructured.Unstructured, error)
GetGlobalReferenceFunc func(string, string) (any, error)
GetImageDataFunc func(string) (*imagedataloader.ImageData, error)
ParseImageReferenceFunc func(string) (imagedataloader.ImageReference, error)
ListResourcesFunc func(string, string, string) (*unstructured.UnstructuredList, error)
GetResourcesFunc 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)
}
func (mock *MockCtx) GetImageData(n string) (*imagedataloader.ImageData, error) {
return mock.GetImageDataFunc(n)
}
func (mock *MockCtx) ParseImageReference(n string) (imagedataloader.ImageReference, error) {
return mock.ParseImageReferenceFunc(n)
}
func (mock *MockCtx) ListResources(apiVersion, resource, namespace string) (*unstructured.UnstructuredList, error) {
return mock.ListResourcesFunc(apiVersion, resource, namespace)
}
func (mock *MockCtx) GetResource(apiVersion, resource, namespace, name string) (*unstructured.Unstructured, error) {
return mock.GetResourcesFunc(apiVersion, resource, namespace, name)
}
type mockGctxStore struct {
data map[string]store.Entry
}
func (m *mockGctxStore) Get(name string) (store.Entry, bool) {
entry, ok := m.data[name]
return entry, ok
}
func (m *mockGctxStore) Set(name string, data store.Entry) {
if m.data == nil {
m.data = make(map[string]store.Entry)
}
m.data[name] = data
}
type mockEntry struct {
data any
err error
}
func (m *mockEntry) Get(_ string) (any, error) {
return m.data, m.err
}
func (m *mockEntry) Stop() {}