@@ -969,6 +969,7 @@ func (r *Reconciler) listSortedResourceSnapshots(ctx context.Context, placementO
969969}
970970
971971// setPlacementStatus returns if there is a cluster scheduled by the scheduler.
972+ // it returns true if the cluster schedule succeeded, false otherwise.
972973func (r * Reconciler ) setPlacementStatus (
973974 ctx context.Context ,
974975 placementObj fleetv1beta1.PlacementObj ,
@@ -997,11 +998,8 @@ func (r *Reconciler) setPlacementStatus(
997998 // TODO: need to track whether we have deleted the resources for the last decisions.
998999 // The undeleted resources on these old clusters could lead to failed synchronized or applied condition.
9991000 // Today, we only track the resources progress if the same cluster is selected again.
1000-
1001- // For now, only CRP supports placement statuses - namespace-scoped placements would need different handling
1002- if crp , ok := placementObj .(* fleetv1beta1.ClusterResourcePlacement ); ok {
1003- crp .Status .PlacementStatuses = []fleetv1beta1.ResourcePlacementStatus {}
1004- }
1001+ klog .V (2 ).InfoS ("Resetting the resource placement status since scheduled condition is unknown" , "placement" , klog .KObj (placementObj ))
1002+ placementStatus .PlacementStatuses = []fleetv1beta1.ResourcePlacementStatus {}
10051003 return false , nil
10061004 }
10071005
@@ -1032,43 +1030,47 @@ func (r *Reconciler) setPlacementStatus(
10321030
10331031 // For clusters that failed to get scheduled, set a resource placement status with the
10341032 // failed to schedule condition for each of them.
1035- // This is currently CRP-specific functionality
1036- if crp , ok := placementObj .(* fleetv1beta1.ClusterResourcePlacement ); ok {
1037- allRPS = appendFailedToScheduleResourcePlacementStatuses (allRPS , unselected , failedToScheduleClusterCount , crp )
1038-
1039- crp .Status .PlacementStatuses = allRPS
1033+ allRPS = appendFailedToScheduleResourcePlacementStatuses (allRPS , unselected , failedToScheduleClusterCount , placementObj )
1034+ placementStatus .PlacementStatuses = allRPS
1035+ klog .V (2 ).InfoS ("Updated placement status for each individual cluster" , "selectedNoCluster" , len (selected ), "unselectedNoCluster" , len (unselected ), "failedToScheduleClusterCount" , failedToScheduleClusterCount , "placement" , klog .KObj (placementObj ))
10401036
1041- // Prepare the conditions for the CRP object itself.
1042-
1043- if len (selected ) == 0 {
1044- // There is no selected cluster at all. It could be that there is no matching cluster
1045- // given the current scheduling policy; there remains a corner case as well where a cluster
1046- // has been selected before (with resources being possibly applied), but has now
1047- // left the fleet. To address this corner case, Fleet here will remove all lingering
1048- // conditions (any condition type other than CRPScheduled).
1037+ // Prepare the conditions for the placement object itself.
1038+ policy := placementObj . GetPlacementSpec (). Policy
1039+ if len (selected ) == 0 {
1040+ // There is no selected cluster at all. It could be that there is no matching cluster
1041+ // given the current scheduling policy; there remains a corner case as well where a cluster
1042+ // has been selected before (with resources being possibly applied), but has now
1043+ // left the fleet. To address this corner case, Fleet here will remove all lingering
1044+ // conditions (any condition type other than CRPScheduled).
10491045
1050- // Note that the scheduled condition has been set earlier in this method.
1051- crp . Status . Conditions = []metav1.Condition {* crp .GetCondition (string ( fleetv1beta1 . ClusterResourcePlacementScheduledConditionType ))}
1052- return false , nil
1053- }
1046+ // Note that the scheduled condition has been set earlier in this method.
1047+ placementStatus . Conditions = []metav1.Condition {* placementObj .GetCondition (getPlacementScheduledConditionType ( placementObj ))}
1048+ return isPolicySelectingNoClusters ( policy ) , nil
1049+ }
10541050
1055- if crp .Spec .Strategy .Type == fleetv1beta1 .ExternalRolloutStrategyType {
1056- // For external rollout strategy, if clusters observe different resource snapshot versions,
1057- // we set RolloutStarted to Unknown without any other conditions since we do not know exactly which version is rolling out.
1058- // We also need to reset ObservedResourceIndex and selectedResources.
1059- rolloutStartedUnknown , err := r .determineRolloutStateForPlacementWithExternalRolloutStrategy (ctx , placementObj , selected , allRPS , selectedResourceIDs )
1060- if err != nil || rolloutStartedUnknown {
1061- return true , err
1062- }
1051+ if placementObj .GetPlacementSpec ().Strategy .Type == fleetv1beta1 .ExternalRolloutStrategyType {
1052+ // For external rollout strategy, if clusters observe different resource snapshot versions,
1053+ // we set RolloutStarted to Unknown without any other conditions since we do not know exactly which version is rolling out.
1054+ // We also need to reset ObservedResourceIndex and selectedResources.
1055+ rolloutStartedUnknown , err := r .determineRolloutStateForPlacementWithExternalRolloutStrategy (ctx , placementObj , selected , allRPS , selectedResourceIDs )
1056+ if err != nil || rolloutStartedUnknown {
1057+ return true , err
10631058 }
1064-
1065- setCRPConditions (crp , allRPS , rpsSetCondTypeCounter , expectedCondTypes )
10661059 }
1067-
1060+ setCRPConditions (placementObj , allRPS , rpsSetCondTypeCounter , expectedCondTypes )
1061+ klog .V (2 ).InfoS ("Updated placement status for the entire placement" , "placement" , klog .KObj (placementObj ))
10681062 return true , nil
10691063}
10701064
1071- // TODO: This currently only works for cluster-scoped placements due to condition type differences
1065+ // isPolicySelectingNoClusters checks if the given placement policy is selecting no clusters by design.
1066+ func isPolicySelectingNoClusters (policy * fleetv1beta1.PlacementPolicy ) bool {
1067+ if policy == nil {
1068+ return false
1069+ }
1070+ return (policy .PlacementType == fleetv1beta1 .PickNPlacementType && * policy .NumberOfClusters == 0 ) || (policy .PlacementType == fleetv1beta1 .PickFixedPlacementType && len (policy .ClusterNames ) == 0 )
1071+ }
1072+
1073+ // determineRolloutStateForPlacementWithExternalRolloutStrategy checks the rollout state for the placement with external rollout strategy.
10721074func (r * Reconciler ) determineRolloutStateForPlacementWithExternalRolloutStrategy (
10731075 ctx context.Context ,
10741076 placementObj fleetv1beta1.PlacementObj ,
@@ -1100,7 +1102,7 @@ func (r *Reconciler) determineRolloutStateForPlacementWithExternalRolloutStrateg
11001102 placementStatus .ObservedResourceIndex = ""
11011103 placementStatus .SelectedResources = []fleetv1beta1.ResourceIdentifier {}
11021104 placementObj .SetConditions (metav1.Condition {
1103- Type : string ( fleetv1beta1 . ClusterResourcePlacementRolloutStartedConditionType ),
1105+ Type : getPlacementRolloutStartedConditionType ( placementObj ),
11041106 Status : metav1 .ConditionUnknown ,
11051107 Reason : condition .RolloutControlledByExternalControllerReason ,
11061108 Message : "Rollout is controlled by an external controller and different resource snapshot versions are observed across clusters" ,
@@ -1121,7 +1123,7 @@ func (r *Reconciler) determineRolloutStateForPlacementWithExternalRolloutStrateg
11211123 placementStatus .ObservedResourceIndex = ""
11221124 placementStatus .SelectedResources = []fleetv1beta1.ResourceIdentifier {}
11231125 placementObj .SetConditions (metav1.Condition {
1124- Type : string ( fleetv1beta1 . ClusterResourcePlacementRolloutStartedConditionType ),
1126+ Type : getPlacementRolloutStartedConditionType ( placementObj ),
11251127 Status : metav1 .ConditionUnknown ,
11261128 Reason : condition .RolloutControlledByExternalControllerReason ,
11271129 Message : "Rollout is controlled by an external controller and no resource snapshot name is observed across clusters, probably rollout has not started yet" ,
@@ -1163,7 +1165,7 @@ func (r *Reconciler) determineRolloutStateForPlacementWithExternalRolloutStrateg
11631165 klog .V (2 ).InfoS ("Placement has External rollout strategy and some cluster is in RolloutStarted Unknown state, set RolloutStarted condition to Unknown" ,
11641166 "clusterName" , allRPS [i ].ClusterName , "observedResourceIndex" , observedResourceIndex , "placement" , klog .KObj (placementObj ))
11651167 placementObj .SetConditions (metav1.Condition {
1166- Type : string ( fleetv1beta1 . ClusterResourcePlacementRolloutStartedConditionType ),
1168+ Type : getPlacementRolloutStartedConditionType ( placementObj ),
11671169 Status : metav1 .ConditionUnknown ,
11681170 Reason : condition .RolloutControlledByExternalControllerReason ,
11691171 Message : fmt .Sprintf ("Rollout is controlled by an external controller and cluster %s is in RolloutStarted Unknown state" , allRPS [i ].ClusterName ),
@@ -1179,6 +1181,28 @@ func (r *Reconciler) determineRolloutStateForPlacementWithExternalRolloutStrateg
11791181 return false , nil
11801182}
11811183
1184+ // TODO: determine the actual condition type for RP
1185+ // getPlacementScheduledConditionType returns the appropriate scheduled condition type based on the placement type.
1186+ func getPlacementScheduledConditionType (placementObj fleetv1beta1.PlacementObj ) string {
1187+ if placementObj .GetNamespace () == "" {
1188+ // Cluster-scoped placement
1189+ return string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType )
1190+ }
1191+ // Namespace-scoped placement
1192+ return string (fleetv1beta1 .ClusterResourcePlacementScheduledConditionType )
1193+ }
1194+
1195+ // TODO: determine the actual condition type for RP
1196+ // getPlacementRolloutStartedConditionType returns the appropriate rollout started condition type based on the placement type.
1197+ func getPlacementRolloutStartedConditionType (placementObj fleetv1beta1.PlacementObj ) string {
1198+ if placementObj .GetNamespace () == "" {
1199+ // Cluster-scoped placement
1200+ return string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType )
1201+ }
1202+ // Namespace-scoped placement
1203+ return string (fleetv1beta1 .ClusterResourcePlacementRolloutStartedConditionType )
1204+ }
1205+
11821206func buildScheduledCondition (placementObj fleetv1beta1.PlacementObj , latestSchedulingPolicySnapshot fleetv1beta1.PolicySnapshotObj ) metav1.Condition {
11831207 scheduledCondition := latestSchedulingPolicySnapshot .GetCondition (string (fleetv1beta1 .PolicySnapshotScheduled ))
11841208
@@ -1191,15 +1215,15 @@ func buildScheduledCondition(placementObj fleetv1beta1.PlacementObj, latestSched
11911215 scheduledCondition .Status == metav1 .ConditionUnknown {
11921216 return metav1.Condition {
11931217 Status : metav1 .ConditionUnknown ,
1194- Type : string ( fleetv1beta1 . ClusterResourcePlacementScheduledConditionType ),
1218+ Type : getPlacementScheduledConditionType ( placementObj ),
11951219 Reason : condition .SchedulingUnknownReason ,
11961220 Message : "Scheduling has not completed" ,
11971221 ObservedGeneration : placementObj .GetGeneration (),
11981222 }
11991223 }
12001224 return metav1.Condition {
12011225 Status : scheduledCondition .Status ,
1202- Type : string ( fleetv1beta1 . ClusterResourcePlacementScheduledConditionType ),
1226+ Type : getPlacementScheduledConditionType ( placementObj ),
12031227 Reason : scheduledCondition .Reason ,
12041228 Message : scheduledCondition .Message ,
12051229 ObservedGeneration : placementObj .GetGeneration (),
0 commit comments