@@ -156,77 +156,78 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
156
156
})
157
157
}
158
158
} else {
159
- // For permanent error changes the condition
159
+ // For permanent error that changes the condition
160
160
for i := range c .conditions {
161
161
condition := & c .conditions [i ]
162
162
if condition .Type == result .Rule .Condition {
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
175
+ }
176
+ }
177
+
178
+ needToUpdateCondition := true
179
+ var newReason string
180
+ var newMessage string
163
181
status := toConditionStatus (result .ExitStatus )
164
- // change 1: Condition status change from True to False/Unknown
165
182
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
- }
183
+ // Scenario 1: Condition status changes from True to False/Unknown
184
+ newReason = defaultConditionReason
185
+ if newMessage == "" {
186
+ newMessage = defaultConditionMessage
187
+ } else {
188
+ newMessage = result .Message
176
189
}
177
-
178
- inactiveProblemEvents = append (inactiveProblemEvents , util .GenerateConditionChangeEvent (
179
- condition .Type ,
180
- status ,
181
- defaultConditionReason ,
182
- timestamp ,
183
- ))
184
-
185
- condition .Status = status
186
- condition .Message = defaultConditionMessage
187
- condition .Reason = defaultConditionReason
188
190
} 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
191
+ // Scenario 2: Condition status changes from False/Unknown to True
192
+ newReason = result .Rule .Reason
193
+ newMessage = result .Message
201
194
} 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 &&
195
+ // Scenario 3: Condition status changes from False to Unknown or vice versa
196
+ newReason = defaultConditionReason
197
+ if newMessage == "" {
198
+ newMessage = defaultConditionMessage
199
+ } else {
200
+ newMessage = result .Message
201
+ }
202
+ } else if condition .Status == types .True && status == types .True &&
215
203
(condition .Reason != result .Rule .Reason ||
216
204
(* 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 .
218
206
// condition reason changes or
219
207
// 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 {
220
218
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
+
223
223
updateEvent := util .GenerateConditionChangeEvent (
224
224
condition .Type ,
225
225
status ,
226
- condition . Reason ,
226
+ newReason ,
227
227
timestamp ,
228
228
)
229
- if condition .Status == types .True {
229
+
230
+ if status == types .True {
230
231
activeProblemEvents = append (activeProblemEvents , updateEvent )
231
232
} else {
232
233
inactiveProblemEvents = append (inactiveProblemEvents , updateEvent )
0 commit comments