mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-07 00:17:13 +00:00
* feat: add context cel lib to get config map Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * function name Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix type Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package context
|
|
|
|
import (
|
|
"github.com/google/cel-go/cel"
|
|
"github.com/google/cel-go/common/types"
|
|
)
|
|
|
|
type lib struct{}
|
|
|
|
func Lib() cel.EnvOption {
|
|
// create the cel lib env option
|
|
return cel.Lib(&lib{})
|
|
}
|
|
|
|
func (*lib) LibraryName() string {
|
|
return "kyverno.context"
|
|
}
|
|
|
|
func (c *lib) CompileOptions() []cel.EnvOption {
|
|
return []cel.EnvOption{
|
|
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(),
|
|
}
|
|
// build our function overloads
|
|
libraryDecls := map[string][]cel.FunctionOpt{
|
|
"GetConfigMap": {
|
|
// TODO: should not use DynType in return
|
|
cel.MemberOverload("get_configmap_string_string", []*cel.Type{ContextType, types.StringType, types.StringType}, types.DynType, cel.FunctionBinding(impl.get_configmap_string_string)),
|
|
},
|
|
}
|
|
// 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...)
|
|
}
|