1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/cel/policy/context.go
Vishal Choudhary 7d8ed212a4
feat: create image data loader (#12036)
* feat: add image data loader to context

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

* fix: build

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

* fix: linter

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

* feat: tests

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

* fix: update types

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

* feat: replace crane with remote

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

* fix: linter

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

* fix: linter

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

---------

Signed-off-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
Co-authored-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2025-02-03 13:42:40 +00:00

52 lines
1.5 KiB
Go

package policy
import (
"context"
contextlib "github.com/kyverno/kyverno/pkg/cel/libs/context"
"github.com/kyverno/kyverno/pkg/config"
"github.com/kyverno/kyverno/pkg/imagedataloader"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes"
)
type Context = contextlib.ContextInterface
type contextProvider struct {
client kubernetes.Interface
imagedata imagedataloader.Fetcher
}
func NewContextProvider(client kubernetes.Interface, imageOpts []imagedataloader.Option) (Context, error) {
idl, err := imagedataloader.New(client.CoreV1().Secrets(config.KyvernoNamespace()), imageOpts...)
if err != nil {
return nil, err
}
return &contextProvider{
client: client,
imagedata: idl,
}, 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 unstructured.Unstructured{}, err
}
out, err := kubeutils.ObjToUnstructured(cm)
if err != nil {
return unstructured.Unstructured{}, err
}
return *out, nil
}
func (cp *contextProvider) GetGlobalReference(string) (any, error) {
return nil, nil
}
func (cp *contextProvider) GetImageData(image string) (*imagedataloader.ImageData, error) {
// TODO: get image credentials from image verification policies?
return cp.imagedata.FetchImageData(context.TODO(), image)
}