Skip to content

Commit 3932154

Browse files
authored
Set default messages & reconcile clusteroperator status conditions (#584) (#588)
1 parent d42ff0e commit 3932154

File tree

3 files changed

+34
-38
lines changed

3 files changed

+34
-38
lines changed

pkg/controller/status/conditions.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,21 @@ func newConditions(cos *configv1.ClusterOperatorStatus, time metav1.Time) *condi
5353
}
5454
}
5555

56-
func (c *conditions) setCondition(condition configv1.ClusterStatusConditionType,
56+
func (c *conditions) setCondition(conditionType configv1.ClusterStatusConditionType,
5757
status configv1.ConditionStatus, reason, message string, lastTime metav1.Time) {
58-
entries := make(conditionsMap)
59-
for k, v := range c.entryMap {
60-
entries[k] = v
58+
originalCondition, ok := c.entryMap[conditionType]
59+
// if condition is defined and there is not new status then don't update transition time
60+
if ok && originalCondition.Status == status {
61+
lastTime = originalCondition.LastTransitionTime
6162
}
6263

63-
existing, ok := c.entryMap[condition]
64-
if !ok || existing.Status != status || existing.Reason != reason {
65-
if lastTime.IsZero() {
66-
lastTime = metav1.Now()
67-
}
68-
entries[condition] = configv1.ClusterOperatorStatusCondition{
69-
Type: condition,
70-
Status: status,
71-
Reason: reason,
72-
Message: message,
73-
LastTransitionTime: lastTime,
74-
}
64+
c.entryMap[conditionType] = configv1.ClusterOperatorStatusCondition{
65+
Type: conditionType,
66+
Reason: reason,
67+
Status: status,
68+
Message: message,
69+
LastTransitionTime: lastTime,
7570
}
76-
77-
c.entryMap = entries
7871
}
7972

8073
func (c *conditions) removeCondition(condition configv1.ClusterStatusConditionType) {

pkg/controller/status/controller.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
// OCMAPIFailureCountThreshold defines how many unsuccessful responses from the OCM API in a row is tolerated
3131
// before the operator is marked as Degraded
3232
OCMAPIFailureCountThreshold = 5
33+
34+
insightsAvailableMessage = "Insights works as expected"
3335
)
3436

3537
type Reported struct {
@@ -146,10 +148,6 @@ func (c *Controller) merge(clusterOperator *configv1.ClusterOperator) *configv1.
146148
// cluster operator conditions
147149
cs := newConditions(&clusterOperator.Status, metav1.Time{Time: now})
148150
updateControllerConditions(cs, c.ctrlStatus, isInitializing, lastTransition)
149-
150-
// once the operator is running it is always considered available
151-
cs.setCondition(configv1.OperatorAvailable, configv1.ConditionTrue, "AsExpected", "", metav1.Now())
152-
153151
updateControllerConditionsByStatus(cs, c.ctrlStatus, isInitializing, lastTransition)
154152

155153
// all status conditions from conditions to cluster operator
@@ -265,7 +263,7 @@ func (c *Controller) Start(ctx context.Context) error {
265263
}
266264
}
267265
if err := c.updateStatus(ctx, false); err != nil {
268-
klog.Errorf("Unable to write cluster operator statusMessage: %v", err)
266+
klog.Errorf("Unable to write cluster operator status: %v", err)
269267
}
270268
}
271269
}, time.Second, ctx.Done())
@@ -285,32 +283,31 @@ func (c *Controller) updateStatus(ctx context.Context, initial bool) error {
285283
var reported Reported
286284
if len(existing.Status.Extension.Raw) > 0 {
287285
if err := json.Unmarshal(existing.Status.Extension.Raw, &reported); err != nil { //nolint: govet
288-
klog.Errorf("The initial operator extension statusMessage is invalid: %v", err)
286+
klog.Errorf("The initial operator extension status is invalid: %v", err)
289287
}
290288
}
291289
c.SetLastReportedTime(reported.LastReportTime.Time.UTC())
292290
cs := newConditions(&existing.Status, metav1.Now())
293291
if con := cs.findCondition(configv1.OperatorDegraded); con == nil ||
294292
con != nil && con.Status == configv1.ConditionFalse {
295-
klog.Info("The initial operator extension statusMessage is healthy")
293+
klog.Info("The initial operator extension status is healthy")
296294
}
297295
}
298296
}
299297

300-
updated := c.merge(existing)
298+
updatedClusterOperator := c.merge(existing)
301299
if existing == nil {
302-
created, err := c.client.ClusterOperators().Create(ctx, updated, metav1.CreateOptions{}) //nolint: govet
300+
created, err := c.client.ClusterOperators().Create(ctx, updatedClusterOperator, metav1.CreateOptions{}) //nolint: govet
303301
if err != nil {
304302
return err
305303
}
306-
updated.ObjectMeta = created.ObjectMeta
307-
updated.Spec = created.Spec
308-
} else if reflect.DeepEqual(updated.Status, existing.Status) {
309-
klog.V(4).Infof("No statusMessage update necessary, objects are identical")
304+
updatedClusterOperator.ObjectMeta = created.ObjectMeta
305+
updatedClusterOperator.Spec = created.Spec
306+
} else if reflect.DeepEqual(updatedClusterOperator.Status, existing.Status) {
307+
klog.V(4).Infof("No status update necessary, objects are identical")
310308
return nil
311309
}
312-
313-
_, err = c.client.ClusterOperators().UpdateStatus(ctx, updated, metav1.UpdateOptions{})
310+
_, err = c.client.ClusterOperators().UpdateStatus(ctx, updatedClusterOperator, metav1.UpdateOptions{})
314311
return err
315312
}
316313

@@ -339,7 +336,7 @@ func updateControllerConditions(cs *conditions, ctrlStatus *controllerStatus,
339336
if es := ctrlStatus.getStatus(ErrorStatus); es != nil {
340337
cs.setCondition(configv1.OperatorDegraded, configv1.ConditionTrue, es.reason, es.message, metav1.Time{Time: lastTransition})
341338
} else {
342-
cs.setCondition(configv1.OperatorDegraded, configv1.ConditionFalse, "AsExpected", "", metav1.Now())
339+
cs.setCondition(configv1.OperatorDegraded, configv1.ConditionFalse, "AsExpected", insightsAvailableMessage, metav1.Now())
343340
}
344341

345342
// handle when upload fails
@@ -379,6 +376,8 @@ func updateControllerConditionsByStatus(cs *conditions, ctrlStatus *controllerSt
379376
if es := ctrlStatus.getStatus(ErrorStatus); es != nil {
380377
klog.V(4).Infof("The operator has some internal errors: %s", es.message)
381378
cs.setCondition(configv1.OperatorProgressing, configv1.ConditionFalse, "Degraded", "An error has occurred", metav1.Now())
379+
cs.setCondition(configv1.OperatorAvailable, configv1.ConditionFalse, es.reason, es.message, metav1.Now())
380+
cs.setCondition(configv1.OperatorUpgradeable, configv1.ConditionFalse, "InsightsNotUpgradeable", es.message, metav1.Now())
382381
}
383382

384383
if ds := ctrlStatus.getStatus(DisabledStatus); ds != nil {
@@ -389,6 +388,9 @@ func updateControllerConditionsByStatus(cs *conditions, ctrlStatus *controllerSt
389388
if ctrlStatus.isHealthy() {
390389
klog.V(4).Infof("The operator is healthy")
391390
cs.setCondition(configv1.OperatorProgressing, configv1.ConditionFalse, "AsExpected", "Monitoring the cluster", metav1.Now())
391+
cs.setCondition(configv1.OperatorAvailable, configv1.ConditionTrue, "AsExpected", insightsAvailableMessage, metav1.Now())
392+
cs.setCondition(configv1.OperatorUpgradeable, configv1.ConditionTrue, "InsightsUpgradeable",
393+
"Insights operator can be upgraded", metav1.Now())
392394
}
393395
}
394396

@@ -403,6 +405,7 @@ func handleControllerStatusError(errs []string, errorReason string) (reason, mes
403405
reason = "UnknownError"
404406
}
405407
message = errs[0]
408+
reason = errorReason
406409
}
407410
return reason, message
408411
}

pkg/controllerstatus/controllerstatus.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ func (s *Simple) UpdateStatus(summary Summary) { //nolint: gocritic
5050
s.lock.Lock()
5151
defer s.lock.Unlock()
5252

53+
if summary.LastTransitionTime.IsZero() {
54+
s.summary.LastTransitionTime = time.Now()
55+
}
56+
5357
if s.summary.Healthy != summary.Healthy {
5458
klog.V(2).Infof("name=%s healthy=%t reason=%s message=%s", s.Name, summary.Healthy, summary.Reason, summary.Message)
55-
if summary.LastTransitionTime.IsZero() {
56-
summary.LastTransitionTime = time.Now()
57-
}
58-
5959
s.summary = summary
6060
s.summary.Count = 1
6161
return

0 commit comments

Comments
 (0)