Skip to content

Commit

Permalink
Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelawyu committed Jan 18, 2025
1 parent 7fc17c0 commit 0d057e7
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 198 deletions.
73 changes: 54 additions & 19 deletions pkg/controllers/clusterresourceplacement/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ func (r *Reconciler) handleUpdate(ctx context.Context, crp *fleetv1beta1.Cluster
}
}

// There is no need to check if the CRP is available or not.
// If the available condition is true, it means the rollout is completed.
// Rollout is considered to be completed when all the expected condition types are set to the
// True status.
if isRolloutCompleted(crp) {
if !isRolloutCompleted(oldCRP) {
klog.V(2).InfoS("Placement rollout has finished and resources are available", "clusterResourcePlacement", crpKObj, "generation", crp.Generation)
r.Recorder.Event(crp, corev1.EventTypeNormal, "PlacementRolloutCompleted", "Resources are available in the selected clusters")
klog.V(2).InfoS("Placement has finished the rollout process and reached the desired status", "clusterResourcePlacement", crpKObj, "generation", crp.Generation)
r.Recorder.Event(crp, corev1.EventTypeNormal, "PlacementRolloutCompleted", "Placement has finished the rollout process and reached the desired status")
}
// We don't need to requeue any request now by watching the binding changes
return ctrl.Result{}, nil
Expand Down Expand Up @@ -888,8 +888,13 @@ func parseResourceGroupHashFromAnnotation(s *fleetv1beta1.ClusterResourceSnapsho
}

// setPlacementStatus returns if there is a cluster scheduled by the scheduler.
func (r *Reconciler) setPlacementStatus(ctx context.Context, crp *fleetv1beta1.ClusterResourcePlacement, selectedResourceIDs []fleetv1beta1.ResourceIdentifier,
latestSchedulingPolicySnapshot *fleetv1beta1.ClusterSchedulingPolicySnapshot, latestResourceSnapshot *fleetv1beta1.ClusterResourceSnapshot) (bool, error) {
func (r *Reconciler) setPlacementStatus(
ctx context.Context,
crp *fleetv1beta1.ClusterResourcePlacement,
selectedResourceIDs []fleetv1beta1.ResourceIdentifier,
latestSchedulingPolicySnapshot *fleetv1beta1.ClusterSchedulingPolicySnapshot,
latestResourceSnapshot *fleetv1beta1.ClusterResourceSnapshot,
) (bool, error) {
crp.Status.SelectedResources = selectedResourceIDs
scheduledCondition := buildScheduledCondition(crp, latestSchedulingPolicySnapshot)
crp.SetConditions(scheduledCondition)
Expand All @@ -913,7 +918,47 @@ func (r *Reconciler) setPlacementStatus(ctx context.Context, crp *fleetv1beta1.C
return false, nil
}

return r.setResourceConditions(ctx, crp, latestSchedulingPolicySnapshot, latestResourceSnapshot)
// Classify cluster decisions; find out clusters that have been selected and
// have not been selected.
selected, unselected := classifyClusterDecisions(latestSchedulingPolicySnapshot.Status.ClusterDecisions)
// Calculate the number of clusters that should have been selected yet cannot be, due to
// scheduling constraints.
failedToScheduleClusterCount := calculateFailedToScheduleClusterCount(crp, selected, unselected)

// Prepare the resource placement status (status per cluster) in the CRP status.
allRPS := make([]fleetv1beta1.ResourcePlacementStatus, 0, len(latestSchedulingPolicySnapshot.Status.ClusterDecisions))

// For clusters that have been selected, set the resource placement status based on the
// respective resource binding status for each of them.
expectedCondTypes := determineExpectedCRPAndResourcePlacementStatusCondType(crp)
allRPS, rpsSetCondTypeCounter, err := r.setScheduledResourcePlacementStatuses(
ctx, allRPS, selected, expectedCondTypes, crp, latestSchedulingPolicySnapshot, latestResourceSnapshot)
if err != nil {
return false, err
}

// For clusters that failed to get scheduled, set a resource placement status with the
// failed to schedule condition for each of them.
allRPS = setFailedToScheduleResourcePlacementStatuses(allRPS, unselected, failedToScheduleClusterCount, klog.KObj(crp), crp.Generation)

crp.Status.PlacementStatuses = allRPS

// Prepare the conditions for the CRP object itself.

if len(selected) == 0 {
// There is no selected cluster at all. It could be that there is no matching cluster
// given the current scheduling policy; there remains a corner case as well where a cluster
// has been selected before (with resources being possibly applied), but has now
// left the fleet. To address this corner case, Fleet here will remove all lingering
// conditions (any condition type other than CRPScheduled).

// Note that the scheduled condition has been set earlier in this method.
crp.Status.Conditions = []metav1.Condition{*crp.GetCondition(string(fleetv1beta1.ClusterResourcePlacementScheduledConditionType))}
return false, nil
}

setCRPConditions(crp, allRPS, rpsSetCondTypeCounter, expectedCondTypes)
return true, nil
}

func buildScheduledCondition(crp *fleetv1beta1.ClusterResourcePlacement, latestSchedulingPolicySnapshot *fleetv1beta1.ClusterSchedulingPolicySnapshot) metav1.Condition {
Expand Down Expand Up @@ -974,18 +1019,8 @@ func isRolloutCompleted(crp *fleetv1beta1.ClusterResourcePlacement) bool {
return false
}

skippedCondTypes := skippedCondTypesForCSASSAApplyStrategyTypes
if crp.Spec.Strategy.ApplyStrategy != nil && crp.Spec.Strategy.ApplyStrategy.Type == fleetv1beta1.ApplyStrategyTypeReportDiff {
skippedCondTypes = skippedCondTypesForReportDiffApplyStrategyType
}
for i := condition.RolloutStartedCondition; i < condition.TotalCondition; i++ {
// If the CRP has an apply strategy of the ClientSideApply or ServerSideApply type, skip
// checking the DiffReported condition type; similarly, should the apply strategy be of
// the ReportDiff type, Fleet will skip checking the Applied and Available condition
// type.
if _, ok := skippedCondTypes[i]; ok {
continue
}
expectedCondTypes := determineExpectedCRPAndResourcePlacementStatusCondType(crp)
for _, i := range expectedCondTypes {
if !condition.IsConditionStatusTrue(crp.GetCondition(string(i.ClusterResourcePlacementConditionType())), crp.Generation) {
return false
}
Expand Down
Loading

0 comments on commit 0d057e7

Please sign in to comment.