-
Notifications
You must be signed in to change notification settings - Fork 23
/
controller.go
832 lines (763 loc) · 40.3 KB
/
controller.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
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
// Package clusterresourceplacement features a controller to reconcile the clusterResourcePlacement changes.
package clusterresourceplacement
import (
"context"
"crypto/sha256"
"fmt"
"sort"
"strconv"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
"go.goms.io/fleet/pkg/utils"
"go.goms.io/fleet/pkg/utils/controller"
)
const (
// ApplyFailedReason is the reason string of placement condition when the selected resources fail to apply.
ApplyFailedReason = "ApplyFailed"
// ApplyPendingReason is the reason string of placement condition when the selected resources are pending to apply.
ApplyPendingReason = "ApplyPending"
// ApplySucceededReason is the reason string of placement condition when the selected resources are applied successfully.
ApplySucceededReason = "ApplySucceeded"
resourcePlacementConditionScheduleFailedMessageFormat = "%s is not selected: %s"
resourcePlacementConditionScheduleFailedWithScoreMessageFormat = "%s is not selected with clusterScore %+v: %s"
resourcePlacementConditionScheduleSucceededMessageFormat = "Successfully scheduled resources for placement in %s: %s"
resourcePlacementConditionScheduleSucceededWithScoreMessageFormat = "Successfully scheduled resources for placement in %s with clusterScore %+v: %s"
)
func (r *Reconciler) Reconcile(ctx context.Context, _ controller.QueueKey) (ctrl.Result, error) {
// TODO workaround to bypass lint check
return r.handleUpdate(ctx, nil)
}
func (r *Reconciler) handleDelete(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) (ctrl.Result, error) {
crpKObj := klog.KObj(crp)
if !controllerutil.ContainsFinalizer(crp, fleetv1beta1.ClusterResourcePlacementCleanupFinalizer) {
klog.V(4).InfoS("clusterResourcePlacement is being deleted and no cleanup work needs to be done", "clusterResourcePlacement", crpKObj)
return ctrl.Result{}, nil
}
klog.V(2).InfoS("Removing snapshots created by clusterResourcePlacement", "clusterResourcePlacement", crpKObj)
if err := r.deleteClusterSchedulingPolicySnapshots(ctx, crp); err != nil {
return ctrl.Result{}, err
}
if err := r.deleteClusterResourceSnapshots(ctx, crp); err != nil {
return ctrl.Result{}, err
}
controllerutil.RemoveFinalizer(crp, fleetv1beta1.ClusterResourcePlacementCleanupFinalizer)
if err := r.Client.Update(ctx, crp); err != nil {
klog.ErrorS(err, "Failed to remove crp finalizer", "clusterResourcePlacement", crpKObj)
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *Reconciler) deleteClusterSchedulingPolicySnapshots(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) error {
snapshotList := &fleetv1beta1.ClusterSchedulingPolicySnapshotList{}
crpKObj := klog.KObj(crp)
if err := r.UncachedReader.List(ctx, snapshotList, client.MatchingLabels{fleetv1beta1.CRPTrackingLabel: crp.Name}); err != nil {
klog.ErrorS(err, "Failed to list all clusterSchedulingPolicySnapshots", "clusterResourcePlacement", crpKObj)
return controller.NewAPIServerError(false, err)
}
for i := range snapshotList.Items {
if err := r.Client.Delete(ctx, &snapshotList.Items[i]); err != nil && !errors.IsNotFound(err) {
klog.ErrorS(err, "Failed to delete clusterSchedulingPolicySnapshot", "clusterResourcePlacement", crpKObj, "clusterSchedulingPolicySnapshot", klog.KObj(&snapshotList.Items[i]))
return controller.NewAPIServerError(false, err)
}
}
klog.V(2).InfoS("Deleted clusterSchedulingPolicySnapshots", "clusterResourcePlacement", crpKObj, "numberOfSnapshots", len(snapshotList.Items))
return nil
}
func (r *Reconciler) deleteClusterResourceSnapshots(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) error {
snapshotList := &fleetv1beta1.ClusterResourceSnapshotList{}
crpKObj := klog.KObj(crp)
if err := r.UncachedReader.List(ctx, snapshotList, client.MatchingLabels{fleetv1beta1.CRPTrackingLabel: crp.Name}); err != nil {
klog.ErrorS(err, "Failed to list all clusterResourceSnapshots", "clusterResourcePlacement", crpKObj)
return controller.NewAPIServerError(false, err)
}
for i := range snapshotList.Items {
if err := r.Client.Delete(ctx, &snapshotList.Items[i]); err != nil && !errors.IsNotFound(err) {
klog.ErrorS(err, "Failed to delete clusterResourceSnapshots", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", klog.KObj(&snapshotList.Items[i]))
return controller.NewAPIServerError(false, err)
}
}
klog.V(2).InfoS("Deleted clusterResourceSnapshots", "clusterResourcePlacement", crpKObj, "numberOfSnapshots", len(snapshotList.Items))
return nil
}
// handleUpdate handles the create/update clusterResourcePlacement event.
// It creates corresponding clusterSchedulingPolicySnapshot and clusterResourceSnapshot if needed and updates the status based on
// clusterSchedulingPolicySnapshot status and work status.
// If the error type is ErrUnexpectedBehavior, the controller will skip the reconciling.
func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) (ctrl.Result, error) {
revisionLimit := fleetv1beta1.RevisionHistoryLimitDefaultValue
if crp.Spec.RevisionHistoryLimit != nil {
revisionLimit = *crp.Spec.RevisionHistoryLimit
if revisionLimit <= 0 {
err := fmt.Errorf("invalid clusterResourcePlacement %s: invalid revisionHistoryLimit %d", crp.Name, revisionLimit)
klog.ErrorS(controller.NewUnexpectedBehaviorError(err), "Invalid revisionHistoryLimit value and using default value instead", "clusterResourcePlacement", klog.KObj(crp))
// use the default value instead
revisionLimit = fleetv1beta1.RevisionHistoryLimitDefaultValue
}
}
_, err := r.getOrCreateClusterSchedulingPolicySnapshot(ctx, crp, int(revisionLimit))
if err != nil {
return ctrl.Result{}, err
}
selectedResources, err := r.selectResourcesForPlacement(crp)
if err != nil {
return ctrl.Result{}, err
}
resourceSnapshotSpec := fleetv1beta1.ResourceSnapshotSpec{
SelectedResources: selectedResources,
}
_, err = r.getOrCreateClusterResourceSnapshot(ctx, crp, &resourceSnapshotSpec, int(revisionLimit))
if err != nil {
return ctrl.Result{}, err
}
// update the status based on the latestPolicySnapshot status
// update the status based on the work
return ctrl.Result{}, nil
}
func (r *Reconciler) getOrCreateClusterSchedulingPolicySnapshot(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, revisionHistoryLimit int) (*fleetv1beta1.ClusterSchedulingPolicySnapshot, error) {
crpKObj := klog.KObj(crp)
schedulingPolicy := crp.Spec.Policy.DeepCopy()
if schedulingPolicy != nil {
schedulingPolicy.NumberOfClusters = nil // will exclude the numberOfClusters
}
policyHash, err := generatePolicyHash(schedulingPolicy)
if err != nil {
klog.ErrorS(err, "Failed to generate policy hash of crp", "clusterResourcePlacement", crpKObj)
return nil, controller.NewUnexpectedBehaviorError(err)
}
// latestPolicySnapshotIndex should be -1 when there is no snapshot.
latestPolicySnapshot, latestPolicySnapshotIndex, err := r.lookupLatestClusterSchedulingPolicySnapshot(ctx, crp)
if err != nil {
return nil, err
}
if latestPolicySnapshot != nil && string(latestPolicySnapshot.Spec.PolicyHash) == policyHash {
if err := r.ensureLatestPolicySnapshot(ctx, crp, latestPolicySnapshot); err != nil {
return nil, err
}
return latestPolicySnapshot, nil
}
// Need to create new snapshot when 1) there is no snapshots or 2) the latest snapshot hash != current one.
// mark the last policy snapshot as inactive if it is different from what we have now
if latestPolicySnapshot != nil &&
string(latestPolicySnapshot.Spec.PolicyHash) != policyHash &&
latestPolicySnapshot.Labels[fleetv1beta1.IsLatestSnapshotLabel] == strconv.FormatBool(true) {
// set the latest label to false first to make sure there is only one or none active policy snapshot
latestPolicySnapshot.Labels[fleetv1beta1.IsLatestSnapshotLabel] = strconv.FormatBool(false)
if err := r.Client.Update(ctx, latestPolicySnapshot); err != nil {
klog.ErrorS(err, "Failed to set the isLatestSnapshot label to false", "clusterResourcePlacement", crpKObj, "clusterSchedulingPolicySnapshot", klog.KObj(latestPolicySnapshot))
return nil, controller.NewAPIServerError(false, err)
}
}
// delete redundant snapshot revisions before creating a new snapshot to guarantee that the number of snapshots
// won't exceed the limit.
if err := r.deleteRedundantSchedulingPolicySnapshots(ctx, crp, revisionHistoryLimit); err != nil {
return nil, err
}
// create a new policy snapshot
latestPolicySnapshotIndex++
latestPolicySnapshot = &fleetv1beta1.ClusterSchedulingPolicySnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(fleetv1beta1.PolicySnapshotNameFmt, crp.Name, latestPolicySnapshotIndex),
Labels: map[string]string{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
fleetv1beta1.PolicyIndexLabel: strconv.Itoa(latestPolicySnapshotIndex),
},
},
Spec: fleetv1beta1.SchedulingPolicySnapshotSpec{
Policy: schedulingPolicy,
PolicyHash: []byte(policyHash),
},
}
policySnapshotKObj := klog.KObj(latestPolicySnapshot)
if err := controllerutil.SetControllerReference(crp, latestPolicySnapshot, r.Scheme); err != nil {
klog.ErrorS(err, "Failed to set owner reference", "clusterSchedulingPolicySnapshot", policySnapshotKObj)
// should never happen
return nil, controller.NewUnexpectedBehaviorError(err)
}
// make sure each policySnapshot should always have the annotation if CRP is selectN type
if crp.Spec.Policy != nil &&
crp.Spec.Policy.PlacementType == fleetv1beta1.PickNPlacementType &&
crp.Spec.Policy.NumberOfClusters != nil {
latestPolicySnapshot.Annotations = map[string]string{
fleetv1beta1.NumberOfClustersAnnotation: strconv.Itoa(int(*crp.Spec.Policy.NumberOfClusters)),
}
}
if err := r.Client.Create(ctx, latestPolicySnapshot); err != nil {
klog.ErrorS(err, "Failed to create new clusterSchedulingPolicySnapshot", "clusterSchedulingPolicySnapshot", policySnapshotKObj)
return nil, controller.NewAPIServerError(false, err)
}
return latestPolicySnapshot, nil
}
func (r *Reconciler) deleteRedundantSchedulingPolicySnapshots(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, revisionHistoryLimit int) error {
sortedList, err := r.listSortedClusterSchedulingPolicySnapshots(ctx, crp)
if err != nil {
return err
}
if len(sortedList.Items) < revisionHistoryLimit {
return nil
}
if len(sortedList.Items)-revisionHistoryLimit > 0 {
// We always delete before creating a new snapshot, the snapshot size should never exceed the limit as there is
// no finalizer added and object should be deleted immediately.
klog.Warningf("The number of clusterSchedulingPolicySnapshots exceeds the revisionHistoryLimit and it should never happen", "clusterResourcePlacement", klog.KObj(crp), "numberOfSnapshots", len(sortedList.Items), "revisionHistoryLimit", revisionHistoryLimit)
}
// In normal situation, The max of len(sortedList) should be revisionHistoryLimit.
// We just need to delete one policySnapshot before creating a new one.
// As a result of defensive programming, it will delete any redundant snapshots which could be more than one.
for i := 0; i <= len(sortedList.Items)-revisionHistoryLimit; i++ { // need to reserve one slot for the new snapshot
if err := r.Client.Delete(ctx, &sortedList.Items[i]); err != nil && !errors.IsNotFound(err) {
klog.ErrorS(err, "Failed to delete clusterSchedulingPolicySnapshot", "clusterResourcePlacement", klog.KObj(crp), "clusterSchedulingPolicySnapshot", klog.KObj(&sortedList.Items[i]))
return controller.NewAPIServerError(false, err)
}
}
return nil
}
// deleteRedundantResourceSnapshots handles multiple snapshots in a group.
func (r *Reconciler) deleteRedundantResourceSnapshots(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, revisionHistoryLimit int) error {
sortedList, err := r.listSortedResourceSnapshots(ctx, crp)
if err != nil {
return err
}
if len(sortedList.Items) < revisionHistoryLimit {
// If the number of existing snapshots is less than the limit no matter how many snapshots in a group, we don't
// need to delete any snapshots.
// Skip the checking and deleting.
return nil
}
crpKObj := klog.KObj(crp)
lastGroupIndex := -1
groupCounter := 0
// delete the snapshots from the end as there are could be multiple snapshots in a group in order to keep the latest
// snapshots from the end.
for i := len(sortedList.Items) - 1; i >= 0; i-- {
snapshotKObj := klog.KObj(&sortedList.Items[i])
ii, err := parseResourceIndexFromLabel(&sortedList.Items[i])
if err != nil {
klog.ErrorS(err, "Failed to parse the resource index label", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", snapshotKObj)
return controller.NewUnexpectedBehaviorError(err)
}
if ii != lastGroupIndex {
groupCounter++
lastGroupIndex = ii
}
if groupCounter < revisionHistoryLimit { // need to reserve one slot for the new snapshot
// When the number of group is less than the revision limit, skipping deleting the snapshot.
continue
}
if err := r.Client.Delete(ctx, &sortedList.Items[i]); err != nil && !errors.IsNotFound(err) {
klog.ErrorS(err, "Failed to delete clusterResourceSnapshot", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", snapshotKObj)
return controller.NewAPIServerError(false, err)
}
}
if groupCounter-revisionHistoryLimit > 0 {
// We always delete before creating a new snapshot, the snapshot group size should never exceed the limit
// as there is no finalizer added and the object should be deleted immediately.
klog.Warningf("The number of clusterResourceSnapshot groups exceeds the revisionHistoryLimit and it should never happen", "clusterResourcePlacement", klog.KObj(crp), "numberOfSnapshotGroups", groupCounter, "revisionHistoryLimit", revisionHistoryLimit)
}
return nil
}
// TODO handle all the resources selected by placement larger than 1MB size limit of k8s objects.
func (r *Reconciler) getOrCreateClusterResourceSnapshot(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, resourceSnapshotSpec *fleetv1beta1.ResourceSnapshotSpec, revisionHistoryLimit int) (*fleetv1beta1.ClusterResourceSnapshot, error) {
resourceHash, err := generateResourceHash(resourceSnapshotSpec)
if err != nil {
klog.ErrorS(err, "Failed to generate resource hash of crp", "clusterResourcePlacement", klog.KObj(crp))
return nil, controller.NewUnexpectedBehaviorError(err)
}
// latestResourceSnapshotIndex should be -1 when there is no snapshot.
latestResourceSnapshot, latestResourceSnapshotIndex, err := r.lookupLatestResourceSnapshot(ctx, crp)
if err != nil {
return nil, err
}
latestResourceSnapshotHash := ""
if latestResourceSnapshot != nil {
latestResourceSnapshotHash, err = parseResourceGroupHashFromAnnotation(latestResourceSnapshot)
if err != nil {
klog.ErrorS(err, "Failed to get the ResourceGroupHashAnnotation", "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
return nil, controller.NewUnexpectedBehaviorError(err)
}
}
if latestResourceSnapshot != nil && latestResourceSnapshotHash == resourceHash {
if err := r.ensureLatestResourceSnapshot(ctx, latestResourceSnapshot); err != nil {
return nil, err
}
return latestResourceSnapshot, nil
}
// Need to create new snapshot when 1) there is no snapshots or 2) the latest snapshot hash != current one.
// mark the last resource snapshot as inactive if it is different from what we have now
if latestResourceSnapshot != nil &&
latestResourceSnapshotHash != resourceHash &&
latestResourceSnapshot.Labels[fleetv1beta1.IsLatestSnapshotLabel] == strconv.FormatBool(true) {
// set the latest label to false first to make sure there is only one or none active resource snapshot
latestResourceSnapshot.Labels[fleetv1beta1.IsLatestSnapshotLabel] = strconv.FormatBool(false)
if err := r.Client.Update(ctx, latestResourceSnapshot); err != nil {
klog.ErrorS(err, "Failed to set the isLatestSnapshot label to false", "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
return nil, controller.NewAPIServerError(false, err)
}
}
// delete redundant snapshot revisions before creating a new snapshot to guarantee that the number of snapshots
// won't exceed the limit.
if err := r.deleteRedundantResourceSnapshots(ctx, crp, revisionHistoryLimit); err != nil {
return nil, err
}
// create a new resource snapshot
latestResourceSnapshotIndex++
latestResourceSnapshot = &fleetv1beta1.ClusterResourceSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf(fleetv1beta1.ResourceSnapshotNameFmt, crp.Name, latestResourceSnapshotIndex),
Labels: map[string]string{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
fleetv1beta1.ResourceIndexLabel: strconv.Itoa(latestResourceSnapshotIndex),
},
Annotations: map[string]string{
fleetv1beta1.ResourceGroupHashAnnotation: resourceHash,
// TODO need to updated once we support multiple snapshots
fleetv1beta1.NumberOfResourceSnapshotsAnnotation: "1",
},
},
Spec: *resourceSnapshotSpec,
}
resourceSnapshotKObj := klog.KObj(latestResourceSnapshot)
if err := controllerutil.SetControllerReference(crp, latestResourceSnapshot, r.Scheme); err != nil {
klog.ErrorS(err, "Failed to set owner reference", "clusterResourceSnapshot", resourceSnapshotKObj)
// should never happen
return nil, controller.NewUnexpectedBehaviorError(err)
}
if err := r.Client.Create(ctx, latestResourceSnapshot); err != nil {
klog.ErrorS(err, "Failed to create new clusterResourceSnapshot", "clusterResourceSnapshot", resourceSnapshotKObj)
return nil, controller.NewAPIServerError(false, err)
}
return latestResourceSnapshot, nil
}
// ensureLatestPolicySnapshot ensures the latest policySnapshot has the isLatest label and the numberOfClusters are updated.
func (r *Reconciler) ensureLatestPolicySnapshot(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, latest *fleetv1beta1.ClusterSchedulingPolicySnapshot) error {
needUpdate := false
if latest.Labels[fleetv1beta1.IsLatestSnapshotLabel] != strconv.FormatBool(true) {
// When latestPolicySnapshot.Spec.PolicyHash == policyHash,
// It could happen when the controller just sets the latest label to false for the old snapshot, and fails to
// create a new policy snapshot.
// And then the customers revert back their policy to the old one again.
// In this case, the "latest" snapshot without isLatest label has the same policy hash as the current policy.
latest.Labels[fleetv1beta1.IsLatestSnapshotLabel] = strconv.FormatBool(true)
needUpdate = true
}
if crp.Spec.Policy != nil &&
crp.Spec.Policy.PlacementType == fleetv1beta1.PickNPlacementType &&
crp.Spec.Policy.NumberOfClusters != nil {
oldCount, err := utils.ExtractNumOfClustersFromPolicySnapshot(latest)
if err != nil {
klog.ErrorS(err, "Failed to parse the numberOfClusterAnnotation", "clusterSchedulingPolicySnapshot", klog.KObj(latest))
return controller.NewUnexpectedBehaviorError(err)
}
newCount := int(*crp.Spec.Policy.NumberOfClusters)
if oldCount != newCount {
latest.Annotations[fleetv1beta1.NumberOfClustersAnnotation] = strconv.Itoa(newCount)
needUpdate = true
}
}
if !needUpdate {
return nil
}
if err := r.Client.Update(ctx, latest); err != nil {
klog.ErrorS(err, "Failed to update the clusterSchedulingPolicySnapshot", "clusterSchedulingPolicySnapshot", klog.KObj(latest))
return controller.NewAPIServerError(false, err)
}
return nil
}
// ensureLatestResourceSnapshot ensures the latest resourceSnapshot has the isLatest label.
func (r *Reconciler) ensureLatestResourceSnapshot(ctx context.Context, latest *fleetv1beta1.ClusterResourceSnapshot) error {
if latest.Labels[fleetv1beta1.IsLatestSnapshotLabel] == strconv.FormatBool(true) {
return nil
}
// It could happen when the controller just sets the latest label to false for the old snapshot, and fails to
// create a new resource snapshot.
// And then the customers revert back their resource to the old one again.
// In this case, the "latest" snapshot without isLatest label has the same resource hash as the current one.
latest.Labels[fleetv1beta1.IsLatestSnapshotLabel] = strconv.FormatBool(true)
if err := r.Client.Update(ctx, latest); err != nil {
klog.ErrorS(err, "Failed to update the clusterResourceSnapshot", "ClusterResourceSnapshot", klog.KObj(latest))
return controller.NewAPIServerError(false, err)
}
return nil
}
// lookupLatestClusterSchedulingPolicySnapshot finds the latest snapshots and its policy index.
// There will be only one active policy snapshot if exists.
// It first checks whether there is an active policy snapshot.
// If not, it finds the one whose policyIndex label is the largest.
// The policy index will always start from 0.
// Return error when 1) cannot list the snapshots 2) there are more than one active policy snapshots 3) snapshot has the
// invalid label value.
// 2 & 3 should never happen.
func (r *Reconciler) lookupLatestClusterSchedulingPolicySnapshot(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) (*fleetv1beta1.ClusterSchedulingPolicySnapshot, int, error) {
snapshotList := &fleetv1beta1.ClusterSchedulingPolicySnapshotList{}
latestSnapshotLabelMatcher := client.MatchingLabels{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
}
crpKObj := klog.KObj(crp)
if err := r.Client.List(ctx, snapshotList, latestSnapshotLabelMatcher); err != nil {
klog.ErrorS(err, "Failed to list active clusterSchedulingPolicySnapshots", "clusterResourcePlacement", crpKObj)
// CRP controller needs a scheduling policy snapshot watcher to enqueue the CRP request.
// So the snapshots should be read from cache.
return nil, -1, controller.NewAPIServerError(true, err)
}
if len(snapshotList.Items) == 1 {
policyIndex, err := parsePolicyIndexFromLabel(&snapshotList.Items[0])
if err != nil {
klog.ErrorS(err, "Failed to parse the policy index label", "clusterResourcePlacement", crpKObj, "clusterSchedulingPolicySnapshot", klog.KObj(&snapshotList.Items[0]))
return nil, -1, controller.NewUnexpectedBehaviorError(err)
}
return &snapshotList.Items[0], policyIndex, nil
} else if len(snapshotList.Items) > 1 {
// It means there are multiple active snapshots and should never happen.
err := fmt.Errorf("there are %d active clusterSchedulingPolicySnapshots owned by clusterResourcePlacement %v", len(snapshotList.Items), crp.Name)
klog.ErrorS(err, "Invalid clusterSchedulingPolicySnapshots", "clusterResourcePlacement", crpKObj)
return nil, -1, controller.NewUnexpectedBehaviorError(err)
}
// When there are no active snapshots, find the one who has the largest policy index.
// It should be rare only when CRP is crashed before creating the new active snapshot.
sortedList, err := r.listSortedClusterSchedulingPolicySnapshots(ctx, crp)
if err != nil {
return nil, -1, err
}
if len(sortedList.Items) == 0 {
// The policy index of the first snapshot will start from 0.
return nil, -1, nil
}
latestSnapshot := &sortedList.Items[len(sortedList.Items)-1]
policyIndex, err := parsePolicyIndexFromLabel(latestSnapshot)
if err != nil {
klog.ErrorS(err, "Failed to parse the policy index label", "clusterResourcePlacement", crpKObj, "clusterSchedulingPolicySnapshot", klog.KObj(latestSnapshot))
return nil, -1, controller.NewUnexpectedBehaviorError(err)
}
return latestSnapshot, policyIndex, nil
}
// listSortedClusterSchedulingPolicySnapshots returns the policy snapshots sorted by the policy index.
func (r *Reconciler) listSortedClusterSchedulingPolicySnapshots(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) (*fleetv1beta1.ClusterSchedulingPolicySnapshotList, error) {
snapshotList := &fleetv1beta1.ClusterSchedulingPolicySnapshotList{}
crpKObj := klog.KObj(crp)
if err := r.Client.List(ctx, snapshotList, client.MatchingLabels{fleetv1beta1.CRPTrackingLabel: crp.Name}); err != nil {
klog.ErrorS(err, "Failed to list all clusterSchedulingPolicySnapshots", "clusterResourcePlacement", crpKObj)
// CRP controller needs a scheduling policy snapshot watcher to enqueue the CRP request.
// So the snapshots should be read from cache.
return nil, controller.NewAPIServerError(true, err)
}
var errs []error
sort.Slice(snapshotList.Items, func(i, j int) bool {
ii, err := parsePolicyIndexFromLabel(&snapshotList.Items[i])
if err != nil {
klog.ErrorS(err, "Failed to parse the policy index label", "clusterResourcePlacement", crpKObj, "clusterSchedulingPolicySnapshot", klog.KObj(&snapshotList.Items[i]))
errs = append(errs, err)
}
ji, err := parsePolicyIndexFromLabel(&snapshotList.Items[j])
if err != nil {
klog.ErrorS(err, "Failed to parse the policy index label", "clusterResourcePlacement", crpKObj, "clusterSchedulingPolicySnapshot", klog.KObj(&snapshotList.Items[j]))
errs = append(errs, err)
}
return ii < ji
})
if len(errs) > 0 {
return nil, controller.NewUnexpectedBehaviorError(utilerrors.NewAggregate(errs))
}
return snapshotList, nil
}
// lookupLatestResourceSnapshot finds the latest snapshots and.
// There will be only one active resource snapshot if exists.
// It first checks whether there is an active resource snapshot.
// If not, it finds the one whose resourceIndex label is the largest.
// The resource index will always start from 0.
// Return error when 1) cannot list the snapshots 2) there are more than one active resource snapshots 3) snapshot has the
// invalid label value.
// 2 & 3 should never happen.
func (r *Reconciler) lookupLatestResourceSnapshot(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) (*fleetv1beta1.ClusterResourceSnapshot, int, error) {
snapshotList := &fleetv1beta1.ClusterResourceSnapshotList{}
latestSnapshotLabelMatcher := client.MatchingLabels{
fleetv1beta1.CRPTrackingLabel: crp.Name,
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
}
crpKObj := klog.KObj(crp)
if err := r.Client.List(ctx, snapshotList, latestSnapshotLabelMatcher); err != nil {
klog.ErrorS(err, "Failed to list active clusterResourceSnapshots", "clusterResourcePlacement", crpKObj)
return nil, -1, controller.NewAPIServerError(false, err)
}
if len(snapshotList.Items) == 1 {
resourceIndex, err := parseResourceIndexFromLabel(&snapshotList.Items[0])
if err != nil {
klog.ErrorS(err, "Failed to parse the resource index label", "clusterResourceSnapshot", klog.KObj(&snapshotList.Items[0]))
return nil, -1, controller.NewUnexpectedBehaviorError(err)
}
return &snapshotList.Items[0], resourceIndex, nil
} else if len(snapshotList.Items) > 1 {
// It means there are multiple active snapshots and should never happen.
err := fmt.Errorf("there are %d active clusterResourceSnapshots owned by clusterResourcePlacement %v", len(snapshotList.Items), crp.Name)
klog.ErrorS(err, "Invalid clusterResourceSnapshots", "clusterResourcePlacement", crpKObj)
return nil, -1, controller.NewUnexpectedBehaviorError(err)
}
// When there are no active snapshots, find the first snapshot who has the largest resource index.
// It should be rare only when CRP is crashed before creating the new active snapshot.
sortedList, err := r.listSortedResourceSnapshots(ctx, crp)
if err != nil {
return nil, -1, err
}
if len(sortedList.Items) == 0 {
// The resource index of the first snapshot will start from 0.
return nil, -1, nil
}
latestSnapshot := &sortedList.Items[len(sortedList.Items)-1]
resourceIndex, err := parseResourceIndexFromLabel(latestSnapshot)
if err != nil {
klog.ErrorS(err, "Failed to parse the resource index label", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", klog.KObj(latestSnapshot))
return nil, -1, controller.NewUnexpectedBehaviorError(err)
}
return latestSnapshot, resourceIndex, nil
}
// listSortedResourceSnapshots returns the resource snapshots sorted by its index and its subindex.
// The resourceSnapshot is less than the other one when resourceIndex is less.
// When the resourceIndex is equal, then order by the subindex.
// Note: the snapshot does not have subindex is the largest of a group and there should be only one in a group.
func (r *Reconciler) listSortedResourceSnapshots(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement) (*fleetv1beta1.ClusterResourceSnapshotList, error) {
snapshotList := &fleetv1beta1.ClusterResourceSnapshotList{}
crpKObj := klog.KObj(crp)
if err := r.Client.List(ctx, snapshotList, client.MatchingLabels{fleetv1beta1.CRPTrackingLabel: crp.Name}); err != nil {
klog.ErrorS(err, "Failed to list all clusterResourceSnapshots", "clusterResourcePlacement", crpKObj)
return nil, controller.NewAPIServerError(false, err)
}
var errs []error
sort.Slice(snapshotList.Items, func(i, j int) bool {
iKObj := klog.KObj(&snapshotList.Items[i])
jKObj := klog.KObj(&snapshotList.Items[j])
ii, err := parseResourceIndexFromLabel(&snapshotList.Items[i])
if err != nil {
klog.ErrorS(err, "Failed to parse the resource index label", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", iKObj)
errs = append(errs, err)
}
ji, err := parseResourceIndexFromLabel(&snapshotList.Items[j])
if err != nil {
klog.ErrorS(err, "Failed to parse the resource index label", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", jKObj)
errs = append(errs, err)
}
if ii != ji {
return ii < ji
}
iDoesExist, iSubindex, err := utils.ExtractSubindexFromClusterResourceSnapshot(&snapshotList.Items[i])
if err != nil {
klog.ErrorS(err, "Failed to parse the subindex index", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", iKObj)
errs = append(errs, err)
}
jDoesExist, jSubindex, err := utils.ExtractSubindexFromClusterResourceSnapshot(&snapshotList.Items[j])
if err != nil {
klog.ErrorS(err, "Failed to parse the subindex index", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", jKObj)
errs = append(errs, err)
}
// Both of the snapshots do not have subindex, which should not happen.
if !iDoesExist && !jDoesExist {
klog.ErrorS(err, "There are more than one resource snapshot which do not have subindex in a group", "clusterResourcePlacement", crpKObj, "clusterResourceSnapshot", iKObj, "clusterResourceSnapshot", jKObj)
errs = append(errs, err)
}
if !iDoesExist { // check if it's the first snapshot
return false
}
if !jDoesExist { // check if it's the first snapshot
return true
}
return iSubindex < jSubindex
})
if len(errs) > 0 {
return nil, controller.NewUnexpectedBehaviorError(utilerrors.NewAggregate(errs))
}
return snapshotList, nil
}
// parsePolicyIndexFromLabel returns error when parsing the label which should never return error in production.
func parsePolicyIndexFromLabel(s *fleetv1beta1.ClusterSchedulingPolicySnapshot) (int, error) {
indexLabel := s.Labels[fleetv1beta1.PolicyIndexLabel]
v, err := strconv.Atoi(indexLabel)
if err != nil || v < 0 {
return -1, fmt.Errorf("invalid policy index %q, error: %w", indexLabel, err)
}
return v, nil
}
// parseResourceIndexFromLabel returns error when parsing the label which should never return error in production.
func parseResourceIndexFromLabel(s *fleetv1beta1.ClusterResourceSnapshot) (int, error) {
indexLabel := s.Labels[fleetv1beta1.ResourceIndexLabel]
v, err := strconv.Atoi(indexLabel)
if err != nil || v < 0 {
return -1, fmt.Errorf("invalid resource index %q, error: %w", indexLabel, err)
}
return v, nil
}
func generatePolicyHash(policy *fleetv1beta1.PlacementPolicy) (string, error) {
jsonBytes, err := json.Marshal(policy)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", sha256.Sum256(jsonBytes)), nil
}
func generateResourceHash(rs *fleetv1beta1.ResourceSnapshotSpec) (string, error) {
jsonBytes, err := json.Marshal(rs)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", sha256.Sum256(jsonBytes)), nil
}
// parseResourceGroupHashFromAnnotation returns error when parsing the annotation which should never return error in production.
func parseResourceGroupHashFromAnnotation(s *fleetv1beta1.ClusterResourceSnapshot) (string, error) {
v, ok := s.Annotations[fleetv1beta1.ResourceGroupHashAnnotation]
if !ok {
return "", fmt.Errorf("ResourceGroupHashAnnotation is not set")
}
return v, nil
}
// TODO: handle multiple resourceSnapshots
func (r *Reconciler) buildPlacementStatus(crp *fleetv1beta1.ClusterResourcePlacement, latestSchedulingPolicySnapshot *fleetv1beta1.ClusterSchedulingPolicySnapshot, latestResourceSnapshot *fleetv1beta1.ClusterResourceSnapshot) (*fleetv1beta1.ClusterResourcePlacementStatus, error) {
status := &fleetv1beta1.ClusterResourcePlacementStatus{
SelectedResources: make([]fleetv1beta1.ResourceIdentifier, 0, len(latestResourceSnapshot.Spec.SelectedResources)),
PlacementStatuses: make([]fleetv1beta1.ResourcePlacementStatus, 0, 20), // pre-allocate with a reasonable capacity
Conditions: []metav1.Condition{},
}
// TODO query the sub resourceSnapshots by using latestResourceSnapshot and then update the status.
// Work generator also needs to do this and we need to refactor the code to share the same functionality.
for i := range latestResourceSnapshot.Spec.SelectedResources {
obj, err := r.decodeResourceContent(&latestResourceSnapshot.Spec.SelectedResources[i])
if err != nil {
klog.ErrorS(err, "Failed to decode resourceContent of clusterResourceSnapshot", "clusterResourcePlacement", klog.KObj(crp), "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
return nil, controller.NewUnexpectedBehaviorError(err)
}
ri := fleetv1beta1.ResourceIdentifier{
Group: obj.GroupVersionKind().Group,
Version: obj.GroupVersionKind().Version,
Kind: obj.GroupVersionKind().Kind,
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
status.SelectedResources = append(status.SelectedResources, ri)
}
scheduledCondition := buildScheduledCondition(crp, latestSchedulingPolicySnapshot)
meta.SetStatusCondition(&status.Conditions, scheduledCondition)
// When scheduledCondition is unknown, appliedCondition should be unknown too.
// Note: If the scheduledCondition is failed, it means the placement requirement cannot be satisfied fully. For example,
// pickN deployment requires 5 clusters and scheduler schedules the resources on 3 clusters. And the appliedCondition
// could be true when resources are applied successfully on these 3 clusters and the detailed the resourcePlacementStatuses
// need to be populated.
if scheduledCondition.Status == metav1.ConditionUnknown {
appliedCondition := metav1.Condition{
Status: metav1.ConditionUnknown,
Type: string(fleetv1beta1.ClusterResourcePlacementAppliedConditionType),
Reason: ApplyPendingReason,
Message: "Scheduling has not completed",
ObservedGeneration: crp.Generation,
}
meta.SetStatusCondition(&status.Conditions, appliedCondition)
// skip populating detailed resourcePlacementStatus
return status, nil
}
// TODO collect works and build appliedCondition & resourcePlacementStatus
status.PlacementStatuses = buildResourcePlacementStatus(crp, latestSchedulingPolicySnapshot)
return status, nil
}
func buildScheduledCondition(crp *fleetv1beta1.ClusterResourcePlacement, latestSchedulingPolicySnapshot *fleetv1beta1.ClusterSchedulingPolicySnapshot) metav1.Condition {
scheduledCondition := latestSchedulingPolicySnapshot.GetCondition(string(fleetv1beta1.PolicySnapshotScheduled))
if scheduledCondition == nil ||
// defensive check and not needed for now as the policySnapshot should be immutable.
scheduledCondition.ObservedGeneration < latestSchedulingPolicySnapshot.Generation ||
// We have numberOfCluster annotation added on the CRP and it won't change the CRP generation.
// So that we need to compare the CRP observedCRPGeneration reported by the scheduler.
latestSchedulingPolicySnapshot.Status.ObservedCRPGeneration < crp.Generation ||
scheduledCondition.Status == metav1.ConditionUnknown {
return metav1.Condition{
Status: metav1.ConditionUnknown,
Type: string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: "Scheduling",
Message: "Record the intermediate status of the scheduling",
ObservedGeneration: crp.Generation,
}
}
return metav1.Condition{
Status: scheduledCondition.Status,
Type: string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: scheduledCondition.Reason,
Message: scheduledCondition.Message,
ObservedGeneration: crp.Generation,
}
}
func classifyClusterDecisions(decisions []fleetv1beta1.ClusterDecision) (selected []*fleetv1beta1.ClusterDecision, unselected []*fleetv1beta1.ClusterDecision) {
selected = make([]*fleetv1beta1.ClusterDecision, 0, len(decisions))
unselected = make([]*fleetv1beta1.ClusterDecision, 0, len(decisions))
for i := range decisions {
if decisions[i].Selected {
selected = append(selected, &decisions[i])
} else {
unselected = append(unselected, &decisions[i])
}
}
return selected, unselected
}
func buildResourcePlacementStatus(crp *fleetv1beta1.ClusterResourcePlacement, latestSchedulingPolicySnapshot *fleetv1beta1.ClusterSchedulingPolicySnapshot) []fleetv1beta1.ResourcePlacementStatus {
res := make([]fleetv1beta1.ResourcePlacementStatus, 0, len(latestSchedulingPolicySnapshot.Status.ClusterDecisions))
decisions := latestSchedulingPolicySnapshot.Status.ClusterDecisions
selected, unselected := classifyClusterDecisions(decisions)
// In the pickN case, if the placement cannot be satisfied. For example, pickN deployment requires 5 clusters and
// scheduler schedules the resources on 3 clusters. We'll populate why the other two cannot be scheduled.
// Here it is calculating how many unscheduled resources there are.
unscheduledClusterCount := 0
if crp.Spec.Policy != nil &&
crp.Spec.Policy.PlacementType == fleetv1beta1.PickNPlacementType &&
crp.Spec.Policy.NumberOfClusters != nil {
unscheduledClusterCount = int(*crp.Spec.Policy.NumberOfClusters) - len(selected)
}
for _, c := range selected {
var rp fleetv1beta1.ResourcePlacementStatus
scheduledCondition := metav1.Condition{
Status: metav1.ConditionTrue,
Type: string(fleetv1beta1.ResourceScheduledConditionType),
Reason: "ScheduleSucceeded",
Message: fmt.Sprintf(resourcePlacementConditionScheduleSucceededMessageFormat, c.ClusterName, c.Reason),
ObservedGeneration: crp.Generation,
}
rp.ClusterName = c.ClusterName
if c.ClusterScore != nil {
scheduledCondition.Message = fmt.Sprintf(resourcePlacementConditionScheduleSucceededWithScoreMessageFormat, c.ClusterName, c.ClusterScore, c.Reason)
}
// TODO populate the work status
meta.SetStatusCondition(&rp.Conditions, scheduledCondition)
res = append(res, rp)
}
for i := 0; i < unscheduledClusterCount && i < len(unselected); i++ {
// TODO: we could improve the message by summarizing the failure reasons from all of the unselected clusters.
// For now, it starts from adding some sample failures of unselected clusters.
var rp fleetv1beta1.ResourcePlacementStatus
scheduledCondition := metav1.Condition{
Status: metav1.ConditionFalse,
Type: string(fleetv1beta1.ResourceScheduledConditionType),
Reason: "ScheduleFailed",
Message: fmt.Sprintf(resourcePlacementConditionScheduleFailedMessageFormat, unselected[i].ClusterName, unselected[i].Reason),
ObservedGeneration: crp.Generation,
}
if unselected[i].ClusterScore != nil {
scheduledCondition.Message = fmt.Sprintf(resourcePlacementConditionScheduleFailedWithScoreMessageFormat, unselected[i].ClusterName, unselected[i].ClusterScore, unselected[i].Reason)
}
meta.SetStatusCondition(&rp.Conditions, scheduledCondition)
res = append(res, rp)
}
return res
}
// decodeResourceContent decodes the resourceContent into usable structs.
func (r *Reconciler) decodeResourceContent(resourceContent *fleetv1beta1.ResourceContent) (*unstructured.Unstructured, error) {
unstructuredObj := &unstructured.Unstructured{}
if err := unstructuredObj.UnmarshalJSON(resourceContent.Raw); err != nil {
return nil, fmt.Errorf("failed to decode object: %w", err)
}
return unstructuredObj, nil
}