-
Notifications
You must be signed in to change notification settings - Fork 12
HYPERFLEET-537 - feat: refactor condition_rules to use CEL expressions #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,22 @@ resource_type: clusters | |
| # Accepts Go duration format: ns, us/µs, ms, s, m, h. | ||
| poll_interval: 5s | ||
|
|
||
| # Max age interval for resources that are not ready. | ||
| # Resources in transitional states are re-checked more frequently. | ||
| max_age_not_ready: 10s | ||
|
|
||
| # Max age interval for resources that are ready and stable. | ||
| # Stable resources are checked less frequently to reduce API load. | ||
| max_age_ready: 30m | ||
| # Condition rules for decision making. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't found how are condition combined, is it AND or OR? |
||
| # Uses CEL expressions for flexible condition matching. | ||
| # | ||
| # Available CEL helper functions: | ||
| # condition(resource, type) → map - returns full condition map | ||
| # status(resource, type) → string - returns condition status | ||
| # conditionTime(resource, type) → string - returns last_updated_time (RFC3339) | ||
| conditions: | ||
| reference_time: 'conditionTime(resource, "Ready")' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is not clear to me what
In my mind, the CEL engine could be more generic.
One idea, but I think we should brainstorm this:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My proposal: trigger_decission:
- name: max_age
expression: 30m
- name: ready
expression: resource.conditions.filter(c, c.type=="Ready")[0]
- name: ref_time
expression: has(ready.last_updated_time )
? ready.last_updated_time
: ready.created_time
- name: operand1
expression: ready != "True"
result:
expression: operand1 AND operand2 AND (operand3 OR operand4)
reason:
expression: xxxxOR message_decision:
params:
max_age: 30m
ready: resource.conditions.filter(c, c.type=="Ready")[0]
ref_time: has(ready.last_updated_time )
? ready.last_updated_time
: ready.created_time
operand1: ready != "True"
result: operand1 AND operand2 AND (operand3 OR operand4) |
||
| rules: | ||
| - name: isReady | ||
| expression: 'status(resource, "Ready") == "True"' | ||
| max_age: 30m # Stable resources checked less frequently | ||
| - name: isNotReady | ||
| expression: 'status(resource, "Ready") == "False"' | ||
| max_age: 10s # Transitional resources re-checked more frequently | ||
|
|
||
| # Resource selector (optional) - filter resources by labels. | ||
| # If empty or omitted, all resources of the specified type are watched. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,24 @@ type LabelSelector struct { | |
| // LabelSelectorList is a list of label selectors | ||
| type LabelSelectorList []LabelSelector | ||
|
|
||
| // ConditionRule defines a CEL expression that, when matched, determines the max age for a resource | ||
| type ConditionRule struct { | ||
| Name string `mapstructure:"name"` | ||
| Expression string `mapstructure:"expression"` | ||
| MaxAge time.Duration `mapstructure:"max_age"` | ||
| } | ||
|
|
||
| // Conditions configures CEL-based condition evaluation for the decision engine | ||
| type Conditions struct { | ||
| ReferenceTime string `mapstructure:"reference_time"` | ||
| Rules []ConditionRule `mapstructure:"rules"` | ||
| } | ||
|
|
||
| // SentinelConfig represents the Sentinel configuration | ||
| type SentinelConfig struct { | ||
| ResourceType string `mapstructure:"resource_type"` | ||
| PollInterval time.Duration `mapstructure:"poll_interval"` | ||
| MaxAgeNotReady time.Duration `mapstructure:"max_age_not_ready"` | ||
| MaxAgeReady time.Duration `mapstructure:"max_age_ready"` | ||
| Conditions Conditions `mapstructure:"conditions"` | ||
| ResourceSelector LabelSelectorList `mapstructure:"resource_selector"` | ||
| HyperFleetAPI *HyperFleetAPIConfig `mapstructure:"hyperfleet_api"` | ||
| MessageData map[string]interface{} `mapstructure:"message_data"` | ||
|
|
@@ -57,9 +69,14 @@ func (ls LabelSelectorList) ToMap() map[string]string { | |
| func NewSentinelConfig() *SentinelConfig { | ||
| return &SentinelConfig{ | ||
| // ResourceType is required and must be set in config file | ||
| PollInterval: 5 * time.Second, | ||
| MaxAgeNotReady: 10 * time.Second, | ||
| MaxAgeReady: 30 * time.Minute, | ||
| PollInterval: 5 * time.Second, | ||
| Conditions: Conditions{ | ||
| ReferenceTime: `conditionTime(resource, "Ready")`, | ||
| Rules: []ConditionRule{ | ||
| {Name: "isReady", Expression: `status(resource, "Ready") == "True"`, MaxAge: 30 * time.Minute}, | ||
| {Name: "isNotReady", Expression: `status(resource, "Ready") == "False"`, MaxAge: 10 * time.Second}, | ||
| }, | ||
| }, | ||
| ResourceSelector: []LabelSelector{}, // Empty means watch all resources | ||
| HyperFleetAPI: &HyperFleetAPIConfig{ | ||
| // Endpoint is required and must be set in config file | ||
|
|
@@ -138,12 +155,24 @@ func (c *SentinelConfig) Validate() error { | |
| return fmt.Errorf("poll_interval must be positive") | ||
| } | ||
|
|
||
| if c.MaxAgeNotReady <= 0 { | ||
| return fmt.Errorf("max_age_not_ready must be positive") | ||
| if strings.TrimSpace(c.Conditions.ReferenceTime) == "" { | ||
| return fmt.Errorf("conditions.reference_time is required") | ||
| } | ||
|
|
||
| if c.MaxAgeReady <= 0 { | ||
| return fmt.Errorf("max_age_ready must be positive") | ||
| if len(c.Conditions.Rules) == 0 { | ||
| return fmt.Errorf("conditions.rules must have at least one rule") | ||
| } | ||
|
|
||
| for i, rule := range c.Conditions.Rules { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed — neither |
||
| if strings.TrimSpace(rule.Name) == "" { | ||
| return fmt.Errorf("conditions.rules[%d].name is required", i) | ||
| } | ||
| if strings.TrimSpace(rule.Expression) == "" { | ||
| return fmt.Errorf("conditions.rules[%d].expression is required", i) | ||
| } | ||
| if rule.MaxAge <= 0 { | ||
| return fmt.Errorf("conditions.rules[%d].max_age must be positive", i) | ||
| } | ||
| } | ||
|
|
||
| if c.MessageData == nil { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.