-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathstatus.go
358 lines (312 loc) · 11.9 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package operator
import (
"context"
"fmt"
"reflect"
"strings"
osconfigv1 "github.com/openshift/api/config/v1"
"github.com/openshift/library-go/pkg/config/clusteroperator/v1helpers"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)
// StatusReason is a MixedCaps string representing the reason for a
// status condition change.
type StatusReason string
// The default set of status change reasons.
const (
ReasonAsExpected StatusReason = "AsExpected"
ReasonInitializing StatusReason = "Initializing"
ReasonSyncing StatusReason = "SyncingResources"
ReasonSyncFailed StatusReason = "SyncingFailed"
)
const (
clusterOperatorName = "machine-api"
)
var (
// This is to be compliant with
// https://github.com/openshift/cluster-version-operator/blob/b57ee63baf65f7cb6e95a8b2b304d88629cfe3c0/docs/dev/clusteroperator.md#what-should-an-operator-report-with-clusteroperator-custom-resource
// When known hazardous states for upgrades are determined
// specific "Upgradeable=False" status can be added with messages for how admins
// can resolve it.
operatorUpgradeable = newClusterOperatorStatusCondition(osconfigv1.OperatorUpgradeable, osconfigv1.ConditionTrue, "", "")
)
// statusProgressing sets the Progressing condition to True, with the given
// reason and message, and sets the upgradeable condition to True. It does not
// modify any existing Available or Degraded conditions.
func (optr *Operator) statusProgressing() error {
desiredVersions := optr.operandVersions
currentVersions, err := optr.getCurrentVersions()
if err != nil {
klog.Errorf("Error getting operator current versions: %v", err)
return err
}
var isProgressing osconfigv1.ConditionStatus
co, err := optr.getOrCreateClusterOperator()
if err != nil {
klog.Errorf("Failed to get or create Cluster Operator: %v", err)
return err
}
var message, reason string
if !reflect.DeepEqual(desiredVersions, currentVersions) {
klog.V(2).Info("Syncing status: progressing")
message = fmt.Sprintf("Progressing towards %s", optr.printOperandVersions())
optr.eventRecorder.Eventf(co, v1.EventTypeNormal, "Status upgrade", message)
isProgressing = osconfigv1.ConditionTrue
reason = string(ReasonSyncing)
} else {
klog.V(2).Info("Syncing status: re-syncing")
reason = string(ReasonAsExpected)
isProgressing = osconfigv1.ConditionFalse
}
conds := []osconfigv1.ClusterOperatorStatusCondition{
newClusterOperatorStatusCondition(osconfigv1.OperatorProgressing, isProgressing, reason, message),
operatorUpgradeable,
}
return optr.syncStatus(co, conds)
}
// statusAvailable sets the Available condition to True, with the given reason
// and message, and sets both the Progressing and Degraded conditions to False.
func (optr *Operator) statusAvailable(message string) error {
conds := []osconfigv1.ClusterOperatorStatusCondition{
newClusterOperatorStatusCondition(osconfigv1.OperatorAvailable, osconfigv1.ConditionTrue, string(ReasonAsExpected), message),
newClusterOperatorStatusCondition(osconfigv1.OperatorProgressing, osconfigv1.ConditionFalse, string(ReasonAsExpected), ""),
newClusterOperatorStatusCondition(osconfigv1.OperatorDegraded, osconfigv1.ConditionFalse, string(ReasonAsExpected), ""),
operatorUpgradeable,
}
co, err := optr.getOrCreateClusterOperator()
if err != nil {
return err
}
// important: we only write the version field if we report available at the present level
co.Status.Versions = optr.operandVersions
klog.V(2).Info("Syncing status: available")
return optr.syncStatus(co, conds)
}
// statusDegraded sets the Degraded condition to True, with the given reason and
// message, and sets the upgradeable condition. It does not modify any existing
// Available or Progressing conditions.
func (optr *Operator) statusDegraded(error string) error {
desiredVersions := optr.operandVersions
currentVersions, err := optr.getCurrentVersions()
if err != nil {
klog.Errorf("Error getting current versions: %v", err)
return err
}
var message string
if !reflect.DeepEqual(desiredVersions, currentVersions) {
message = fmt.Sprintf("Failed when progressing towards %s because %s", optr.printOperandVersions(), error)
} else {
message = fmt.Sprintf("Failed to resync for %s because %s", optr.printOperandVersions(), error)
}
conds := []osconfigv1.ClusterOperatorStatusCondition{
newClusterOperatorStatusCondition(osconfigv1.OperatorDegraded, osconfigv1.ConditionTrue,
string(ReasonSyncFailed), message),
operatorUpgradeable,
}
co, err := optr.getOrCreateClusterOperator()
if err != nil {
return err
}
optr.eventRecorder.Eventf(co, v1.EventTypeWarning, "Status degraded", error)
klog.V(2).Info("Syncing status: degraded")
return optr.syncStatus(co, conds)
}
func newClusterOperatorStatusCondition(conditionType osconfigv1.ClusterStatusConditionType,
conditionStatus osconfigv1.ConditionStatus, reason string,
message string) osconfigv1.ClusterOperatorStatusCondition {
return osconfigv1.ClusterOperatorStatusCondition{
Type: conditionType,
Status: conditionStatus,
LastTransitionTime: metav1.Now(),
Reason: reason,
Message: message,
}
}
// syncStatus applies the new condition to the mao ClusterOperator object.
func (optr *Operator) syncStatus(co *osconfigv1.ClusterOperator, conds []osconfigv1.ClusterOperatorStatusCondition) error {
for _, c := range conds {
v1helpers.SetStatusCondition(&co.Status.Conditions, c)
}
if co.Annotations == nil {
co.Annotations = map[string]string{}
}
co.Annotations["openshift.io/required-scc"] = "restricted-v2"
_, err := optr.osClient.ConfigV1().ClusterOperators().UpdateStatus(context.Background(), co, metav1.UpdateOptions{})
return err
}
// relatedObjects returns the current list of ObjectReference's for the
// ClusterOperator objects's status.
func (optr *Operator) relatedObjects() []osconfigv1.ObjectReference {
return []osconfigv1.ObjectReference{
{
Group: "",
Resource: "namespaces",
Name: optr.namespace,
},
{
Group: "machine.openshift.io",
Resource: "machines",
Name: "",
Namespace: optr.namespace,
},
{
Group: "machine.openshift.io",
Resource: "machinesets",
Name: "",
Namespace: optr.namespace,
},
{
Group: "machine.openshift.io",
Resource: "machinehealthchecks",
Name: "",
Namespace: optr.namespace,
},
{
Group: "rbac.authorization.k8s.io",
Resource: "roles",
Name: "",
Namespace: optr.namespace,
},
{
Group: "rbac.authorization.k8s.io",
Resource: "clusterroles",
Name: "machine-api-operator",
},
{
Group: "rbac.authorization.k8s.io",
Resource: "clusterroles",
Name: "machine-api-controllers",
},
{
Group: "metal3.io",
Resource: "baremetalhosts",
Name: "",
Namespace: optr.namespace,
},
}
}
// defaultStatusConditions returns the default set of status conditions for the
// ClusterOperator resource used on first creation of the ClusterOperator.
func (optr *Operator) defaultStatusConditions() []osconfigv1.ClusterOperatorStatusCondition {
return []osconfigv1.ClusterOperatorStatusCondition{
newClusterOperatorStatusCondition(
osconfigv1.OperatorProgressing,
osconfigv1.ConditionTrue,
string(ReasonInitializing),
"Operator is initializing",
),
newClusterOperatorStatusCondition(
osconfigv1.OperatorDegraded,
osconfigv1.ConditionFalse,
string(ReasonAsExpected), "",
),
newClusterOperatorStatusCondition(
osconfigv1.OperatorAvailable,
osconfigv1.ConditionFalse,
string(ReasonInitializing),
"Operator is initializing",
),
}
}
// defaultClusterOperator returns the default ClusterOperator resource with
// default values for related objects and status conditions.
func (optr *Operator) defaultClusterOperator() *osconfigv1.ClusterOperator {
return &osconfigv1.ClusterOperator{
ObjectMeta: metav1.ObjectMeta{
Name: clusterOperatorName,
Annotations: map[string]string{
"openshift.io/required-scc": "restricted-v2",
},
},
Status: osconfigv1.ClusterOperatorStatus{
Conditions: optr.defaultStatusConditions(),
RelatedObjects: optr.relatedObjects(),
},
}
}
// updateRelatedObjects updates the ClusterOperator's related objects field if
// necessary and returns the updated ClusterOperator object.
func (optr *Operator) updateRelatedObjects(co *osconfigv1.ClusterOperator) (*osconfigv1.ClusterOperator, error) {
relatedObjects := optr.relatedObjects()
if !equality.Semantic.DeepEqual(co.Status.RelatedObjects, relatedObjects) {
co.Status.RelatedObjects = relatedObjects
return optr.osClient.ConfigV1().ClusterOperators().UpdateStatus(context.Background(), co, metav1.UpdateOptions{})
}
return co, nil
}
// setMissingStatusConditions checks that the given ClusterOperator has a value
// for each of the default status conditions, and sets the default value for any
// that are missing.
func (optr *Operator) setMissingStatusConditions(co *osconfigv1.ClusterOperator) (*osconfigv1.ClusterOperator, error) {
var modified bool
for _, c := range optr.defaultStatusConditions() {
if v1helpers.FindStatusCondition(co.Status.Conditions, c.Type) == nil {
v1helpers.SetStatusCondition(&co.Status.Conditions, c)
modified = true
}
}
if modified {
return optr.osClient.ConfigV1().ClusterOperators().UpdateStatus(context.Background(), co, metav1.UpdateOptions{})
}
return co, nil
}
// getClusterOperator returns the current ClusterOperator.
func (optr *Operator) getClusterOperator() (*osconfigv1.ClusterOperator, error) {
return optr.osClient.ConfigV1().ClusterOperators().
Get(context.Background(), clusterOperatorName, metav1.GetOptions{})
}
// createClusterOperator creates the ClusterOperator and updates its status.
func (optr *Operator) createClusterOperator() (*osconfigv1.ClusterOperator, error) {
defaultCO := optr.defaultClusterOperator()
co, err := optr.osClient.ConfigV1().ClusterOperators().Create(context.Background(), defaultCO, metav1.CreateOptions{})
if err != nil {
return nil, err
}
co.Status = defaultCO.Status
return optr.osClient.ConfigV1().ClusterOperators().UpdateStatus(context.Background(), co, metav1.UpdateOptions{})
}
// getOrCreateClusterOperator fetches the current ClusterOperator or creates a
// default one if not found -- ensuring the related objects list is current.
func (optr *Operator) getOrCreateClusterOperator() (*osconfigv1.ClusterOperator, error) {
existing, err := optr.getClusterOperator()
if errors.IsNotFound(err) {
klog.Infof("ClusterOperator does not exist, creating a new one.")
return optr.createClusterOperator()
}
if err != nil {
return nil, fmt.Errorf("failed to get clusterOperator %q: %v", clusterOperatorName, err)
}
// Update any missing status conditions with their default value.
existing, err = optr.setMissingStatusConditions(existing)
if err != nil {
return nil, fmt.Errorf("failed to set default conditions: %v", err)
}
return optr.updateRelatedObjects(existing)
}
func (optr *Operator) getCurrentVersions() ([]osconfigv1.OperandVersion, error) {
co, err := optr.getOrCreateClusterOperator()
if err != nil {
return nil, err
}
return co.Status.Versions, nil
}
func (optr *Operator) printOperandVersions() string {
versionsOutput := []string{}
for _, operand := range optr.operandVersions {
versionsOutput = append(versionsOutput, fmt.Sprintf("%s: %s", operand.Name, operand.Version))
}
return strings.Join(versionsOutput, ", ")
}
// isInitializing determines if the operator Available condition is still in the initializing
// phase. This means the operator has never reached an available status.
func (optr *Operator) isInitializing() (bool, error) {
co, err := optr.getClusterOperator()
if err != nil {
return false, fmt.Errorf("could not get cluster operator: %w", err)
}
availableCondition := v1helpers.FindStatusCondition(co.Status.Conditions, osconfigv1.OperatorAvailable)
return availableCondition != nil && availableCondition.Reason == string(ReasonInitializing), nil
}