@@ -158,77 +158,78 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
158
158
})
159
159
}
160
160
} else {
161
- // For permanent error changes the condition
161
+ // For permanent error that changes the condition
162
162
for i := range c .conditions {
163
163
condition := & c .conditions [i ]
164
164
if condition .Type == result .Rule .Condition {
165
+ // The condition reason specified in the rule and the result message
166
+ // represent the problem happened. We need to know the default condition
167
+ // from the config, so that we can set the new condition reason/message
168
+ // back when such problem goes away.
169
+ var defaultConditionReason string
170
+ var defaultConditionMessage string
171
+ for j := range c .config .DefaultConditions {
172
+ defaultCondition := & c .config .DefaultConditions [j ]
173
+ if defaultCondition .Type == result .Rule .Condition {
174
+ defaultConditionReason = defaultCondition .Reason
175
+ defaultConditionMessage = defaultCondition .Message
176
+ break
177
+ }
178
+ }
179
+
180
+ needToUpdateCondition := true
181
+ var newReason string
182
+ var newMessage string
165
183
status := toConditionStatus (result .ExitStatus )
166
- // change 1: Condition status change from True to False/Unknown
167
184
if condition .Status == types .True && status != types .True {
168
- condition .Transition = timestamp
169
- var defaultConditionReason string
170
- var defaultConditionMessage string
171
- for j := range c .config .DefaultConditions {
172
- defaultCondition := & c .config .DefaultConditions [j ]
173
- if defaultCondition .Type == result .Rule .Condition {
174
- defaultConditionReason = defaultCondition .Reason
175
- defaultConditionMessage = defaultCondition .Message
176
- break
177
- }
185
+ // Scenario 1: Condition status changes from True to False/Unknown
186
+ newReason = defaultConditionReason
187
+ if newMessage == "" {
188
+ newMessage = defaultConditionMessage
189
+ } else {
190
+ newMessage = result .Message
178
191
}
179
-
180
- inactiveProblemEvents = append (inactiveProblemEvents , util .GenerateConditionChangeEvent (
181
- condition .Type ,
182
- status ,
183
- defaultConditionReason ,
184
- timestamp ,
185
- ))
186
-
187
- condition .Status = status
188
- condition .Message = defaultConditionMessage
189
- condition .Reason = defaultConditionReason
190
192
} else if condition .Status != types .True && status == types .True {
191
- // change 2: Condition status change from False/Unknown to True
192
- condition .Transition = timestamp
193
- condition .Message = result .Message
194
- activeProblemEvents = append (activeProblemEvents , util .GenerateConditionChangeEvent (
195
- condition .Type ,
196
- status ,
197
- result .Rule .Reason ,
198
- timestamp ,
199
- ))
200
-
201
- condition .Status = status
202
- 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
203
196
} else if condition .Status != status {
204
- // change 3: Condition status change from False to Unknown or vice versa
205
- condition .Transition = timestamp
206
- condition .Message = result .Message
207
- inactiveProblemEvents = append (inactiveProblemEvents , util .GenerateConditionChangeEvent (
208
- condition .Type ,
209
- status ,
210
- result .Rule .Reason ,
211
- timestamp ,
212
- ))
213
-
214
- condition .Status = status
215
- condition .Reason = result .Rule .Reason
216
- } 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
+ } else {
202
+ newMessage = result .Message
203
+ }
204
+ } else if condition .Status == types .True && status == types .True &&
217
205
(condition .Reason != result .Rule .Reason ||
218
206
(* c .config .PluginGlobalConfig .EnableMessageChangeBasedConditionUpdate && condition .Message != result .Message )) {
219
- // change 4: Condition status do not change.
207
+ // Scenario 4: Condition status does not change and it stays true .
220
208
// condition reason changes or
221
209
// condition message changes when message based condition update is enabled.
210
+ newReason = result .Rule .Reason
211
+ newMessage = result .Message
212
+ } else {
213
+ // Scenario 5: Condition status does not change and it stays False/Unknown.
214
+ // This should just be the default reason or message (as a consequence
215
+ // of scenario 1 and scenario 3 above).
216
+ needToUpdateCondition = false
217
+ }
218
+
219
+ if needToUpdateCondition {
222
220
condition .Transition = timestamp
223
- condition .Reason = result .Rule .Reason
224
- condition .Message = result .Message
221
+ condition .Status = status
222
+ condition .Reason = newReason
223
+ condition .Message = newMessage
224
+
225
225
updateEvent := util .GenerateConditionChangeEvent (
226
226
condition .Type ,
227
227
status ,
228
- condition . Reason ,
228
+ newReason ,
229
229
timestamp ,
230
230
)
231
- if condition .Status == types .True {
231
+
232
+ if status == types .True {
232
233
activeProblemEvents = append (activeProblemEvents , updateEvent )
233
234
} else {
234
235
inactiveProblemEvents = append (inactiveProblemEvents , updateEvent )
0 commit comments