-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition.go
246 lines (206 loc) · 6.71 KB
/
condition.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package monitoring
import (
"errors"
"regexp"
"time"
ncondition "github.com/rancher/norman/condition"
"github.com/rancher/norman/controller"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
)
var temfileRegexp = regexp.MustCompile("/tmp/[-_a-zA-Z0-9]+")
type condition ncondition.Cond
func (c condition) True(obj *mgmtv3.MonitoringStatus) {
setStatus(obj, mgmtv3.ClusterConditionType(c), corev1.ConditionTrue)
}
func (c condition) IsTrue(obj *mgmtv3.MonitoringStatus) bool {
return getStatus(obj, mgmtv3.ClusterConditionType(c)) == corev1.ConditionTrue
}
func (c condition) LastUpdated(obj *mgmtv3.MonitoringStatus, ts string) {
setTS(obj, mgmtv3.ClusterConditionType(c), ts)
}
func (c condition) GetLastUpdated(obj *mgmtv3.MonitoringStatus) string {
return getTS(obj, mgmtv3.ClusterConditionType(c))
}
func (c condition) False(obj *mgmtv3.MonitoringStatus) {
setStatus(obj, mgmtv3.ClusterConditionType(c), corev1.ConditionFalse)
}
func (c condition) IsFalse(obj *mgmtv3.MonitoringStatus) bool {
return getStatus(obj, mgmtv3.ClusterConditionType(c)) == corev1.ConditionFalse
}
func (c condition) GetStatus(obj *mgmtv3.MonitoringStatus) corev1.ConditionStatus {
return getStatus(obj, mgmtv3.ClusterConditionType(c))
}
func (c condition) Unknown(obj *mgmtv3.MonitoringStatus) {
setStatus(obj, mgmtv3.ClusterConditionType(c), corev1.ConditionUnknown)
}
func (c condition) CreateUnknownIfNotExists(obj *mgmtv3.MonitoringStatus) {
cond := findCond(obj, mgmtv3.ClusterConditionType(c))
if cond == nil {
c.Unknown(obj)
}
}
func (c condition) IsUnknown(obj *mgmtv3.MonitoringStatus) bool {
return getStatus(obj, mgmtv3.ClusterConditionType(c)) == corev1.ConditionUnknown
}
func (c condition) Reason(obj *mgmtv3.MonitoringStatus, reason string) {
condIdx := findOrCreateCond(obj, mgmtv3.ClusterConditionType(c))
obj.Conditions[*condIdx].Reason = reason
touchTS(obj, condIdx)
}
func (c condition) SetMessageIfBlank(obj *mgmtv3.MonitoringStatus, message string) {
if c.GetMessage(obj) == "" {
c.Message(obj, message)
}
}
func (c condition) Message(obj *mgmtv3.MonitoringStatus, message string) {
condIdx := findOrCreateCond(obj, mgmtv3.ClusterConditionType(c))
obj.Conditions[*condIdx].Message = message
touchTS(obj, condIdx)
}
func (c condition) GetMessage(obj *mgmtv3.MonitoringStatus) string {
condIdx := findCond(obj, mgmtv3.ClusterConditionType(c))
if condIdx == nil {
return ""
}
return obj.Conditions[*condIdx].Message
}
func (c condition) ReasonAndMessageFromError(obj *mgmtv3.MonitoringStatus, err error) {
if k8serrors.IsConflict(err) {
return
}
condIdx := findOrCreateCond(obj, mgmtv3.ClusterConditionType(c))
obj.Conditions[*condIdx].Message = err.Error()
obj.Conditions[*condIdx].Reason = "Error"
touchTS(obj, condIdx)
}
func (c condition) GetReason(obj *mgmtv3.MonitoringStatus) string {
condIdx := findCond(obj, mgmtv3.ClusterConditionType(c))
if condIdx == nil {
return ""
}
return obj.Conditions[*condIdx].Reason
}
func (c condition) Once(obj *mgmtv3.MonitoringStatus, f func() (*mgmtv3.MonitoringStatus, error)) (*mgmtv3.MonitoringStatus, error) {
if c.IsFalse(obj) {
return obj, &controller.ForgetError{
Err: errors.New(c.GetReason(obj)),
}
}
return c.DoUntilTrue(obj, f)
}
func (c condition) DoUntilTrue(obj *mgmtv3.MonitoringStatus, f func() (*mgmtv3.MonitoringStatus, error)) (*mgmtv3.MonitoringStatus, error) {
if c.IsTrue(obj) {
return obj, nil
}
return c.do(obj, f, true)
}
func (c condition) DoUntilFalse(obj *mgmtv3.MonitoringStatus, f func() (*mgmtv3.MonitoringStatus, error)) (*mgmtv3.MonitoringStatus, error) {
if c.IsFalse(obj) {
return obj, nil
}
return c.do(obj, f, false)
}
func (c condition) Do(obj *mgmtv3.MonitoringStatus, f func() (*mgmtv3.MonitoringStatus, error)) (*mgmtv3.MonitoringStatus, error) {
return c.do(obj, f, true)
}
func (c condition) do(obj *mgmtv3.MonitoringStatus, f func() (*mgmtv3.MonitoringStatus, error), conditionStatus bool) (*mgmtv3.MonitoringStatus, error) {
status := c.GetStatus(obj)
ts := c.GetLastUpdated(obj)
reason := c.GetReason(obj)
message := c.GetMessage(obj)
obj, err := c.doInternal(obj, f, conditionStatus)
// This is to prevent non stop flapping of states and update
if status == c.GetStatus(obj) &&
reason == c.GetReason(obj) {
if message != c.GetMessage(obj) {
replaced := temfileRegexp.ReplaceAllString(c.GetMessage(obj), "file_path_redacted")
c.Message(obj, replaced)
}
if message == c.GetMessage(obj) {
c.LastUpdated(obj, ts)
}
}
return obj, err
}
func (c condition) doInternal(obj *mgmtv3.MonitoringStatus, f func() (*mgmtv3.MonitoringStatus, error), conditionStatus bool) (*mgmtv3.MonitoringStatus, error) {
if !c.IsFalse(obj) {
c.Unknown(obj)
}
newObj, err := f()
if newObj != nil {
obj = newObj
}
if err != nil {
if _, ok := err.(*controller.ForgetError); ok {
if c.GetMessage(obj) == "" {
c.ReasonAndMessageFromError(obj, err)
}
return obj, err
}
if conditionStatus {
c.False(obj)
} else {
c.Unknown(obj)
}
c.ReasonAndMessageFromError(obj, err)
return obj, err
}
if conditionStatus {
c.True(obj)
} else {
c.False(obj)
}
c.Reason(obj, "")
c.Message(obj, "")
return obj, nil
}
func touchTS(obj *mgmtv3.MonitoringStatus, condIdx *int) {
obj.Conditions[*condIdx].LastUpdateTime = time.Now().Format(time.RFC3339)
}
func setTS(obj *mgmtv3.MonitoringStatus, condName mgmtv3.ClusterConditionType, ts string) {
condIdx := findOrCreateCond(obj, condName)
obj.Conditions[*condIdx].LastUpdateTime = ts
}
func getTS(obj *mgmtv3.MonitoringStatus, condName mgmtv3.ClusterConditionType) string {
condIdx := findCond(obj, condName)
if condIdx == nil {
return ""
}
return obj.Conditions[*condIdx].LastUpdateTime
}
func setStatus(obj *mgmtv3.MonitoringStatus, condName mgmtv3.ClusterConditionType, status corev1.ConditionStatus) {
condIdx := findOrCreateCond(obj, condName)
obj.Conditions[*condIdx].Status = status
touchTS(obj, condIdx)
}
func getStatus(obj *mgmtv3.MonitoringStatus, condName mgmtv3.ClusterConditionType) corev1.ConditionStatus {
condIdx := findCond(obj, condName)
if condIdx == nil {
return ""
}
return obj.Conditions[*condIdx].Status
}
func findCond(obj *mgmtv3.MonitoringStatus, condName mgmtv3.ClusterConditionType) *int {
for idx, cond := range obj.Conditions {
if cond.Type == condName {
return &idx
}
}
return nil
}
func findOrCreateCond(obj *mgmtv3.MonitoringStatus, condName mgmtv3.ClusterConditionType) *int {
if condIdx := findCond(obj, condName); condIdx != nil {
return condIdx
}
obj.Conditions = append(
obj.Conditions,
mgmtv3.MonitoringCondition{
Type: condName,
Status: corev1.ConditionUnknown,
},
)
size := len(obj.Conditions) - 1
return &size
}