forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
539 lines (418 loc) · 14.8 KB
/
state.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package subscription
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
clientv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/typed/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/comparison"
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubestate"
)
// SubscriptionState describes subscription states.
type SubscriptionState interface {
kubestate.State
isSubscriptionState()
setSubscription(*v1alpha1.Subscription)
Subscription() *v1alpha1.Subscription
Add() SubscriptionExistsState
Update() SubscriptionExistsState
Delete() SubscriptionDeletedState
}
// SubscriptionExistsState describes subscription states in which the subscription exists on the cluster.
type SubscriptionExistsState interface {
SubscriptionState
isSubscriptionExistsState()
}
// SubscriptionAddedState describes subscription states in which the subscription was added to cluster.
type SubscriptionAddedState interface {
SubscriptionExistsState
isSubscriptionAddedState()
}
// SubscriptionUpdatedState describes subscription states in which the subscription was updated in the cluster.
type SubscriptionUpdatedState interface {
SubscriptionExistsState
isSubscriptionUpdatedState()
}
// SubscriptionDeletedState describes subscription states in which the subscription no longer exists and was deleted from the cluster.
type SubscriptionDeletedState interface {
SubscriptionState
isSubscriptionDeletedState()
}
// CatalogHealthState describes subscription states that represent a subscription with respect to catalog health.
type CatalogHealthState interface {
SubscriptionExistsState
isCatalogHealthState()
// UpdateHealth transitions the CatalogHealthState to another CatalogHealthState based on the given subscription catalog health.
// The state's underlying subscription may be updated on the cluster. If the subscription is updated, the resulting state will contain the updated version.
UpdateHealth(now *metav1.Time, client clientv1alpha1.SubscriptionInterface, health ...v1alpha1.SubscriptionCatalogHealth) (CatalogHealthState, error)
}
// CatalogHealthKnownState describes subscription states in which all relevant catalog health is known.
type CatalogHealthKnownState interface {
CatalogHealthState
isCatalogHealthKnownState()
}
// CatalogHealthyState describes subscription states in which all relevant catalogs are known to be healthy.
type CatalogHealthyState interface {
CatalogHealthKnownState
isCatalogHealthyState()
}
// CatalogUnhealthyState describes subscription states in which at least one relevant catalog is known to be unhealthy.
type CatalogUnhealthyState interface {
CatalogHealthKnownState
isCatalogUnhealthyState()
}
// InstallPlanState describes Subscription states with respect to an InstallPlan.
type InstallPlanState interface {
SubscriptionExistsState
isInstallPlanState()
CheckReference() InstallPlanState
}
type NoInstallPlanReferencedState interface {
InstallPlanState
isNoInstallPlanReferencedState()
}
type InstallPlanReferencedState interface {
InstallPlanState
isInstallPlanReferencedState()
InstallPlanNotFound(now *metav1.Time, client clientv1alpha1.SubscriptionInterface) (InstallPlanReferencedState, error)
CheckInstallPlanStatus(now *metav1.Time, client clientv1alpha1.SubscriptionInterface, status *v1alpha1.InstallPlanStatus) (InstallPlanReferencedState, error)
}
type InstallPlanKnownState interface {
InstallPlanReferencedState
isInstallPlanKnownState()
}
type InstallPlanMissingState interface {
InstallPlanKnownState
isInstallPlanMissingState()
}
type InstallPlanPendingState interface {
InstallPlanKnownState
isInstallPlanPendingState()
}
type InstallPlanFailedState interface {
InstallPlanKnownState
isInstallPlanFailedState()
}
type InstallPlanInstalledState interface {
InstallPlanKnownState
isInstallPlanInstalledState()
}
type subscriptionState struct {
kubestate.State
subscription *v1alpha1.Subscription
}
func (s *subscriptionState) isSubscriptionState() {}
func (s *subscriptionState) setSubscription(sub *v1alpha1.Subscription) {
s.subscription = sub
}
func (s *subscriptionState) Subscription() *v1alpha1.Subscription {
return s.subscription
}
func (s *subscriptionState) Add() SubscriptionExistsState {
return &subscriptionAddedState{
SubscriptionExistsState: &subscriptionExistsState{
SubscriptionState: s,
},
}
}
func (s *subscriptionState) Update() SubscriptionExistsState {
return &subscriptionUpdatedState{
SubscriptionExistsState: &subscriptionExistsState{
SubscriptionState: s,
},
}
}
func (s *subscriptionState) Delete() SubscriptionDeletedState {
return &subscriptionDeletedState{
SubscriptionState: s,
}
}
func NewSubscriptionState(sub *v1alpha1.Subscription) SubscriptionState {
return &subscriptionState{
State: kubestate.NewState(),
subscription: sub,
}
}
type subscriptionExistsState struct {
SubscriptionState
}
func (*subscriptionExistsState) isSubscriptionExistsState() {}
type subscriptionAddedState struct {
SubscriptionExistsState
}
func (c *subscriptionAddedState) isSubscriptionAddedState() {}
type subscriptionUpdatedState struct {
SubscriptionExistsState
}
func (c *subscriptionUpdatedState) isSubscriptionUpdatedState() {}
type subscriptionDeletedState struct {
SubscriptionState
}
func (c *subscriptionDeletedState) isSubscriptionDeletedState() {}
type catalogHealthState struct {
SubscriptionExistsState
}
func (c *catalogHealthState) isCatalogHealthState() {}
func (c *catalogHealthState) UpdateHealth(now *metav1.Time, client clientv1alpha1.SubscriptionInterface, catalogHealth ...v1alpha1.SubscriptionCatalogHealth) (CatalogHealthState, error) {
in := c.Subscription()
out := in.DeepCopy()
healthSet := make(map[types.UID]v1alpha1.SubscriptionCatalogHealth, len(catalogHealth))
healthy := true
missingTargeted := true
cond := out.Status.GetCondition(v1alpha1.SubscriptionCatalogSourcesUnhealthy)
for _, h := range catalogHealth {
ref := h.CatalogSourceRef
healthSet[ref.UID] = h
healthy = healthy && h.Healthy
if ref.Namespace == in.Spec.CatalogSourceNamespace && ref.Name == in.Spec.CatalogSource {
missingTargeted = false
if !h.Healthy {
cond.Message = fmt.Sprintf("targeted catalogsource %s/%s unhealthy", ref.Namespace, ref.Name)
}
}
}
var known CatalogHealthKnownState
switch {
case missingTargeted:
healthy = false
cond.Message = fmt.Sprintf("targeted catalogsource %s/%s missing", in.Spec.CatalogSourceNamespace, in.Spec.CatalogSource)
fallthrough
case !healthy:
cond.Status = corev1.ConditionTrue
cond.Reason = v1alpha1.UnhealthyCatalogSourceFound
known = &catalogUnhealthyState{
CatalogHealthKnownState: &catalogHealthKnownState{
CatalogHealthState: c,
},
}
default:
cond.Status = corev1.ConditionFalse
cond.Reason = v1alpha1.AllCatalogSourcesHealthy
cond.Message = "all available catalogsources are healthy"
known = &catalogHealthyState{
CatalogHealthKnownState: &catalogHealthKnownState{
CatalogHealthState: c,
},
}
}
// Check for changes in CatalogHealth
update := true
switch numNew, numOld := len(healthSet), len(in.Status.CatalogHealth); {
case numNew > numOld:
cond.Reason = v1alpha1.CatalogSourcesAdded
case numNew < numOld:
cond.Reason = v1alpha1.CatalogSourcesDeleted
case numNew == 0 && numNew == numOld:
healthy = false
cond.Reason = v1alpha1.NoCatalogSourcesFound
cond.Message = "dependency resolution requires at least one catalogsource"
case numNew == numOld:
// Check against existing subscription
for _, oldHealth := range in.Status.CatalogHealth {
uid := oldHealth.CatalogSourceRef.UID
if newHealth, ok := healthSet[uid]; !ok || !newHealth.Equals(oldHealth) {
cond.Reason = v1alpha1.CatalogSourcesUpdated
break
}
}
fallthrough
default:
update = false
}
if !update && cond.Equals(in.Status.GetCondition(v1alpha1.SubscriptionCatalogSourcesUnhealthy)) {
// Nothing to do, transition to self
return known, nil
}
cond.LastTransitionTime = now
out.Status.LastUpdated = *now
out.Status.SetCondition(cond)
out.Status.CatalogHealth = catalogHealth
updated, err := client.UpdateStatus(context.TODO(), out, metav1.UpdateOptions{})
if err != nil {
// Error occurred, transition to self
return c, err
}
// Inject updated subscription into the state
known.setSubscription(updated)
return known, nil
}
func NewCatalogHealthState(s SubscriptionExistsState) CatalogHealthState {
return &catalogHealthState{
SubscriptionExistsState: s,
}
}
type catalogHealthKnownState struct {
CatalogHealthState
}
func (c *catalogHealthKnownState) isCatalogHealthKnownState() {}
func (c *catalogHealthKnownState) CatalogHealth() []v1alpha1.SubscriptionCatalogHealth {
return c.Subscription().Status.CatalogHealth
}
type catalogHealthyState struct {
CatalogHealthKnownState
}
func (c *catalogHealthyState) isCatalogHealthyState() {}
type catalogUnhealthyState struct {
CatalogHealthKnownState
}
func (c *catalogUnhealthyState) isCatalogUnhealthyState() {}
type installPlanState struct {
SubscriptionExistsState
}
func (i *installPlanState) isInstallPlanState() {}
func (i *installPlanState) CheckReference() InstallPlanState {
if i.Subscription().Status.InstallPlanRef != nil {
return &installPlanReferencedState{
InstallPlanState: i,
}
}
return &noInstallPlanReferencedState{
InstallPlanState: i,
}
}
func newInstallPlanState(s SubscriptionExistsState) InstallPlanState {
return &installPlanState{
SubscriptionExistsState: s,
}
}
type noInstallPlanReferencedState struct {
InstallPlanState
}
func (n *noInstallPlanReferencedState) isNoInstallPlanReferencedState() {}
type installPlanReferencedState struct {
InstallPlanState
}
func (i *installPlanReferencedState) isInstallPlanReferencedState() {}
var hashEqual = comparison.NewHashEqualitor()
func (i *installPlanReferencedState) InstallPlanNotFound(now *metav1.Time, client clientv1alpha1.SubscriptionInterface) (InstallPlanReferencedState, error) {
in := i.Subscription()
out := in.DeepCopy()
// Remove pending and failed conditions
out.Status.RemoveConditions(v1alpha1.SubscriptionInstallPlanPending, v1alpha1.SubscriptionInstallPlanFailed)
// If the installplan is missing when subscription is in pending upgrade,
// clear the installplan ref so the resolution can happen again
if in.Status.State == v1alpha1.SubscriptionStateUpgradePending {
out.Status.InstallPlanRef = nil
out.Status.Install = nil
out.Status.CurrentCSV = ""
out.Status.State = v1alpha1.SubscriptionStateNone
out.Status.LastUpdated = *now
} else {
// Set missing condition to true
cond := out.Status.GetCondition(v1alpha1.SubscriptionInstallPlanMissing)
cond.Status = corev1.ConditionTrue
cond.Reason = v1alpha1.ReferencedInstallPlanNotFound
cond.LastTransitionTime = now
out.Status.SetCondition(cond)
}
// Build missing state
missingState := &installPlanMissingState{
InstallPlanKnownState: &installPlanKnownState{
InstallPlanReferencedState: i,
},
}
// Bail out if the conditions haven't changed (using select fields included in a hash)
if hashEqual(out.Status.Conditions, in.Status.Conditions) {
return missingState, nil
}
// Update the Subscription
out.Status.LastUpdated = *now
updated, err := client.UpdateStatus(context.TODO(), out, metav1.UpdateOptions{})
if err != nil {
return i, err
}
// Stuff updated Subscription into state
missingState.setSubscription(updated)
return missingState, nil
}
func (i *installPlanReferencedState) CheckInstallPlanStatus(now *metav1.Time, client clientv1alpha1.SubscriptionInterface, status *v1alpha1.InstallPlanStatus) (InstallPlanReferencedState, error) {
in := i.Subscription()
out := in.DeepCopy()
// Remove missing, pending, and failed conditions
out.Status.RemoveConditions(v1alpha1.SubscriptionInstallPlanMissing, v1alpha1.SubscriptionInstallPlanPending, v1alpha1.SubscriptionInstallPlanFailed)
// Build and set the InstallPlan condition, if any
cond := v1alpha1.SubscriptionCondition{
Status: corev1.ConditionUnknown,
LastTransitionTime: now,
}
// TODO: Use InstallPlan conditions instead of phases
// Get the status of the InstallPlan and create the appropriate condition and state
var known InstallPlanKnownState
switch phase := status.Phase; phase {
case v1alpha1.InstallPlanPhaseNone:
// Set reason and let the following case fill out the pending condition
cond.Reason = v1alpha1.InstallPlanNotYetReconciled
fallthrough
case v1alpha1.InstallPlanPhasePlanning, v1alpha1.InstallPlanPhaseInstalling, v1alpha1.InstallPlanPhaseRequiresApproval:
if cond.Reason == "" {
cond.Reason = string(phase)
}
cond.Type = v1alpha1.SubscriptionInstallPlanPending
cond.Status = corev1.ConditionTrue
out.Status.SetCondition(cond)
// Build pending state
known = &installPlanPendingState{
InstallPlanKnownState: &installPlanKnownState{
InstallPlanReferencedState: i,
},
}
case v1alpha1.InstallPlanPhaseFailed:
// Attempt to project reason from failed InstallPlan condition
if installedCond := status.GetCondition(v1alpha1.InstallPlanInstalled); installedCond.Status == corev1.ConditionFalse {
cond.Reason = string(installedCond.Reason)
} else {
cond.Reason = v1alpha1.InstallPlanFailed
}
cond.Type = v1alpha1.SubscriptionInstallPlanFailed
cond.Status = corev1.ConditionTrue
out.Status.SetCondition(cond)
// Build failed state
known = &installPlanFailedState{
InstallPlanKnownState: &installPlanKnownState{
InstallPlanReferencedState: i,
},
}
default:
// Build installed state
known = &installPlanInstalledState{
InstallPlanKnownState: &installPlanKnownState{
InstallPlanReferencedState: i,
},
}
}
// Bail out if the conditions haven't changed (using select fields included in a hash)
if hashEqual(out.Status.Conditions, in.Status.Conditions) {
return known, nil
}
// Update the Subscription
out.Status.LastUpdated = *now
updated, err := client.UpdateStatus(context.TODO(), out, metav1.UpdateOptions{})
if err != nil {
return i, err
}
// Stuff updated Subscription into state
known.setSubscription(updated)
return known, nil
}
type installPlanKnownState struct {
InstallPlanReferencedState
}
func (i *installPlanKnownState) isInstallPlanKnownState() {}
type installPlanMissingState struct {
InstallPlanKnownState
}
func (i *installPlanMissingState) isInstallPlanMissingState() {}
type installPlanPendingState struct {
InstallPlanKnownState
}
func (i *installPlanPendingState) isInstallPlanPendingState() {}
type installPlanFailedState struct {
InstallPlanKnownState
}
func (i *installPlanFailedState) isInstallPlanFailedState() {}
type installPlanInstalledState struct {
InstallPlanKnownState
}
func (i *installPlanInstalledState) isInstallPlanInstalledState() {}