Skip to content

Commit 170397f

Browse files
author
Ryan Zhang
committed
test again
Signed-off-by: Ryan Zhang <[email protected]>
1 parent d9f703f commit 170397f

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

pkg/controllers/clusterresourceplacement/controller.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import (
4949
)
5050

5151
// The max size of an object in k8s is 1.5MB because of ETCD limit https://etcd.io/docs/v3.3/dev-guide/limit/.
52-
// We choose 800KB as the soft limit for all the selected resources within one clusterResourceSnapshot object because of this test in k8s which checks
52+
// We choose 800KB as the soft limit for all the selected resources within one resourceSnapshot object because of this test in k8s which checks
5353
// if object size is greater than 1MB https://github.com/kubernetes/kubernetes/blob/db1990f48b92d603f469c1c89e2ad36da1b74846/test/integration/master/synthetic_master_test.go#L337
5454
var resourceSnapshotResourceSizeLimit = 800 * (1 << 10) // 800KB
5555

@@ -186,19 +186,19 @@ func (r *Reconciler) handleUpdate(ctx context.Context, placementObj fleetv1beta1
186186
// Rebuild the seletedResourceIDs using the latestResourceSnapshot.
187187
latestResourceSnapshotIndex, err := labels.ExtractResourceIndexFromResourceSnapshot(latestResourceSnapshot)
188188
if err != nil {
189-
klog.ErrorS(err, "Failed to extract the resource index from the clusterResourceSnapshot", "placement", placementKObj, "clusterResourceSnapshot", latestResourceSnapshotKObj)
189+
klog.ErrorS(err, "Failed to extract the resource index from the resourceSnapshot", "placement", placementKObj, "resourceSnapshot", latestResourceSnapshotKObj)
190190
return ctrl.Result{}, controller.NewUnexpectedBehaviorError(err)
191191
}
192192
selectedResourceIDs, err = controller.CollectResourceIdentifiersUsingMasterResourceSnapshot(ctx, r.Client, placementObj.GetName(), latestResourceSnapshot, strconv.Itoa(latestResourceSnapshotIndex))
193193
if err != nil {
194-
klog.ErrorS(err, "Failed to collect resource identifiers from the clusterResourceSnapshot", "placement", placementKObj, "clusterResourceSnapshot", latestResourceSnapshotKObj)
194+
klog.ErrorS(err, "Failed to collect resource identifiers from the resourceSnapshot", "placement", placementKObj, "resourceSnapshot", latestResourceSnapshotKObj)
195195
return ctrl.Result{}, err
196196
}
197-
klog.V(2).InfoS("Fetched the selected resources from the lastestResourceSnapshot", "placement", placementKObj, "clusterResourceSnapshot", latestResourceSnapshotKObj, "generation", placementObj.GetGeneration())
197+
klog.V(2).InfoS("Fetched the selected resources from the lastestResourceSnapshot", "placement", placementKObj, "resourceSnapshot", latestResourceSnapshotKObj, "generation", placementObj.GetGeneration())
198198
}
199199

200-
// isClusterScheduled is to indicate whether we need to requeue the placement request to track the rollout status.
201-
isClusterScheduled, err := r.setPlacementStatus(ctx, placementObj, selectedResourceIDs, latestSchedulingPolicySnapshot, latestResourceSnapshot)
200+
// isScheduleFullfilled is to indicate whether we need to requeue the placement request to track the rollout status.
201+
isScheduleFullfilled, err := r.setPlacementStatus(ctx, placementObj, selectedResourceIDs, latestSchedulingPolicySnapshot, latestResourceSnapshot)
202202
if err != nil {
203203
return ctrl.Result{}, err
204204
}
@@ -222,8 +222,8 @@ func (r *Reconciler) handleUpdate(ctx context.Context, placementObj fleetv1beta1
222222

223223
// Rollout is considered to be completed when all the expected condition types are set to the
224224
// True status.
225-
if isRolloutCompleted(placementObj) {
226-
if !isRolloutCompleted(oldPlacement) {
225+
if isPlacementRolloutCompleted(placementObj) {
226+
if !isPlacementRolloutCompleted(oldPlacement) {
227227
klog.V(2).InfoS("Placement has finished the rollout process and reached the desired status", "placement", placementKObj, "generation", placementObj.GetGeneration())
228228
r.Recorder.Event(placementObj, corev1.EventTypeNormal, "PlacementRolloutCompleted", "Placement has finished the rollout process and reached the desired status")
229229
}
@@ -236,7 +236,7 @@ func (r *Reconciler) handleUpdate(ctx context.Context, placementObj fleetv1beta1
236236
return ctrl.Result{}, nil
237237
}
238238

239-
if !isClusterScheduled {
239+
if !isScheduleFullfilled {
240240
// Note:
241241
// If the scheduledCondition is failed, it means the placement requirement cannot be satisfied fully. For example,
242242
// pickN deployment requires 5 clusters and scheduler schedules the resources on 3 clusters. And the appliedCondition
@@ -352,12 +352,6 @@ func (r *Reconciler) lookupLatestSchedulingPolicySnapshot(ctx context.Context, p
352352
return nil, -1, err
353353
}
354354
placementKObj := klog.KObj(placement)
355-
if err != nil {
356-
klog.ErrorS(err, "Failed to list active schedulingPolicySnapshots", "placement", placementKObj)
357-
// Placement controller needs a scheduling policy snapshot watcher to enqueue the placement request.
358-
// So the snapshots should be read from cache.
359-
return nil, -1, controller.NewAPIServerError(true, err)
360-
}
361355
policySnapshotItems := snapshotList.GetPolicySnapshotObjs()
362356
if len(policySnapshotItems) == 1 {
363357
policyIndex, err := labels.ParsePolicyIndexFromLabel(policySnapshotItems[0])
@@ -533,20 +527,20 @@ func (r *Reconciler) getOrCreateResourceSnapshot(ctx context.Context, placement
533527
if latestResourceSnapshot != nil {
534528
latestResourceSnapshotHash, err = annotations.ParseResourceGroupHashFromAnnotation(latestResourceSnapshot)
535529
if err != nil {
536-
klog.ErrorS(err, "Failed to get the ResourceGroupHashAnnotation", "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
530+
klog.ErrorS(err, "Failed to get the ResourceGroupHashAnnotation", "resourceSnapshot", klog.KObj(latestResourceSnapshot))
537531
return ctrl.Result{}, nil, controller.NewUnexpectedBehaviorError(err)
538532
}
539533
numberOfSnapshots, err = annotations.ExtractNumberOfResourceSnapshotsFromResourceSnapshot(latestResourceSnapshot)
540534
if err != nil {
541-
klog.ErrorS(err, "Failed to get the NumberOfResourceSnapshotsAnnotation", "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
535+
klog.ErrorS(err, "Failed to get the NumberOfResourceSnapshotsAnnotation", "resourceSnapshot", klog.KObj(latestResourceSnapshot))
542536
return ctrl.Result{}, nil, controller.NewUnexpectedBehaviorError(err)
543537
}
544538
}
545539

546540
shouldCreateNewMasterResourceSnapshot := true
547541
// This index indicates the selected resource in the split selectedResourceList, if this index is zero we start
548-
// from creating the master clusterResourceSnapshot if it's greater than zero it means that the master clusterResourceSnapshot
549-
// got created but not all sub-indexed clusterResourceSnapshots have been created yet. It covers the corner case where the
542+
// from creating the master resourceSnapshot if it's greater than zero it means that the master resourceSnapshot
543+
// got created but not all sub-indexed resourceSnapshots have been created yet. It covers the corner case where the
550544
// controller crashes in the middle.
551545
resourceSnapshotStartIndex := 0
552546
if latestResourceSnapshot != nil && latestResourceSnapshotHash == resourceHash {
@@ -560,7 +554,7 @@ func (r *Reconciler) getOrCreateResourceSnapshot(ctx context.Context, placement
560554
return ctrl.Result{}, nil, controller.NewAPIServerError(true, err)
561555
}
562556
if len(resourceSnapshotList.GetResourceSnapshotObjs()) == numberOfSnapshots {
563-
klog.V(2).InfoS("ClusterResourceSnapshots have not changed", "placement", placementKObj, "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
557+
klog.V(2).InfoS("resourceSnapshots have not changed", "placement", placementKObj, "resourceSnapshot", klog.KObj(latestResourceSnapshot))
564558
return ctrl.Result{}, latestResourceSnapshot, nil
565559
}
566560
// we should not create a new master cluster resource snapshot.
@@ -593,10 +587,10 @@ func (r *Reconciler) getOrCreateResourceSnapshot(ctx context.Context, placement
593587
labels[fleetv1beta1.IsLatestSnapshotLabel] = strconv.FormatBool(false)
594588
latestResourceSnapshot.SetLabels(labels)
595589
if err := r.Client.Update(ctx, latestResourceSnapshot); err != nil {
596-
klog.ErrorS(err, "Failed to set the isLatestSnapshot label to false", "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
590+
klog.ErrorS(err, "Failed to set the isLatestSnapshot label to false", "resourceSnapshot", klog.KObj(latestResourceSnapshot))
597591
return ctrl.Result{}, nil, controller.NewUpdateIgnoreConflictError(err)
598592
}
599-
klog.V(2).InfoS("Marked the existing clusterResourceSnapshot as inactive", "placement", placementKObj, "clusterResourceSnapshot", klog.KObj(latestResourceSnapshot))
593+
klog.V(2).InfoS("Marked the existing resourceSnapshot as inactive", "placement", placementKObj, "resourceSnapshot", klog.KObj(latestResourceSnapshot))
600594
}
601595

602596
// only delete redundant resource snapshots and increment the latest resource snapshot index if new master resource snapshot is to be created.
@@ -645,32 +639,33 @@ func (r *Reconciler) shouldCreateNewResourceSnapshotNow(ctx context.Context, lat
645639
nextResourceSnapshotCandidateDetectionTime, err := annotations.ExtractNextResourceSnapshotCandidateDetectionTimeFromResourceSnapshot(latestResourceSnapshot)
646640
if nextResourceSnapshotCandidateDetectionTime.IsZero() || err != nil {
647641
if err != nil {
648-
klog.ErrorS(controller.NewUnexpectedBehaviorError(err), "Failed to get the NextResourceSnapshotCandidateDetectionTimeAnnotation", "clusterResourceSnapshot", snapshotKObj)
642+
klog.ErrorS(controller.NewUnexpectedBehaviorError(err), "Failed to get the NextResourceSnapshotCandidateDetectionTimeAnnotation", "resourceSnapshot", snapshotKObj)
649643
}
650644
// If the annotation is not set, set next resource snapshot candidate detection time is now.
651645
if latestResourceSnapshot.GetAnnotations() == nil {
652646
latestResourceSnapshot.SetAnnotations(make(map[string]string))
653647
}
654648
latestResourceSnapshot.GetAnnotations()[fleetv1beta1.NextResourceSnapshotCandidateDetectionTimeAnnotation] = now.Format(time.RFC3339)
655649
if err := r.Client.Update(ctx, latestResourceSnapshot); err != nil {
656-
klog.ErrorS(err, "Failed to update the NextResourceSnapshotCandidateDetectionTime annotation", "clusterResourceSnapshot", snapshotKObj)
650+
klog.ErrorS(err, "Failed to update the NextResourceSnapshotCandidateDetectionTime annotation", "resourceSnapshot", snapshotKObj)
657651
return ctrl.Result{}, controller.NewUpdateIgnoreConflictError(err)
658652
}
659653
nextResourceSnapshotCandidateDetectionTime = now
660-
klog.V(2).InfoS("Updated the NextResourceSnapshotCandidateDetectionTime annotation", "clusterResourceSnapshot", snapshotKObj, "nextResourceSnapshotCandidateDetectionTimeAnnotation", now.Format(time.RFC3339))
654+
klog.V(2).InfoS("Updated the NextResourceSnapshotCandidateDetectionTime annotation", "resourceSnapshot", snapshotKObj, "nextResourceSnapshotCandidateDetectionTimeAnnotation", now.Format(time.RFC3339))
661655
}
662656
nextCreationTime := fleettime.MaxTime(nextResourceSnapshotCandidateDetectionTime.Add(r.ResourceChangesCollectionDuration), latestResourceSnapshot.GetCreationTimestamp().Add(r.ResourceSnapshotCreationMinimumInterval))
663657
if now.Before(nextCreationTime) {
664658
// If the next resource snapshot creation time is not reached, we requeue the request to avoid too frequent update.
665659
klog.V(2).InfoS("Delaying the new resourceSnapshot creation",
666-
"clusterResourceSnapshot", snapshotKObj, "nextCreationTime", nextCreationTime, "latestResourceSnapshotCreationTime", latestResourceSnapshot.GetCreationTimestamp(),
660+
"resourceSnapshot", snapshotKObj, "nextCreationTime", nextCreationTime, "latestResourceSnapshotCreationTime", latestResourceSnapshot.GetCreationTimestamp(),
667661
"resourceSnapshotCreationMinimumInterval", r.ResourceSnapshotCreationMinimumInterval, "resourceChangesCollectionDuration", r.ResourceChangesCollectionDuration,
668662
"afterDuration", nextCreationTime.Sub(now))
669663
return ctrl.Result{Requeue: true, RequeueAfter: nextCreationTime.Sub(now)}, nil
670664
}
671665
return ctrl.Result{}, nil
672666
}
673667

668+
// TODO: move this to library package
674669
// buildMasterResourceSnapshot builds and returns the master resource snapshot for the latest resource snapshot index and selected resources.
675670
func BuildMasterResourceSnapshot(latestResourceSnapshotIndex, resourceSnapshotCount, envelopeObjCount int, placementName, placementNamespace, resourceHash string, selectedResources []fleetv1beta1.ResourceContent) fleetv1beta1.ResourceSnapshotObj {
676671
labels := map[string]string{
@@ -710,6 +705,7 @@ func BuildMasterResourceSnapshot(latestResourceSnapshotIndex, resourceSnapshotCo
710705
}
711706
}
712707

708+
// TODO: move this to library package
713709
// BuildSubIndexResourceSnapshot builds and returns the sub index resource snapshot for both cluster-scoped and namespace-scoped placements.
714710
// Returns a ClusterResourceSnapshot for cluster-scoped placements (empty namespace) or ResourceSnapshot for namespace-scoped placements.
715711
func BuildSubIndexResourceSnapshot(latestResourceSnapshotIndex, resourceSnapshotSubIndex int, placementName, placementNamespace string, selectedResources []fleetv1beta1.ResourceContent) fleetv1beta1.ResourceSnapshotObj {
@@ -1037,7 +1033,8 @@ func (r *Reconciler) setPlacementStatus(
10371033
// conditions (any condition type other than Scheduled).
10381034

10391035
// Note that the scheduled condition has been set earlier in this method.
1040-
placementStatus.Conditions = []metav1.Condition{scheduledCondition}
1036+
placementStatus.Conditions = []metav1.Condition{}
1037+
placementObj.SetConditions(scheduledCondition)
10411038
return isPolicySelectingNoClusters(placementObj.GetPlacementSpec().Policy), nil
10421039
}
10431040

@@ -1250,7 +1247,11 @@ func buildResourcePlacementStatusMap(placementObj fleetv1beta1.PlacementObj) map
12501247
return m
12511248
}
12521249

1253-
func isRolloutCompleted(placementObj fleetv1beta1.PlacementObj) bool {
1250+
// TODO: make this work for RP too
1251+
// isPlacementRolloutCompleted checks if the placement rollout is completed which means:
1252+
// 1. Placement Scheduled condition is true.
1253+
// 2. All expected placement conditions are true depends on what type of policy placementObj has.
1254+
func isPlacementRolloutCompleted(placementObj fleetv1beta1.PlacementObj) bool {
12541255
if !condition.IsConditionStatusTrue(placementObj.GetCondition(string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType)), placementObj.GetGeneration()) {
12551256
return false
12561257
}

pkg/controllers/clusterresourceplacement/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3569,7 +3569,7 @@ func TestIsRolloutComplete(t *testing.T) {
35693569
Conditions: tc.conditions,
35703570
},
35713571
}
3572-
got := isRolloutCompleted(crp)
3572+
got := isPlacementRolloutCompleted(crp)
35733573
if got != tc.want {
35743574
t.Errorf("isRolloutCompleted() got %v, want %v", got, tc.want)
35753575
}

pkg/controllers/clusterresourceplacement/placement_status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func determineExpectedPlacementAndResourcePlacementStatusCondType(placementObj f
113113

114114
// appendScheduledResourcePlacementStatuses appends the resource placement statuses for the
115115
// scheduled clusters to the list of all resource placement statuses.
116+
// it returns the updated list of resource placement statuses.
116117
func (r *Reconciler) appendScheduledResourcePlacementStatuses(
117118
ctx context.Context,
118119
allRPS []fleetv1beta1.ResourcePlacementStatus,

pkg/utils/controller/policy_snapshot_resolver.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,31 @@ func DeletePolicySnapshots(ctx context.Context, k8Client client.Client, placemen
6363
// For cluster-scoped placements, it returns a ClusterSchedulingPolicySnapshot.
6464
// For namespaced placements, it returns a SchedulingPolicySnapshot.
6565
func BuildPolicySnapshot(placementObj fleetv1beta1.PlacementObj, policySnapshotIndex int, policyHash string) fleetv1beta1.PolicySnapshotObj {
66+
var snapshot fleetv1beta1.PolicySnapshotObj
6667
labels := map[string]string{
6768
fleetv1beta1.PlacementTrackingLabel: placementObj.GetName(),
6869
fleetv1beta1.IsLatestSnapshotLabel: strconv.FormatBool(true),
6970
fleetv1beta1.PolicyIndexLabel: strconv.Itoa(policySnapshotIndex),
7071
}
71-
7272
annotations := map[string]string{
7373
fleetv1beta1.CRPGenerationAnnotation: strconv.FormatInt(placementObj.GetGeneration(), 10),
7474
}
75+
// Add NumberOfClusters annotation if placement is selectN type
76+
if spec := placementObj.GetPlacementSpec(); spec.Policy != nil &&
77+
spec.Policy.PlacementType == fleetv1beta1.PickNPlacementType &&
78+
spec.Policy.NumberOfClusters != nil {
79+
annotations[fleetv1beta1.NumberOfClustersAnnotation] = strconv.Itoa(int(*spec.Policy.NumberOfClusters))
80+
}
7581

82+
spec := fleetv1beta1.SchedulingPolicySnapshotSpec{
83+
Policy: placementObj.GetPlacementSpec().Policy,
84+
PolicyHash: []byte(policyHash),
85+
},
7686
// Set the name following the convention: {PlacementName}-{index}
7787
name := fmt.Sprintf(fleetv1beta1.PolicySnapshotNameFmt, placementObj.GetName(), policySnapshotIndex)
78-
7988
if placementObj.GetNamespace() != "" {
8089
// This is a namespaced ResourcePlacement - create SchedulingPolicySnapshot
81-
snapshot := &fleetv1beta1.SchedulingPolicySnapshot{
90+
snapshot = &fleetv1beta1.SchedulingPolicySnapshot{
8291
ObjectMeta: metav1.ObjectMeta{
8392
Name: name,
8493
Namespace: placementObj.GetNamespace(),
@@ -90,10 +99,9 @@ func BuildPolicySnapshot(placementObj fleetv1beta1.PlacementObj, policySnapshotI
9099
PolicyHash: []byte(policyHash),
91100
},
92101
}
93-
return snapshot
94102
} else {
95103
// This is a cluster-scoped ClusterResourcePlacement - create ClusterSchedulingPolicySnapshot
96-
snapshot := &fleetv1beta1.ClusterSchedulingPolicySnapshot{
104+
snapshot = &fleetv1beta1.ClusterSchedulingPolicySnapshot{
97105
ObjectMeta: metav1.ObjectMeta{
98106
Name: name,
99107
Labels: labels,
@@ -104,16 +112,8 @@ func BuildPolicySnapshot(placementObj fleetv1beta1.PlacementObj, policySnapshotI
104112
PolicyHash: []byte(policyHash),
105113
},
106114
}
107-
108-
// Add NumberOfClusters annotation if placement is selectN type
109-
if spec := placementObj.GetPlacementSpec(); spec.Policy != nil &&
110-
spec.Policy.PlacementType == fleetv1beta1.PickNPlacementType &&
111-
spec.Policy.NumberOfClusters != nil {
112-
snapshot.Annotations[fleetv1beta1.NumberOfClustersAnnotation] = strconv.Itoa(int(*spec.Policy.NumberOfClusters))
113-
}
114-
115-
return snapshot
116115
}
116+
return snapshot
117117
}
118118

119119
// FetchLatestPolicySnapshot fetches the latest policy snapshot for a given placement.

0 commit comments

Comments
 (0)