@@ -121,74 +121,74 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
121
121
})
122
122
}
123
123
} else {
124
- // For permanent error changes the condition
124
+ // For permanent error that changes the condition
125
125
for i := range c .conditions {
126
126
condition := & c .conditions [i ]
127
127
if condition .Type == result .Rule .Condition {
128
+ // The condition reason specified in the rule and the result message
129
+ // represent the problem happened. We need to know the default condition
130
+ // from the config, so that we can set the new condition reason/message
131
+ // back when such problem goes away.
132
+ var defaultConditionReason string
133
+ var defaultConditionMessage string
134
+ for j := range c .config .DefaultConditions {
135
+ defaultCondition := & c .config .DefaultConditions [j ]
136
+ if defaultCondition .Type == result .Rule .Condition {
137
+ defaultConditionReason = defaultCondition .Reason
138
+ defaultConditionMessage = defaultCondition .Message
139
+ break
140
+ }
141
+ }
142
+
143
+ needToUpdateCondition := true
144
+ var newReason string
145
+ var newMessage string
128
146
status := toConditionStatus (result .ExitStatus )
129
- // change 1: Condition status change from True to False/Unknown
130
147
if condition .Status == types .True && status != types .True {
131
- condition .Transition = timestamp
132
- var defaultConditionReason string
133
- var defaultConditionMessage string
134
- for j := range c .config .DefaultConditions {
135
- defaultCondition := & c .config .DefaultConditions [j ]
136
- if defaultCondition .Type == result .Rule .Condition {
137
- defaultConditionReason = defaultCondition .Reason
138
- defaultConditionMessage = defaultCondition .Message
139
- break
140
- }
148
+ // Scenario 1: Condition status changes from True to False/Unknown
149
+ newReason = defaultConditionReason
150
+ if newMessage == "" {
151
+ newMessage = defaultConditionMessage
152
+ } else {
153
+ newMessage = result .Message
141
154
}
142
-
143
- events = append (events , util .GenerateConditionChangeEvent (
144
- condition .Type ,
145
- status ,
146
- defaultConditionReason ,
147
- timestamp ,
148
- ))
149
-
150
- condition .Status = status
151
- condition .Message = defaultConditionMessage
152
- condition .Reason = defaultConditionReason
153
155
} else if condition .Status != types .True && status == types .True {
154
- // change 2: Condition status change from False/Unknown to True
155
- condition .Transition = timestamp
156
- condition .Message = result .Message
157
- events = append (events , util .GenerateConditionChangeEvent (
158
- condition .Type ,
159
- status ,
160
- result .Rule .Reason ,
161
- timestamp ,
162
- ))
163
-
164
- condition .Status = status
165
- condition .Reason = result .Rule .Reason
156
+ // Scenario 2: Condition status changes from False/Unknown to True
157
+ newReason = result .Rule .Reason
158
+ newMessage = result .Message
166
159
} else if condition .Status != status {
167
- // change 3: Condition status change from False to Unknown or vice versa
168
- condition .Transition = timestamp
169
- condition .Message = result .Message
170
- events = append (events , util .GenerateConditionChangeEvent (
171
- condition .Type ,
172
- status ,
173
- result .Rule .Reason ,
174
- timestamp ,
175
- ))
176
-
177
- condition .Status = status
178
- condition .Reason = result .Rule .Reason
179
- } else if condition .Status == status &&
160
+ // Scenario 3: Condition status changes from False to Unknown or vice versa
161
+ newReason = defaultConditionReason
162
+ if newMessage == "" {
163
+ newMessage = defaultConditionMessage
164
+ } else {
165
+ newMessage = result .Message
166
+ }
167
+ } else if condition .Status == types .True && status == types .True &&
180
168
(condition .Reason != result .Rule .Reason ||
181
169
(* c .config .PluginGlobalConfig .EnableMessageChangeBasedConditionUpdate && condition .Message != result .Message )) {
182
- // change 4: Condition status do not change.
170
+ // Scenario 4: Condition status does not change and it stays true .
183
171
// condition reason changes or
184
172
// condition message changes when message based condition update is enabled.
173
+ newReason = result .Rule .Reason
174
+ newMessage = result .Message
175
+ } else {
176
+ // Scenario 5: Condition status does not change and it stays False/Unknown.
177
+ // This should just be the default reason or message (as a consequence
178
+ // of scenario 1 and scenario 3 above).
179
+ needToUpdateCondition = false
180
+ }
181
+
182
+ if needToUpdateCondition {
185
183
condition .Transition = timestamp
186
- condition .Reason = result .Rule .Reason
187
- condition .Message = result .Message
184
+ condition .Status = status
185
+ condition .Reason = newReason
186
+ condition .Message = newMessage
187
+
188
188
events = append (events , util .GenerateConditionChangeEvent (
189
189
condition .Type ,
190
190
status ,
191
- condition . Reason ,
191
+ newReason ,
192
192
timestamp ,
193
193
))
194
194
}
0 commit comments