cel

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TypeNamePrefix is the prefix used for CEL type names when converting OpenAPI schemas.
	// Used to namespace custom types and avoid conflicts with built-in CEL types.
	// Example: "__type_schema.spec.ports" for a ports field in the schema resource.
	TypeNamePrefix = "__type_"
)
View Source
const XKubernetesPreserveUnknownFields = "x-kubernetes-preserve-unknown-fields"

XKubernetesPreserveUnknownFields is the key for the named open api extension, declaration type metadata field and validator rule metadata field that indicates whether unknown fields should be preserved in the deserialized object. KRO effectively translates these fields into CEL Dyn types.

Variables

View Source
var (
	// ErrUnsupportedType is returned when the type is not supported.
	ErrUnsupportedType = errors.New("unsupported type")
)

Functions

func AreTypesStructurallyCompatible added in v0.6.2

func AreTypesStructurallyCompatible(output, expected *cel.Type, provider *DeclTypeProvider) (bool, error)

AreTypesStructurallyCompatible checks if an output type from an expected or executed CEL expression is compatible with an expected type.

This performs deep structural comparison: - For primitives: checks kind equality - For lists: recursively checks element type compatibility - For maps: recursively checks key and value type compatibility - For structs: uses DeclTypeProvider to introspect fields and check all required fields exist with compatible types - For map → struct and struct → map compatibility if fields/keys are structurally compatible

The provider is required for introspecting struct field information. Returns true if types are compatible, false otherwise. If false, the error describes why.

func DefaultEnvironment

func DefaultEnvironment(options ...EnvOption) (*cel.Env, error)

DefaultEnvironment returns the default CEL environment.

func FieldTypeMap added in v0.6.2

func FieldTypeMap(path string, t *apiservercel.DeclType) map[string]*apiservercel.DeclType

FieldTypeMap constructs a map of the field and object types nested within a given type.

func GoNativeType

func GoNativeType(v ref.Val) (interface{}, error)

GoNativeType transforms CEL output into corresponding Go types

func IsBoolOrOptionalBool added in v0.6.0

func IsBoolOrOptionalBool(t *cel.Type) bool

IsBoolOrOptionalBool checks if a CEL type is bool or optional_type(bool). This is useful for validating condition expressions that must return boolean values.

func IsBoolType

func IsBoolType(v ref.Val) bool

IsBoolType checks if the given ref.Val is of type BoolType

func SchemaDeclTypeWithMetadata added in v0.6.2

func SchemaDeclTypeWithMetadata(s common.Schema, isResourceRoot bool) *apiservercel.DeclType

SchemaDeclTypeWithMetadata converts the structural schema to a CEL declaration, or returns nil if the structural schema should not be exposed in CEL expressions. Set isResourceRoot to true for the root of a custom resource or embedded resource.

Schemas with XPreserveUnknownFields not exposed unless they are objects. Array and "maps" schemas are not exposed if their items or additionalProperties schemas are not exposed. Object Properties are not exposed if their schema is not exposed.

The CEL declaration for objects with XPreserveUnknownFields does not expose unknown fields.

This functions acts like [SchemaDeclType] from k8s, but with additional logic to add unknown fields as a metadata field. This can then be used by a DeclTypeProvider to expose unknown fields in CEL expressions on demand without compromising type integrity of other types.

nolint:gocyclo // this function should stay as close to upstream k8s as possible to allow comparisons

func TypedEnvironment added in v0.6.0

func TypedEnvironment(schemas map[string]*spec.Schema) (*cel.Env, error)

TypedEnvironment creates a CEL environment with type checking enabled.

This should be used during RGD build time (pkg/graph.Builder) to validate CEL expressions against OpenAPI schemas.

func UntypedEnvironment added in v0.6.0

func UntypedEnvironment(resourceIDs []string) (*cel.Env, error)

UntypedEnvironment creates a CEL environment without type declarations.

This is theoretically cheaper to use as there are no Schema conversions required. NOTE(a-hilaly): maybe use this for runtime? undecided.

func WouldMatchIfUnwrapped added in v0.6.0

func WouldMatchIfUnwrapped(outputType, expectedType *cel.Type) bool

WouldMatchIfUnwrapped checks if outputType would be assignable to expectedType if we unwrapped the optional wrapper from outputType. This detects the case where outputType is optional_type(T) and expectedType is T.

Types

type DeclTypeProvider added in v0.6.2

type DeclTypeProvider struct {
	// contains filtered or unexported fields
}

DeclTypeProvider extends the CEL ref.TypeProvider interface and provides an Open API Schema-based type-system.

func CreateDeclTypeProvider added in v0.6.0

func CreateDeclTypeProvider(schemas map[string]*spec.Schema) *DeclTypeProvider

CreateDeclTypeProvider creates a DeclTypeProvider from OpenAPI schemas. This is used for deep introspection of type structures when generating schemas. The provider maps CEL type names to their full DeclType definitions with all fields.

func NewDeclTypeProvider added in v0.6.2

func NewDeclTypeProvider(rootTypes ...*apiservercel.DeclType) *DeclTypeProvider

NewDeclTypeProvider returns an Open API Schema-based type-system which is CEL compatible.

func (*DeclTypeProvider) EnumValue added in v0.6.2

func (rt *DeclTypeProvider) EnumValue(enumName string) ref.Val

func (*DeclTypeProvider) EnvOptions added in v0.6.2

func (rt *DeclTypeProvider) EnvOptions(tp types.Provider) ([]cel.EnvOption, error)

EnvOptions returns a set of cel.EnvOption values which includes the declaration set as well as a custom ref.TypeProvider.

If the DeclTypeProvider value is nil, an empty []cel.EnvOption set is returned.

func (*DeclTypeProvider) FindDeclType added in v0.6.2

func (rt *DeclTypeProvider) FindDeclType(typeName string) (*apiservercel.DeclType, bool)

FindDeclType returns the CPT type description which can be mapped to a CEL type.

func (*DeclTypeProvider) FindIdent added in v0.6.2

func (rt *DeclTypeProvider) FindIdent(identName string) (ref.Val, bool)

func (*DeclTypeProvider) FindStructFieldNames added in v0.6.2

func (rt *DeclTypeProvider) FindStructFieldNames(typeName string) ([]string, bool)

FindStructFieldNames returns the field names associated with the type, if the type is found.

func (*DeclTypeProvider) FindStructFieldType added in v0.6.2

func (rt *DeclTypeProvider) FindStructFieldType(typeName, fieldName string) (*types.FieldType, bool)

FindStructFieldType returns a field type given a type name and field name, if found.

Note, the type name for an Open API Schema type is likely to be its qualified object path. If, in the future an object instance rather than a type name were provided, the field resolution might more accurately reflect the expected type model. However, in this case concessions were made to align with the existing CEL interfaces.

func (*DeclTypeProvider) FindStructType added in v0.6.2

func (rt *DeclTypeProvider) FindStructType(typeName string) (*types.Type, bool)

FindStructType attempts to resolve the typeName provided from the rule's rule-schema, or if not from the embedded ref.TypeProvider.

FindStructType overrides the default type-finding behavior of the embedded TypeProvider.

Note, when the type name is based on the Open API Schema, the name will reflect the object path where the type definition appears.

func (*DeclTypeProvider) NativeToValue added in v0.6.2

func (rt *DeclTypeProvider) NativeToValue(val interface{}) ref.Val

NativeToValue is an implementation of the ref.TypeAdapter interface which supports conversion of rule values to CEL ref.Val instances.

func (*DeclTypeProvider) NewValue added in v0.6.2

func (rt *DeclTypeProvider) NewValue(typeName string, fields map[string]ref.Val) ref.Val

func (*DeclTypeProvider) SetRecognizeKeywordAsFieldName added in v0.6.2

func (rt *DeclTypeProvider) SetRecognizeKeywordAsFieldName(recognize bool)

func (*DeclTypeProvider) TypeNames added in v0.6.2

func (rt *DeclTypeProvider) TypeNames() []string

TypeNames returns the list of type names declared within the DeclTypeProvider object.

func (*DeclTypeProvider) WithTypeProvider added in v0.6.2

func (rt *DeclTypeProvider) WithTypeProvider(tp types.Provider) (*DeclTypeProvider, error)

WithTypeProvider returns a new DeclTypeProvider that sets the given TypeProvider If the original DeclTypeProvider is nil, the returned DeclTypeProvider is still nil.

type EnvOption

type EnvOption func(*envOptions)

EnvOption is a function that modifies the environment options.

func WithCustomDeclarations

func WithCustomDeclarations(declarations []cel.EnvOption) EnvOption

WithCustomDeclarations adds custom declarations to the CEL environment.

func WithResourceIDs

func WithResourceIDs(ids []string) EnvOption

WithResourceIDs adds resource ids that will be declared as CEL variables.

func WithTypedResources added in v0.6.0

func WithTypedResources(schemas map[string]*spec.Schema) EnvOption

WithTypedResources adds typed resource declarations to the CEL environment. This enables compile time type checking for field access in CEL expressions.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL