/
status.go
161 lines (132 loc) · 3.64 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package reconciler
import (
"fmt"
"strings"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type Status struct {
S kcv1alpha1.GenericStatus
UpdateFunc func(kcv1alpha1.GenericStatus)
}
func (s *Status) Result() kcv1alpha1.GenericStatus { return s.S }
func (s *Status) IsReconciling() bool {
for _, cond := range s.S.Conditions {
if cond.Type == kcv1alpha1.Reconciling {
return true
}
}
return false
}
func (s *Status) IsReconcileSucceeded() bool {
for _, cond := range s.S.Conditions {
if cond.Type == kcv1alpha1.ReconcileSucceeded {
return true
}
}
return false
}
func (s *Status) IsReconcileFailed() bool {
for _, cond := range s.S.Conditions {
if cond.Type == kcv1alpha1.ReconcileFailed {
return true
}
}
return false
}
func (s *Status) IsDeleting() bool {
for _, cond := range s.S.Conditions {
if cond.Type == kcv1alpha1.Deleting {
return true
}
}
return false
}
func (s *Status) IsDeleteFailed() bool {
for _, cond := range s.S.Conditions {
if cond.Type == kcv1alpha1.DeleteFailed {
return true
}
}
return false
}
func (s *Status) SetReconciling(meta metav1.ObjectMeta) {
s.markObservedLatest(meta)
s.removeAllConditions()
s.S.Conditions = append(s.S.Conditions, kcv1alpha1.AppCondition{
Type: kcv1alpha1.Reconciling,
Status: corev1.ConditionTrue,
})
s.S.FriendlyDescription = "Reconciling"
s.UpdateFunc(s.S)
}
func (s *Status) SetDeleting(meta metav1.ObjectMeta) {
s.markObservedLatest(meta)
s.removeAllConditions()
s.S.Conditions = append(s.S.Conditions, kcv1alpha1.AppCondition{
Type: kcv1alpha1.Deleting,
Status: corev1.ConditionTrue,
})
s.S.FriendlyDescription = "Deleting"
s.UpdateFunc(s.S)
}
func (s *Status) SetReconcileCompleted(err error) {
s.removeAllConditions()
if err != nil {
s.S.Conditions = append(s.S.Conditions, kcv1alpha1.AppCondition{
Type: kcv1alpha1.ReconcileFailed,
Status: corev1.ConditionTrue,
Message: err.Error(),
})
s.S.FriendlyDescription = s.friendlyErrMsg(fmt.Sprintf("Reconcile failed: %s", err.Error()))
} else {
s.S.Conditions = append(s.S.Conditions, kcv1alpha1.AppCondition{
Type: kcv1alpha1.ReconcileSucceeded,
Status: corev1.ConditionTrue,
Message: "",
})
s.S.FriendlyDescription = "Reconcile succeeded"
s.S.UsefulErrorMessage = ""
}
s.UpdateFunc(s.S)
}
func (s *Status) SetDeleteCompleted(err error) {
s.removeAllConditions()
if err != nil {
s.S.Conditions = append(s.S.Conditions, kcv1alpha1.AppCondition{
Type: kcv1alpha1.DeleteFailed,
Status: corev1.ConditionTrue,
Message: err.Error(),
})
s.S.FriendlyDescription = s.friendlyErrMsg(fmt.Sprintf("Delete failed: %s", err.Error()))
} else {
// assume resource will be deleted, hence nothing to update
}
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)
return result, err
}
func (s *Status) markObservedLatest(meta metav1.ObjectMeta) {
s.S.ObservedGeneration = meta.Generation
}
func (s *Status) removeAllConditions() {
s.S.Conditions = nil
}
func (s *Status) SetUsefulErrorMessage(errMsg string) {
s.S.UsefulErrorMessage = errMsg
}