Skip to content

Commit 318dcfe

Browse files
committed
Don't update condition if status stays False/Unknown for custom plugin
1 parent 89ab58b commit 318dcfe

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

pkg/custompluginmonitor/custom_plugin_monitor.go

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -121,74 +121,74 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
121121
})
122122
}
123123
} else {
124-
// For permanent error changes the condition
124+
// For permanent error that changes the condition
125125
for i := range c.conditions {
126126
condition := &c.conditions[i]
127127
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
128146
status := toConditionStatus(result.ExitStatus)
129-
// change 1: Condition status change from True to False/Unknown
130147
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
141154
}
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
153155
} 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
166159
} 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 &&
180168
(condition.Reason != result.Rule.Reason ||
181169
(*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.
183171
// condition reason changes or
184172
// 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 {
185183
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+
188188
events = append(events, util.GenerateConditionChangeEvent(
189189
condition.Type,
190190
status,
191-
condition.Reason,
191+
newReason,
192192
timestamp,
193193
))
194194
}

0 commit comments

Comments
 (0)