Skip to content

Commit c06ca95

Browse files
authored
feat: refactor rollout controller for RP (#158)
--------- Signed-off-by: Wantong Jiang <[email protected]>
1 parent d431faa commit c06ca95

File tree

11 files changed

+1551
-183
lines changed

11 files changed

+1551
-183
lines changed

CLAUDE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ cmd/memberagent/ # Member agent main and setup
178178
- Use shared test manifests in `test/integration/manifests/`
179179
- Run with `make e2e-tests` against 3 Kind clusters
180180

181+
### Test Coding Style
182+
- Use `want` or `wanted` instead of `expect` or `expected` when creating the desired state
183+
181184
## Key Patterns
182185

183186
### Controller Pattern

apis/placement/v1beta1/commons.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const (
112112
// ParentNamespaceLabel is the label applied to work that contains the namespace of the binding that generates the work.
113113
ParentNamespaceLabel = fleetPrefix + "parent-placement-namespace"
114114

115-
// CRPGenerationAnnotation indicates the generation of the CRP from which an object is derived or last updated.
115+
// CRPGenerationAnnotation indicates the generation of the placement from which an object is derived or last updated.
116116
// TODO: rename this variable
117117
CRPGenerationAnnotation = fleetPrefix + "CRP-generation"
118118

cmd/hubagent/workload/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func SetupControllers(ctx context.Context, wg *sync.WaitGroup, mgr ctrl.Manager,
227227
UncachedReader: mgr.GetAPIReader(),
228228
MaxConcurrentReconciles: int(math.Ceil(float64(opts.MaxFleetSizeSupported)/30) * math.Ceil(float64(opts.MaxConcurrentClusterPlacement)/10)),
229229
InformerManager: dynamicInformerManager,
230-
}).SetupWithManager(mgr); err != nil {
230+
}).SetupWithManagerForClusterResourcePlacement(mgr); err != nil {
231231
klog.ErrorS(err, "Unable to set up rollout controller")
232232
return err
233233
}

pkg/controllers/clusterresourceplacement/controller.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (r *Reconciler) handleUpdate(ctx context.Context, placementObj fleetv1beta1
135135

136136
if placementSpec.RevisionHistoryLimit != nil {
137137
if revisionLimit <= 0 {
138-
err := fmt.Errorf("invalid placement %s: invalid revisionHistoryLimit %d", placementObj.GetName(), revisionLimit)
138+
err := fmt.Errorf("invalid placement %s/%s: invalid revisionHistoryLimit %d", placementObj.GetNamespace(), placementObj.GetName(), revisionLimit)
139139
klog.ErrorS(controller.NewUnexpectedBehaviorError(err), "Invalid revisionHistoryLimit value and using default value instead", "placement", placementKObj)
140140
} else {
141141
revisionLimit = *placementSpec.RevisionHistoryLimit
@@ -189,7 +189,8 @@ func (r *Reconciler) handleUpdate(ctx context.Context, placementObj fleetv1beta1
189189
klog.ErrorS(err, "Failed to extract the resource index from the resourceSnapshot", "placement", placementKObj, "resourceSnapshot", latestResourceSnapshotKObj)
190190
return ctrl.Result{}, controller.NewUnexpectedBehaviorError(err)
191191
}
192-
selectedResourceIDs, err = controller.CollectResourceIdentifiersUsingMasterResourceSnapshot(ctx, r.Client, placementObj.GetName(), latestResourceSnapshot, strconv.Itoa(latestResourceSnapshotIndex))
192+
placementKey := controller.GetObjectKeyFromNamespaceName(placementObj.GetNamespace(), placementObj.GetName())
193+
selectedResourceIDs, err = controller.CollectResourceIdentifiersUsingMasterResourceSnapshot(ctx, r.Client, placementKey, latestResourceSnapshot, strconv.Itoa(latestResourceSnapshotIndex))
193194
if err != nil {
194195
klog.ErrorS(err, "Failed to collect resource identifiers from the resourceSnapshot", "placement", placementKObj, "resourceSnapshot", latestResourceSnapshotKObj)
195196
return ctrl.Result{}, err
@@ -459,7 +460,7 @@ func (r *Reconciler) getOrCreateResourceSnapshot(ctx context.Context, placement
459460
// check to see all that the master cluster resource snapshot and sub-indexed snapshots belonging to the same group index exists.
460461
resourceSnapshotList, err := controller.ListAllResourceSnapshotWithAnIndex(ctx, r.Client, latestResourceSnapshot.GetLabels()[fleetv1beta1.ResourceIndexLabel], placement.GetName(), placement.GetNamespace())
461462
if err != nil {
462-
klog.ErrorS(err, "Failed to list the latest group resourceSnapshots associated with the placement", "placement", placement.GetName())
463+
klog.ErrorS(err, "Failed to list the latest group resourceSnapshots associated with the placement", "placement", placementKObj)
463464
return ctrl.Result{}, nil, controller.NewAPIServerError(true, err)
464465
}
465466
if len(resourceSnapshotList.GetResourceSnapshotObjs()) == numberOfSnapshots {
@@ -1054,7 +1055,7 @@ func (r *Reconciler) determineRolloutStateForPlacementWithExternalRolloutStrateg
10541055
) (bool, error) {
10551056
if len(selected) == 0 {
10561057
// This should not happen as we already checked in setPlacementStatus.
1057-
err := controller.NewUnexpectedBehaviorError(fmt.Errorf("selected cluster list is empty for placement %s when checking per-cluster rollout state", placementObj.GetName()))
1058+
err := controller.NewUnexpectedBehaviorError(fmt.Errorf("selected cluster list is empty for placement %s/%s when checking per-cluster rollout state", placementObj.GetNamespace(), placementObj.GetName()))
10581059
klog.ErrorS(err, "Should not happen: selected cluster list is empty in determineRolloutStateForPlacementWithExternalRolloutStrategy()")
10591060
return false, err
10601061
}
@@ -1120,10 +1121,7 @@ func (r *Reconciler) determineRolloutStateForPlacementWithExternalRolloutStrateg
11201121
} else {
11211122
placementStatus.ObservedResourceIndex = observedResourceIndex
11221123
// Construct placement key for the resource collection function
1123-
placementKey := placementObj.GetName()
1124-
if placementObj.GetNamespace() != "" {
1125-
placementKey = placementObj.GetNamespace() + "/" + placementObj.GetName()
1126-
}
1124+
placementKey := controller.GetObjectKeyFromNamespaceName(placementObj.GetNamespace(), placementObj.GetName())
11271125
selectedResources, err := controller.CollectResourceIdentifiersFromResourceSnapshot(ctx, r.Client, placementKey, observedResourceIndex)
11281126
if err != nil {
11291127
klog.ErrorS(err, "Failed to collect resource identifiers from resourceSnapshot", "placement", klog.KObj(placementObj), "resourceSnapshotIndex", observedResourceIndex)

pkg/controllers/clusterschedulingpolicysnapshot/controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ type Reconciler struct {
4242

4343
// Reconcile triggers a single placement reconcile round when scheduling policy has changed.
4444
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
45-
var snapshot fleetv1beta1.PolicySnapshotObj
46-
var err error
4745
snapshotKRef := klog.KRef(req.Namespace, req.Name)
4846

4947
startTime := time.Now()
@@ -53,6 +51,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
5351
klog.V(2).InfoS("Reconciliation ends", "policySnapshot", snapshotKRef, "latency", latency)
5452
}()
5553

54+
var snapshot fleetv1beta1.PolicySnapshotObj
55+
var err error
5656
if req.Namespace == "" {
5757
// ClusterSchedulingPolicySnapshot (cluster-scoped)
5858
var clusterSchedulingPolicySnapshot fleetv1beta1.ClusterSchedulingPolicySnapshot
@@ -71,8 +71,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
7171
snapshot = &schedulingPolicySnapshot
7272
}
7373

74-
placementName, exist := snapshot.GetLabels()[fleetv1beta1.PlacementTrackingLabel]
75-
if !exist || len(placementName) == 0 {
74+
placementName := snapshot.GetLabels()[fleetv1beta1.PlacementTrackingLabel]
75+
if len(placementName) == 0 {
7676
err := fmt.Errorf("label %s is missing or has empty value", fleetv1beta1.PlacementTrackingLabel)
7777
klog.ErrorS(err, "Failed to enqueue placement due to invalid schedulingPolicySnapshot", "policySnapshot", snapshotKRef)
7878
return ctrl.Result{}, controller.NewUnexpectedBehaviorError(err)

0 commit comments

Comments
 (0)