Skip to content

Commit f890392

Browse files
committed
chore: Allow linking condition to states
1 parent 60a2f8e commit f890392

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

config/endpoint/condition.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
type Condition string
2525

2626
// Validate checks if the Condition is valid
27-
func (c Condition) Validate() error {
27+
func (c Condition) Validate() error { // TODO#227 Validate conditions with linked states have valid states
2828
r := &Result{}
2929
c.evaluate(r, false, nil)
3030
if len(r.Errors) != 0 {
@@ -37,6 +37,18 @@ func (c Condition) Validate() error {
3737
func (c Condition) evaluate(result *Result, dontResolveFailedConditions bool, context *gontext.Gontext) bool {
3838
condition := string(c)
3939
success := false
40+
41+
var linkedState string
42+
if strings.Contains(condition, "::") {
43+
conditionParts := strings.Split(condition, "::")
44+
if len(conditionParts) != 2 { // TODO#227 Not sure if this makes sense. Checking that it is 2 or more should be enough. Then there is no character restriction in the remaining condition.
45+
result.AddError(fmt.Sprintf("invalid linked state syntax: %s", condition))
46+
return false
47+
}
48+
linkedState = conditionParts[0]
49+
condition = conditionParts[1]
50+
}
51+
4052
conditionToDisplay := condition
4153
if strings.Contains(condition, " == ") {
4254
parameters, resolvedParameters := sanitizeAndResolveWithContext(strings.Split(condition, " == "), result, context)
@@ -81,7 +93,7 @@ func (c Condition) evaluate(result *Result, dontResolveFailedConditions bool, co
8193
if !success {
8294
//logr.Debugf("[Condition.evaluate] Condition '%s' did not succeed because '%s' is false", condition, condition)
8395
}
84-
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
96+
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success, LinkedState: linkedState})
8597
return success
8698
}
8799

config/endpoint/condition_result.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ type ConditionResult struct {
77

88
// Success whether the condition was met (successful) or not (failed)
99
Success bool `json:"success"`
10+
11+
// The state the condition is linked to
12+
LinkedState string `json:"-"`
1013
}

config/endpoint/endpoint.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var (
6868
ErrUnknownEndpointType = errors.New("unknown endpoint type")
6969

7070
// ErrInvalidConditionFormat is the error with which Gatus will panic if a condition has an invalid format
71-
ErrInvalidConditionFormat = errors.New("invalid condition format: does not match '<VALUE> <COMPARATOR> <VALUE>'")
71+
ErrInvalidConditionFormat = errors.New("invalid condition format: does not match '[STATE::]<VALUE> <COMPARATOR> <VALUE>'")
7272

7373
// ErrInvalidEndpointIntervalForDomainExpirationPlaceholder is the error with which Gatus will panic if an endpoint
7474
// has both an interval smaller than 5 minutes and a condition with DomainExpirationPlaceholder.
@@ -338,10 +338,18 @@ func (e *Endpoint) EvaluateHealthWithContext(context *gontext.Gontext) *Result {
338338
result.Success = false
339339
}
340340
}
341-
if result.Success { // TODO#227 Make dynymic and also check conditions e.g. method somewhere at the end that evaluates the state (conditions and condition results + is maintenance needs to be available)
341+
if result.Success {
342342
result.State = state.DefaultHealthyStateName
343-
} else {
344-
result.State = state.DefaultUnhealthyStateName
343+
} else { // Go over condition results to see if any of them has a specific state to set
344+
for _, conditionResult := range result.ConditionResults {
345+
if !conditionResult.Success && len(conditionResult.LinkedState) > 0 {
346+
result.State = conditionResult.LinkedState // TODO#227 Only set if no other state with h
347+
break
348+
}
349+
}
350+
if len(result.State) == 0 {
351+
result.State = state.DefaultUnhealthyStateName
352+
}
345353
}
346354
result.Timestamp = time.Now()
347355
// Clean up parameters that we don't need to keep in the results

0 commit comments

Comments
 (0)