Skip to content

Commit 4d8fc30

Browse files
committed
Don't update condition if status stays False/Unknown for custom plugin
1 parent 599ca53 commit 4d8fc30

File tree

1 file changed

+54
-53
lines changed

1 file changed

+54
-53
lines changed

pkg/custompluginmonitor/custom_plugin_monitor.go

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -156,77 +156,78 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
156156
})
157157
}
158158
} else {
159-
// For permanent error changes the condition
159+
// For permanent error that changes the condition
160160
for i := range c.conditions {
161161
condition := &c.conditions[i]
162162
if condition.Type == result.Rule.Condition {
163-
status := toConditionStatus(result.ExitStatus)
164-
// change 1: Condition status change from True to False/Unknown
165-
if condition.Status == types.True && status != types.True {
166-
condition.Transition = timestamp
167-
var defaultConditionReason string
168-
var defaultConditionMessage string
169-
for j := range c.config.DefaultConditions {
170-
defaultCondition := &c.config.DefaultConditions[j]
171-
if defaultCondition.Type == result.Rule.Condition {
172-
defaultConditionReason = defaultCondition.Reason
173-
defaultConditionMessage = defaultCondition.Message
174-
break
175-
}
163+
// The condition reason specified in the rule and the result message
164+
// represent the problem happened. We need to know the default condition
165+
// from the config, so that we can set the new condition reason/message
166+
// back when such problem goes away.
167+
var defaultConditionReason string
168+
var defaultConditionMessage string
169+
for j := range c.config.DefaultConditions {
170+
defaultCondition := &c.config.DefaultConditions[j]
171+
if defaultCondition.Type == result.Rule.Condition {
172+
defaultConditionReason = defaultCondition.Reason
173+
defaultConditionMessage = defaultCondition.Message
174+
break
176175
}
176+
}
177177

178-
inactiveProblemEvents = append(inactiveProblemEvents, util.GenerateConditionChangeEvent(
179-
condition.Type,
180-
status,
181-
defaultConditionReason,
182-
timestamp,
183-
))
178+
// For condition reason, set back to default if there is no problem. But
179+
// always honor the result message if possible, which can be explicitly
180+
// sent back by the plugin.
181+
var newReason string
182+
newMessage := result.Message
184183

185-
condition.Status = status
186-
condition.Message = defaultConditionMessage
187-
condition.Reason = defaultConditionReason
184+
needToUpdateCondition := true
185+
status := toConditionStatus(result.ExitStatus)
186+
if condition.Status == types.True && status != types.True {
187+
// Scenario 1: Condition status changes from True to False/Unknown
188+
newReason = defaultConditionReason
189+
if newMessage == "" {
190+
newMessage = defaultConditionMessage
191+
}
188192
} else if condition.Status != types.True && status == types.True {
189-
// change 2: Condition status change from False/Unknown to True
190-
condition.Transition = timestamp
191-
condition.Message = result.Message
192-
activeProblemEvents = append(activeProblemEvents, util.GenerateConditionChangeEvent(
193-
condition.Type,
194-
status,
195-
result.Rule.Reason,
196-
timestamp,
197-
))
198-
199-
condition.Status = status
200-
condition.Reason = result.Rule.Reason
193+
// Scenario 2: Condition status changes from False/Unknown to True
194+
newReason = result.Rule.Reason
195+
newMessage = result.Message
201196
} else if condition.Status != status {
202-
// change 3: Condition status change from False to Unknown or vice versa
203-
condition.Transition = timestamp
204-
condition.Message = result.Message
205-
inactiveProblemEvents = append(inactiveProblemEvents, util.GenerateConditionChangeEvent(
206-
condition.Type,
207-
status,
208-
result.Rule.Reason,
209-
timestamp,
210-
))
211-
212-
condition.Status = status
213-
condition.Reason = result.Rule.Reason
214-
} else if condition.Status == status &&
197+
// Scenario 3: Condition status changes from False to Unknown or vice versa
198+
newReason = defaultConditionReason
199+
if newMessage == "" {
200+
newMessage = defaultConditionMessage
201+
}
202+
} else if condition.Status == types.True && status == types.True &&
215203
(condition.Reason != result.Rule.Reason ||
216204
(*c.config.PluginGlobalConfig.EnableMessageChangeBasedConditionUpdate && condition.Message != result.Message)) {
217-
// change 4: Condition status do not change.
205+
// Scenario 4: Condition status does not change and it stays true.
218206
// condition reason changes or
219207
// condition message changes when message based condition update is enabled.
208+
newReason = result.Rule.Reason
209+
newMessage = result.Message
210+
} else {
211+
// Scenario 5: Condition status does not change and it stays False/Unknown.
212+
// This should just be the default reason or message (as a consequence
213+
// of scenario 1 and scenario 3 above).
214+
needToUpdateCondition = false
215+
}
216+
217+
if needToUpdateCondition {
220218
condition.Transition = timestamp
221-
condition.Reason = result.Rule.Reason
222-
condition.Message = result.Message
219+
condition.Status = status
220+
condition.Reason = newReason
221+
condition.Message = newMessage
222+
223223
updateEvent := util.GenerateConditionChangeEvent(
224224
condition.Type,
225225
status,
226-
condition.Reason,
226+
newReason,
227227
timestamp,
228228
)
229-
if condition.Status == types.True {
229+
230+
if status == types.True {
230231
activeProblemEvents = append(activeProblemEvents, updateEvent)
231232
} else {
232233
inactiveProblemEvents = append(inactiveProblemEvents, updateEvent)

0 commit comments

Comments
 (0)