mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-07 00:17:13 +00:00
* feat: add kyverno vap API Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * add context lib Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * codegen Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: Vishal Choudhary <vishal.choudhary@nirmata.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package context
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/google/cel-go/cel"
|
|
"github.com/google/cel-go/common/types"
|
|
"github.com/google/cel-go/ext"
|
|
"k8s.io/client-go/kubernetes"
|
|
)
|
|
|
|
// lib types
|
|
var ConfigMapReferenceType = types.NewObjectType("context.ConfigMapReference")
|
|
|
|
type lib struct {
|
|
client kubernetes.Interface
|
|
}
|
|
|
|
func Lib(client kubernetes.Interface) cel.EnvOption {
|
|
// create the cel lib env option
|
|
return cel.Lib(&lib{
|
|
client: client,
|
|
})
|
|
}
|
|
|
|
func (c *lib) CompileOptions() []cel.EnvOption {
|
|
return []cel.EnvOption{
|
|
ext.NativeTypes(
|
|
// TODO: needs cel lib bump
|
|
// ext.ParseStructTags(true),
|
|
reflect.TypeFor[ConfigMapReference](),
|
|
),
|
|
// extend environment with function overloads
|
|
c.extendEnv,
|
|
}
|
|
}
|
|
|
|
func (*lib) ProgramOptions() []cel.ProgramOption {
|
|
return []cel.ProgramOption{}
|
|
}
|
|
|
|
func (c *lib) extendEnv(env *cel.Env) (*cel.Env, error) {
|
|
// create implementation, recording the envoy types aware adapter
|
|
impl := impl{
|
|
Adapter: env.CELTypeAdapter(),
|
|
client: c.client,
|
|
}
|
|
// build our function overloads
|
|
libraryDecls := map[string][]cel.FunctionOpt{
|
|
"context.configMap": {
|
|
// TODO: add more overloads...
|
|
cel.Overload("context_get_cm", []*cel.Type{ConfigMapReferenceType}, types.DynType, cel.UnaryBinding(impl.context_get_cm)),
|
|
},
|
|
}
|
|
// create env options corresponding to our function overloads
|
|
options := []cel.EnvOption{}
|
|
for name, overloads := range libraryDecls {
|
|
options = append(options, cel.Function(name, overloads...))
|
|
}
|
|
// extend environment with our function overloads
|
|
return env.Extend(options...)
|
|
}
|