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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package materialiser
import (
"context"
"errors"
"fmt"
"time"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/identitybundle"
"github.com/johnandersen777/socialweb-computer-kcp/internal/portallocator"
"github.com/johnandersen777/socialweb-computer-kcp/internal/procgroup"
"github.com/johnandersen777/socialweb-computer-kcp/internal/revocation"
"github.com/johnandersen777/socialweb-computer-kcp/internal/subnetallocator"
)
const ConditionReady = "Ready"
const PathGateGrace = 10 * time.Second
const (
ReasonInitializing = "Initializing"
ReasonInitTimeout = "InitTimeout"
ReasonDraining = "Draining"
ReasonBundleFailed = "IdentityBundleVerifyFailed"
ReasonBundleHalted = "IdentityVerificationFailed"
ReasonAwaitPathShut = "AwaitTenantPathShut"
ReasonStopped = "Stopped"
ReasonTeardownStuck = "TeardownGateTimeout"
ReasonSessionCap = "SessionCapRefuses"
ReasonUp = "ProcessGroupServing"
ReasonSettling = "AwaitRevocationSettled"
ReasonUnhealthy = "MembersUnhealthy"
)
const ProbeRetry = 2 * time.Second
const GateTimeout = 10 * time.Second
const DefaultInitTimeout = 3 * time.Minute
type Op string
const (
OpReap Op = "reap"
OpAllocatePorts Op = "allocate-ports"
OpSpawn Op = "spawn"
OpIdentityHalted Op = "identity-halted"
OpSpawnFailed Op = "spawn-failed"
OpParked Op = "park-unavailable"
OpShutPath Op = "shut-tenant-path"
OpAwaitPathShut Op = "await-tenant-path-shut"
OpPathShut Op = "tenant-path-shut"
OpReleasePorts Op = "release-ports"
OpReleaseSubnet Op = "release-subnet"
OpDropFinalizer Op = "remove-finalizer"
OpBlocked Op = "teardown-blocked-nothing-destroyed"
)
type PathProbe struct {
Answered bool
Status int
Err string
}
func (p PathProbe) String() string {
if p.Answered {
return fmt.Sprintf("the tenant front answered %d", p.Status)
}
if p.Err == "" {
return "the tenant front was not probed"
}
return "the tenant front did not answer: " + p.Err
}
type Observed struct {
Cluster v1alpha1.Cluster
Path PathProbe
Reachable bool
IdentityState v1alpha1.IdentityState
ServiceAccountPresent bool
Now time.Time
}
type Result struct {
Phase v1alpha1.ClusterPhase
URL string
Instance string
Conditions []metav1.Condition
Alert string
Ops []Op
RequeueAfter time.Duration
Again bool
RemoveFinalizer bool
Halted bool
}
type Leases map[portallocator.Purpose]portallocator.Lease
type StepBuilder func(o Observed, l Leases) ([]procgroup.Step, error)
type IdentityGuard func(ctx context.Context, o Observed) error
type Host interface {
Live(ctx context.Context, cluster string) bool
Spawn(ctx context.Context, cluster string, steps []procgroup.Step) error
Reap(ctx context.Context, cluster string) error
Unhealthy(ctx context.Context, cluster string) ([]string, error)
}
type Options struct {
Host Host
Ports *portallocator.Allocator
Networks *subnetallocator.Allocator
Steps StepBuilder
Front func(ctx context.Context, cluster string) error
Identity IdentityGuard
Alert func(cluster, message string)
NewInstance func() string
Now func() time.Time
InitTimeout time.Duration
GateTimeout time.Duration
Purposes []portallocator.Purpose
}
type Reconciler struct {
opts Options
}
var DefaultPurposes = []portallocator.Purpose{
portallocator.PurposePostgres,
portallocator.PurposeKine,
portallocator.PurposeK3sSecure,
portallocator.PurposeK3sSlotA,
portallocator.PurposeK3sControllerManager,
portallocator.PurposeK3sScheduler,
portallocator.PurposeK3sCloudControllerManager,
}
func New(opts Options) (*Reconciler, error) {
if opts.Host == nil {
return nil, errors.New("materialiser: Host is required")
}
if opts.Ports == nil {
return nil, errors.New("materialiser: Ports is required; the lease file is the authority")
}
if opts.Networks == nil {
return nil, errors.New("materialiser: Networks is required. A cluster's range is leased " +
"for as long as the cluster is, exactly as its ports are -- the range holds the " +
"gateway its guests dial the apiserver through, which is baked into k3s's " +
"advertise-address and its certificate -- so a teardown that dropped the ports and " +
"kept the range would hold a range per deleted cluster until the pool ran out, and one " +
"that dropped the range would renumber a cluster whose CR was recreated")
}
if opts.Identity == nil {
return nil, errors.New("materialiser: Identity is required; the sha256 pre-check has no substitute")
}
if opts.NewInstance == nil {
return nil, errors.New("materialiser: NewInstance is required; status.instance is the only discriminator there is")
}
if opts.Steps == nil {
return nil, errors.New("materialiser: Steps is required")
}
if opts.Now == nil {
opts.Now = time.Now
}
if opts.InitTimeout == 0 {
opts.InitTimeout = DefaultInitTimeout
}
if opts.GateTimeout == 0 {
opts.GateTimeout = GateTimeout
}
if opts.Purposes == nil {
opts.Purposes = DefaultPurposes
}
return &Reconciler{opts: opts}, nil
}
func (r *Reconciler) Reconcile(ctx context.Context, o Observed) (Result, error) {
if o.Cluster.Name == "" {
return Result{}, errors.New("materialiser: observed Cluster has no name")
}
if o.Now.IsZero() {
o.Now = r.opts.Now()
}
if o.Cluster.DeletionTimestamp != nil {
return r.teardown(ctx, o)
}
if !o.Cluster.Spec.Running {
return r.off(ctx, o)
}
if halted(o.Cluster.Status.Conditions) {
return Result{
Phase: v1alpha1.ClusterUnavailable,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
Ops: []Op{OpIdentityHalted},
}, nil
}
return r.on(ctx, o)
}
func StartupOwns(c v1alpha1.Cluster) bool {
return c.Spec.Running && c.DeletionTimestamp == nil && !halted(c.Status.Conditions)
}
func (r *Reconciler) off(ctx context.Context, o Observed) (Result, error) {
res := Result{
Phase: v1alpha1.ClusterUnavailable,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
}
if !shut(o) {
res.Ops = []Op{OpShutPath, OpAwaitPathShut}
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonAwaitPathShut,
Message: "phase is Unavailable; waiting for the front to stop admitting tenant requests before reaping",
ObservedGeneration: o.Cluster.Generation,
})
res.RequeueAfter = shutIn(o)
return res, nil
}
res.Ops = []Op{OpPathShut, OpReap}
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: v1alpha1.ConditionTenantPathShut,
Status: metav1.ConditionTrue,
Reason: ReasonAwaitPathShut,
Message: "the front admits no tenant request; " + o.Path.String(),
ObservedGeneration: o.Cluster.Generation,
})
if err := r.opts.Host.Reap(ctx, o.Cluster.Name); err != nil {
return res, err
}
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonStopped,
Message: "spec.running is false; the front admits no tenant request and the members are reaped",
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
func (r *Reconciler) teardown(ctx context.Context, o Observed) (Result, error) {
res := Result{
Phase: v1alpha1.ClusterDraining,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
}
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonDraining,
Message: "teardown in progress; the tenant path is shut before anything is destroyed",
ObservedGeneration: o.Cluster.Generation,
})
if refuse, why := r.sessionCapRefuses(o); refuse {
res.Ops = append(res.Ops, OpBlocked)
res.Alert = why
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: v1alpha1.ConditionTeardownBlocked,
Status: metav1.ConditionTrue,
Reason: ReasonSessionCap,
Message: why,
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
if !shut(o) {
res.Ops = append(res.Ops, OpShutPath, OpAwaitPathShut)
if o.Now.Sub(drainingSince(o, r.opts.Now())) >= r.opts.GateTimeout {
res.Ops = append(res.Ops, OpBlocked)
res.Alert = "teardown gate timed out; nothing is destroyed. Stuck-and-billing beats silent loss"
res.RequeueAfter = shutIn(o)
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: v1alpha1.ConditionTeardownBlocked,
Status: metav1.ConditionTrue,
Reason: ReasonTeardownStuck,
Message: fmt.Sprintf("the tenant path gate is not shut after %s; %s", r.opts.GateTimeout, o.Path),
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
res.RequeueAfter = shutIn(o)
return res, nil
}
res.Ops = append(res.Ops, OpPathShut, OpReap)
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: v1alpha1.ConditionTenantPathShut,
Status: metav1.ConditionTrue,
Reason: ReasonAwaitPathShut,
Message: "the front admits no tenant request; " + o.Path.String(),
ObservedGeneration: o.Cluster.Generation,
})
if err := r.opts.Host.Reap(ctx, o.Cluster.Name); err != nil {
return res, err
}
res.Ops = append(res.Ops, OpReleasePorts)
if err := r.opts.Ports.Release(o.Cluster.Name); err != nil {
return res, err
}
res.Ops = append(res.Ops, OpReleaseSubnet)
if err := r.opts.Networks.Release(o.Cluster.Name); err != nil {
return res, err
}
res.Ops = append(res.Ops, OpDropFinalizer)
res.RemoveFinalizer = true
return res, nil
}
func (r *Reconciler) on(ctx context.Context, o Observed) (Result, error) {
live := r.opts.Host.Live(ctx, o.Cluster.Name)
if live {
sick, err := r.opts.Host.Unhealthy(ctx, o.Cluster.Name)
if err != nil {
return r.pending(o, "the members of this cluster could not be read: "+err.Error())
}
if len(sick) > 0 {
if readyTrue(o) {
return r.degraded(o, sick)
}
return r.bringUp(ctx, o)
}
}
if live && o.Reachable {
res := Result{
Phase: o.Cluster.Status.Phase,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
}
if o.Cluster.Status.Phase != settledReadyPhase {
return r.claimReady(res, o)
}
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionTrue,
Reason: ReasonUp,
Message: "the apiserver answered",
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
if r.opts.Host.Live(ctx, o.Cluster.Name) {
return r.pending(o, "the process group is up and the apiserver has not answered yet")
}
return r.bringUp(ctx, o)
}
func (r *Reconciler) claimReady(res Result, o Observed) (Result, error) {
settled, err := revocation.WakeSettled(wakeEffect(o))
if err != nil {
res.Phase = v1alpha1.ClusterInitializing
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonSettling,
Message: err.Error(),
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
phase, err := revocation.Ready(settled)
if err != nil {
res.Phase = v1alpha1.ClusterInitializing
return res, nil
}
res.Phase = phase
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionTrue,
Reason: ReasonUp,
Message: "the apiserver answered and the revocation reconcile is settled",
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
func wakeEffect(o Observed) revocation.Effect {
if revocation.Revoked(o.IdentityState) && o.ServiceAccountPresent {
return revocation.Effect{Cluster: o.Cluster.Name, State: v1alpha1.IdentityRevocationPending, DeleteSA: true}
}
return revocation.Effect{Cluster: o.Cluster.Name, State: v1alpha1.IdentityRevoked}
}
var settledReadyPhase = func() v1alpha1.ClusterPhase {
settled, err := revocation.WakeSettled(revocation.Effect{State: v1alpha1.IdentityRevoked})
if err != nil {
panic("materialiser: revocation.WakeSettled refused a terminal effect: " + err.Error())
}
phase, err := revocation.Ready(settled)
if err != nil {
panic("materialiser: revocation.Ready refused a Settled: " + err.Error())
}
return phase
}()
func (r *Reconciler) bringUp(ctx context.Context, o Observed) (Result, error) {
res := Result{
Phase: v1alpha1.ClusterInitializing,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
}
fresh := newInitWindow(o)
if fresh {
meta.RemoveStatusCondition(&res.Conditions, ConditionReady)
}
meta.RemoveStatusCondition(&res.Conditions, v1alpha1.ConditionTenantPathShut)
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonInitializing,
Message: "bringing up embedded-postgres -> openbao -> kine -> k3s",
LastTransitionTime: metav1.NewTime(o.Now),
ObservedGeneration: o.Cluster.Generation,
})
if !fresh && o.Cluster.Status.Phase == v1alpha1.ClusterInitializing {
if since := o.Now.Sub(readySince(o, o.Now)); since >= r.opts.InitTimeout {
return r.park(res, o, fmt.Sprintf("still Initializing after %s; parking in Unavailable", since))
}
}
instance := res.Instance
if instance == "" {
instance = r.opts.NewInstance()
res.Instance = instance
}
if r.opts.Front != nil {
if err := r.opts.Front(ctx, o.Cluster.Name); err != nil {
return res, fmt.Errorf("materialiser: the apiserver front of %s holds the cluster's ports and did not come up: %w", o.Cluster.Name, err)
}
}
leases := Leases{}
for _, p := range r.opts.Purposes {
l, err := r.opts.Ports.Allocate(ctx, portallocator.Request{
Cluster: o.Cluster.Name,
Account: o.Cluster.Spec.Account,
Instance: instance,
Purpose: p,
})
if err != nil {
return res, err
}
leases[p] = l
}
res.Ops = append(res.Ops, OpAllocatePorts)
if api, ok := leases[portallocator.PurposeK3sSecure]; ok {
res.URL = mountURL(api.Port)
}
steps, err := r.opts.Steps(o, leases)
if err != nil {
return res, err
}
steps = guardBeforeK3s(steps, r.opts.Identity, o)
if err := r.opts.Host.Spawn(ctx, o.Cluster.Name, steps); err != nil {
if isBundleFailure(err) {
res.Phase = v1alpha1.ClusterUnavailable
res.Halted = true
res.Ops = append(res.Ops, OpIdentityHalted)
res.Alert = fmt.Sprintf("identity bundle failed verification: %v. Start is halted, not retried", err)
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: v1alpha1.ConditionIdentityVerificationFailed,
Status: metav1.ConditionTrue,
Reason: ReasonBundleHalted,
Message: err.Error(),
LastTransitionTime: metav1.NewTime(o.Now),
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
res.Ops = append(res.Ops, OpSpawnFailed)
res.Alert = fmt.Sprintf("the process group did not come up: %v. A spawn failure that "+
"reports only that it happened leaves the readiness gate's own reason -- and the "+
"component's log under the cluster's data dir -- as the only account of it", err)
return res, nil
}
res.Ops = append(res.Ops, OpSpawn)
if !o.Reachable {
res.RequeueAfter = r.probeIn(o)
return res, nil
}
return r.claimReady(res, o)
}
func readyTrue(o Observed) bool {
c := meta.FindStatusCondition(o.Cluster.Status.Conditions, ConditionReady)
return c != nil && c.Status == metav1.ConditionTrue
}
func (r *Reconciler) degraded(o Observed, sick []string) (Result, error) {
res := Result{
Phase: v1alpha1.ClusterInitializing,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
Again: true,
}
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonUnhealthy,
Message: fmt.Sprintf("not every member of the cluster's slice is running and verified; the next pass re-places them: %v", sick),
LastTransitionTime: metav1.NewTime(o.Now),
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
func (r *Reconciler) pending(o Observed, msg string) (Result, error) {
res := Result{
Phase: v1alpha1.ClusterInitializing,
URL: o.Cluster.Status.URL,
Instance: o.Cluster.Status.Instance,
Conditions: carry(o.Cluster.Status.Conditions),
}
fresh := newInitWindow(o)
if fresh {
meta.RemoveStatusCondition(&res.Conditions, ConditionReady)
} else if o.Cluster.Status.Phase == v1alpha1.ClusterInitializing {
if since := o.Now.Sub(readySince(o, o.Now)); since >= r.opts.InitTimeout {
return r.park(res, o, fmt.Sprintf("still Initializing after %s; parking in Unavailable", since))
}
}
res.RequeueAfter = r.probeIn(o)
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonInitializing,
Message: msg,
LastTransitionTime: metav1.NewTime(o.Now),
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
func (r *Reconciler) park(res Result, o Observed, msg string) (Result, error) {
res.Phase = v1alpha1.ClusterUnavailable
res.Ops = append(res.Ops, OpParked)
res.Alert = msg
res.RequeueAfter = 0
meta.SetStatusCondition(&res.Conditions, metav1.Condition{
Type: ConditionReady,
Status: metav1.ConditionFalse,
Reason: ReasonInitTimeout,
Message: msg,
ObservedGeneration: o.Cluster.Generation,
})
return res, nil
}
func (r *Reconciler) sessionCapRefuses(o Observed) (bool, string) {
e := o.Cluster.Status.NodeExpiry
if e == nil {
if c := meta.FindStatusCondition(o.Cluster.Status.Conditions, v1alpha1.ConditionReachable); c == nil || c.Status != metav1.ConditionTrue {
return false, ""
}
return true, "status.nodeExpiry is unknown and the apiserver has answered; refusing to tear down against a gate that cannot compare"
}
if *e == 0 {
return false, ""
}
if o.Now.Add(r.opts.GateTimeout).Unix() >= *e {
return true, fmt.Sprintf("the gate would outlive status.nodeExpiry (%d); refusing and re-provisioning", *e)
}
return false, ""
}
func shutIn(o Observed) time.Duration {
since, closed := gateClosedSince(o)
if !closed {
return PathGateGrace
}
if left := since.Add(PathGateGrace).Sub(o.Now); left > 0 {
return left
}
return PathGateGrace
}
func (r *Reconciler) probeIn(o Observed) time.Duration {
left := readySince(o, o.Now).Add(r.opts.InitTimeout).Sub(o.Now)
if left > 0 && left < ProbeRetry {
return left
}
return ProbeRetry
}
func shut(o Observed) bool {
since, closed := gateClosedSince(o)
return closed && o.Now.Sub(since) >= PathGateGrace
}
func gateClosedSince(o Observed) (time.Time, bool) {
var since time.Time
closed := false
if d := o.Cluster.DeletionTimestamp; d != nil {
since, closed = d.Time, true
}
if o.Cluster.Status.Phase == v1alpha1.ClusterReady {
return since, closed
}
c := meta.FindStatusCondition(o.Cluster.Status.Conditions, ConditionReady)
switch {
case c == nil:
return time.Time{}, true
case c.Status == metav1.ConditionTrue:
return since, closed
case c.LastTransitionTime.IsZero():
return time.Time{}, true
case !closed || c.LastTransitionTime.Time.Before(since):
return c.LastTransitionTime.Time, true
}
return since, closed
}
func newInitWindow(o Observed) bool {
c := meta.FindStatusCondition(o.Cluster.Status.Conditions, ConditionReady)
if c == nil || c.Status == metav1.ConditionTrue {
return false
}
return c.Reason == ReasonStopped || c.ObservedGeneration != o.Cluster.Generation
}
func readySince(o Observed, fallback time.Time) time.Time {
c := meta.FindStatusCondition(o.Cluster.Status.Conditions, ConditionReady)
if c == nil || c.LastTransitionTime.IsZero() {
return fallback
}
return c.LastTransitionTime.Time
}
func drainingSince(o Observed, fallback time.Time) time.Time {
c := meta.FindStatusCondition(o.Cluster.Status.Conditions, ConditionReady)
if c == nil || c.Reason != ReasonDraining || c.LastTransitionTime.IsZero() {
return fallback
}
return c.LastTransitionTime.Time
}
func halted(conds []metav1.Condition) bool {
c := meta.FindStatusCondition(conds, v1alpha1.ConditionIdentityVerificationFailed)
return c != nil && c.Status == metav1.ConditionTrue
}
func carry(conds []metav1.Condition) []metav1.Condition {
out := make([]metav1.Condition, len(conds))
copy(out, conds)
return out
}
func isBundleFailure(err error) bool {
return errors.Is(err, identitybundle.ErrMismatch) || errors.Is(err, identitybundle.ErrIncomplete)
}
func mountURL(advertisePort int) string {
return fmt.Sprintf("https://127.0.0.1:%d", advertisePort)
}
func guardBeforeK3s(steps []procgroup.Step, guard IdentityGuard, o Observed) []procgroup.Step {
if guard == nil {
return steps
}
for i := range steps {
if steps[i].Name != procgroup.ComponentK3s {
continue
}
inner := steps[i].Prepare
steps[i].Prepare = func(ctx context.Context, ps procgroup.Spec) (procgroup.Spec, error) {
if err := guard(ctx, o); err != nil {
return ps, err
}
if inner == nil {
return ps, nil
}
return inner(ctx, ps)
}
}
return steps
}