-
Notifications
You must be signed in to change notification settings - Fork 28
/
status.go
104 lines (85 loc) · 2.6 KB
/
status.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package reconciler
import (
"fmt"
"strings"
sgv1alpha1 "github.com/vmware-tanzu/carvel-secretgen-controller/pkg/apis/secretgen/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// TerminalReconcileErr represent a reconciliation error that
// cannot be corrected without some change in the system
// (e.g. reconfiguring resources, adding/removing resources).
// It's indicative to not requeue a reconcile request.
type TerminalReconcileErr struct {
Err error
}
func (e TerminalReconcileErr) Error() string { return e.Err.Error() }
type Status struct {
S sgv1alpha1.GenericStatus
UpdateFunc func(sgv1alpha1.GenericStatus)
}
func (s *Status) Result() sgv1alpha1.GenericStatus { return s.S }
func (s *Status) IsReconcileSucceeded() bool {
for _, cond := range s.S.Conditions {
if cond.Type == sgv1alpha1.ReconcileSucceeded {
return true
}
}
return false
}
func (s *Status) SetReconciling(meta metav1.ObjectMeta) {
s.markObservedLatest(meta)
s.removeAllConditions()
s.S.Conditions = append(s.S.Conditions, sgv1alpha1.Condition{
Type: sgv1alpha1.Reconciling,
Status: corev1.ConditionTrue,
})
s.S.FriendlyDescription = "Reconciling"
s.UpdateFunc(s.S)
}
func (s *Status) SetReconcileCompleted(err error) {
s.removeAllConditions()
if err != nil {
s.S.Conditions = append(s.S.Conditions, sgv1alpha1.Condition{
Type: sgv1alpha1.ReconcileFailed,
Status: corev1.ConditionTrue,
Message: err.Error(),
})
s.S.FriendlyDescription = s.friendlyErrMsg(fmt.Sprintf("Reconcile failed: %s", err))
} else {
s.S.Conditions = append(s.S.Conditions, sgv1alpha1.Condition{
Type: sgv1alpha1.ReconcileSucceeded,
Status: corev1.ConditionTrue,
Message: "",
})
s.S.FriendlyDescription = "Reconcile succeeded"
}
s.UpdateFunc(s.S)
}
func (s *Status) friendlyErrMsg(errMsg string) string {
errMsgPieces := strings.Split(errMsg, "\n")
if len(errMsgPieces[0]) > 80 {
errMsgPieces[0] = errMsgPieces[0][:80] + "..."
} else if len(errMsgPieces) > 1 {
errMsgPieces[0] += "..."
}
return errMsgPieces[0]
}
func (s *Status) WithReconcileCompleted(result reconcile.Result, err error) (reconcile.Result, error) {
s.SetReconcileCompleted(err)
if err != nil {
if _, ok := err.(TerminalReconcileErr); ok {
return reconcile.Result{}, nil
}
}
return result, err
}
func (s *Status) markObservedLatest(meta metav1.ObjectMeta) {
s.S.ObservedGeneration = meta.Generation
}
func (s *Status) removeAllConditions() {
s.S.Conditions = nil
}