-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathreconcile_result.go
More file actions
86 lines (75 loc) · 3.48 KB
/
Copy pathreconcile_result.go
File metadata and controls
86 lines (75 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package common
import (
"fmt"
"time"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
v1alpha1 "github.com/openshift/cert-manager-operator/api/operator/v1alpha1"
)
// resolveConditionReason extracts a specific condition reason from the error
// chain if one was set via WithConditionReason, otherwise returns the default.
func resolveConditionReason(err error, defaultReason string) string {
if reason := GetConditionReason(err); reason != "" {
return reason
}
return defaultReason
}
// HandleReconcileResult processes the result of a reconciliation attempt and
// updates status conditions accordingly.
func HandleReconcileResult(
status *v1alpha1.ConditionalStatus,
reconcileErr error,
log logr.Logger,
updateConditionFn func(prependErr error) error,
requeueDuration time.Duration,
) (ctrl.Result, error) {
var errUpdate error
if reconcileErr != nil {
if IsIrrecoverableError(reconcileErr) {
reason := resolveConditionReason(reconcileErr, v1alpha1.ReasonFailed)
degradedChanged := status.SetCondition(v1alpha1.Degraded, metav1.ConditionTrue, reason, fmt.Sprintf("reconciliation failed with irrecoverable error not retrying: %v", reconcileErr))
readyChanged := status.SetCondition(v1alpha1.Ready, metav1.ConditionFalse, reason, "")
progressingChanged := status.SetCondition(v1alpha1.Progressing, metav1.ConditionFalse, reason, "")
if degradedChanged || readyChanged || progressingChanged {
log.V(2).Info("updating conditions on irrecoverable error",
"degradedChanged", degradedChanged,
"readyChanged", readyChanged,
"progressingChanged", progressingChanged,
"reason", reason,
"error", reconcileErr)
errUpdate = updateConditionFn(nil)
}
return ctrl.Result{}, errUpdate
}
readyReason := resolveConditionReason(reconcileErr, v1alpha1.ReasonInProgress)
progressingReason := resolveConditionReason(reconcileErr, v1alpha1.ReasonReconciling)
degradedChanged := status.SetCondition(v1alpha1.Degraded, metav1.ConditionFalse, v1alpha1.ReasonReady, "")
readyChanged := status.SetCondition(v1alpha1.Ready, metav1.ConditionFalse, readyReason, fmt.Sprintf("reconciliation failed, retrying: %v", reconcileErr))
progressingChanged := status.SetCondition(v1alpha1.Progressing, metav1.ConditionTrue, progressingReason, fmt.Sprintf("reconciliation in progress: %v", reconcileErr))
if degradedChanged || readyChanged || progressingChanged {
log.V(2).Info("updating conditions on recoverable error",
"degradedChanged", degradedChanged,
"readyChanged", readyChanged,
"progressingChanged", progressingChanged,
"reason", readyReason,
"error", reconcileErr)
errUpdate = updateConditionFn(reconcileErr)
}
if errUpdate != nil {
return ctrl.Result{}, errUpdate
}
return ctrl.Result{RequeueAfter: requeueDuration}, nil
}
degradedChanged := status.SetCondition(v1alpha1.Degraded, metav1.ConditionFalse, v1alpha1.ReasonReady, "")
readyChanged := status.SetCondition(v1alpha1.Ready, metav1.ConditionTrue, v1alpha1.ReasonReady, "reconciliation successful")
progressingChanged := status.SetCondition(v1alpha1.Progressing, metav1.ConditionFalse, v1alpha1.ReasonReady, "")
if degradedChanged || readyChanged || progressingChanged {
log.V(2).Info("updating conditions on successful reconciliation",
"degradedChanged", degradedChanged,
"readyChanged", readyChanged,
"progressingChanged", progressingChanged)
errUpdate = updateConditionFn(nil)
}
return ctrl.Result{}, errUpdate
}