Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions api/v1/clusterobjectset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,27 @@ const (
ProbeTypeConditionEqual ProbeType = "ConditionEqual"
ProbeTypeFieldsEqual ProbeType = "FieldsEqual"
ProbeTypeFieldValue ProbeType = "FieldValue"
ProbeTypeCEL ProbeType = "CEL"
)

// Assertion is a discriminated union which defines the probe type and definition used as an assertion.
// +union
// +kubebuilder:validation:XValidation:rule="self.type == 'ConditionEqual' ?has(self.conditionEqual) : !has(self.conditionEqual)",message="conditionEqual is required when type is ConditionEqual, and forbidden otherwise"
// +kubebuilder:validation:XValidation:rule="self.type == 'FieldsEqual' ?has(self.fieldsEqual) : !has(self.fieldsEqual)",message="fieldsEqual is required when type is FieldsEqual, and forbidden otherwise"
// +kubebuilder:validation:XValidation:rule="self.type == 'FieldValue' ?has(self.fieldValue) : !has(self.fieldValue)",message="fieldValue is required when type is FieldValue, and forbidden otherwise"
// +kubebuilder:validation:XValidation:rule="self.type == 'CEL' ?has(self.cel) : !has(self.cel)",message="cel is required when type is CEL, and forbidden otherwise"
type Assertion struct {
// type is a required field which specifies the type of probe to use.
//
// The allowed probe types are "ConditionEqual", "FieldsEqual", and "FieldValue".
// The allowed probe types are "ConditionEqual", "FieldsEqual", "FieldValue", and "CEL".
//
// When set to "ConditionEqual", the probe checks objects that have reached a condition of specified type and status.
// When set to "FieldsEqual", the probe checks that the values found at two provided field paths are matching.
// When set to "FieldValue", the probe checks that the value found at the provided field path matches what was specified.
// When set to "CEL", the probe evaluates a boolean CEL rule against the object.
//
// +unionDiscriminator
// +kubebuilder:validation:Enum=ConditionEqual;FieldsEqual;FieldValue
// +kubebuilder:validation:Enum=ConditionEqual;FieldsEqual;FieldValue;CEL
// +required
// <opcon:experimental>
Type ProbeType `json:"type,omitempty"`
Expand All @@ -305,6 +308,60 @@ type Assertion struct {
// +optional
// <opcon:experimental>
FieldValue FieldValueProbe `json:"fieldValue,omitzero"`

// cel contains a boolean CEL rule evaluated against the object's live state.
// A False result reports the configured message; an evaluation error produces
// Unknown and reports the error instead. Compilation and boolean result-type
// checking occur before evaluation, not during CR admission.
// Required when type is "CEL", and forbidden otherwise.
//
// +unionMember
// +optional
// <opcon:experimental>
CEL ProbeCELSpec `json:"cel,omitzero"`
}

// ProbeCELSpec uses Common Expression Language (CEL) to probe an object.
// CEL rules must evaluate to a boolean to be valid. The variable self refers to
// the live Kubernetes resource selected by the enclosing progression probe,
// including its status.
// See:
// https://kubernetes.io/docs/reference/using-api/cel
// https://github.com/google/cel-go
type ProbeCELSpec struct {
// rule is the CEL expression to evaluate. The dynamically typed variable self
// refers to the live Kubernetes resource selected by the enclosing progression
// probe, including its status.
//
// The controller compiles and type-checks rule before evaluation. Its inferred
// result type must be boolean; compilation failures and non-boolean result types
// are invalid configuration. CR admission validates only the configuration's
// structure and string bounds, not CEL syntax or types.
//
// True passes the probe. False fails it and reports message. An evaluation
// error, such as an unguarded missing-field access, produces Unknown and reports
// the error instead of message.
//
// See https://kubernetes.io/docs/reference/using-api/cel and
// https://github.com/google/cel-go.
//
// +kubebuilder:example="self.metadata.name == \"Hans\""
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=1024
// +required
// <opcon:experimental>
Rule string `json:"rule,omitempty"`

// message is the human-readable explanation reported only when rule evaluates
// to False. Evaluation errors produce Unknown and report the error instead.
// Consumers must not parse this message.
//
// +kubebuilder:example="Object must be named Hans"
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=1024
// +required
// <opcon:experimental>
Message string `json:"message,omitempty"`
}

// ConditionEqualProbe defines the condition type and status required for the probe to succeed.
Expand Down
74 changes: 74 additions & 0 deletions api/v1/clusterobjectset_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,80 @@ func TestClusterObjectSetValidity(t *testing.T) {
valid: false,
expectedError: "spec.progressionProbes: Too many",
},
"progressionProbe with GroupKind selector and CEL assertion is valid": {
spec: probeSpec("validity-case-42", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{
{
Type: ProbeTypeCEL,
CEL: ProbeCELSpec{Rule: `self.data.ready == "true"`, Message: "ConfigMap must be ready"},
},
},
}),
valid: true,
},
"progressionProbe CEL assertion requires cel": {
spec: probeSpec("validity-case-43", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{{Type: ProbeTypeCEL}},
}),
valid: false,
expectedError: "cel is required when type is CEL, and forbidden otherwise",
},
"progressionProbe assertion forbids cel when type is not CEL": {
spec: probeSpec("validity-case-44", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{
{
Type: ProbeTypeConditionEqual,
ConditionEqual: ConditionEqualProbe{Type: "Ready", Status: "True"},
CEL: ProbeCELSpec{Rule: "true", Message: "never"},
},
},
}),
valid: false,
expectedError: "cel is required when type is CEL, and forbidden otherwise",
},
"progressionProbe CEL assertion requires rule": {
spec: probeSpec("validity-case-45", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{
{Type: ProbeTypeCEL, CEL: ProbeCELSpec{Message: "ConfigMap must be ready"}},
},
}),
valid: false,
expectedError: "cel.rule: Required value",
},
"progressionProbe CEL assertion requires message": {
spec: probeSpec("validity-case-46", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{
{Type: ProbeTypeCEL, CEL: ProbeCELSpec{Rule: "true"}},
},
}),
valid: false,
expectedError: "cel.message: Required value",
},
"progressionProbe CEL rule must be at most 1024 characters": {
spec: probeSpec("validity-case-47", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{
{Type: ProbeTypeCEL, CEL: ProbeCELSpec{Rule: strings.Repeat("a", 1025), Message: "ConfigMap must be ready"}},
},
}),
valid: false,
expectedError: "cel.rule: Too long",
},
"progressionProbe CEL message must be at most 1024 characters": {
spec: probeSpec("validity-case-48", ProgressionProbe{
Selector: validGroupKindSelector(),
Assertions: []Assertion{
{Type: ProbeTypeCEL, CEL: ProbeCELSpec{Rule: "true", Message: strings.Repeat("a", 1025)}},
},
}),
valid: false,
expectedError: "cel.message: Too long",
},
} {
t.Run(name, func(t *testing.T) {
cos := &ClusterObjectSet{
Expand Down
16 changes: 16 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions applyconfigurations/api/v1/assertion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions applyconfigurations/api/v1/probecelspec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions applyconfigurations/internal/internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions applyconfigurations/utils.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading