@@ -27,6 +27,7 @@ import (
2727 apierrors "k8s.io/apimachinery/pkg/api/errors"
2828 "k8s.io/apimachinery/pkg/api/meta"
2929 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+ "k8s.io/apimachinery/pkg/types"
3031 "k8s.io/klog/v2"
3132 "sigs.k8s.io/controller-runtime/pkg/client"
3233
@@ -44,8 +45,8 @@ func (r *Reconciler) initialize(
4445 ctx context.Context ,
4546 updateRun * placementv1beta1.ClusterStagedUpdateRun ,
4647) ([]* placementv1beta1.ClusterResourceBinding , []* placementv1beta1.ClusterResourceBinding , error ) {
47- // Validate the ClusterResourcePlace object referenced by the ClusterStagedUpdateRun .
48- placementName , err := r .validateCRP (ctx , updateRun )
48+ // Validate the Placement object referenced by the StagedUpdateRun .
49+ placementName , err := r .validatePlacement (ctx , updateRun )
4950 if err != nil {
5051 return nil , nil , err
5152 }
@@ -71,29 +72,48 @@ func (r *Reconciler) initialize(
7172 return scheduledBindings , toBeDeletedBindings , r .recordInitializationSucceeded (ctx , updateRun )
7273}
7374
74- // validateCRP validates the ClusterResourcePlacement object referenced by the ClusterStagedUpdateRun .
75- func (r * Reconciler ) validateCRP (ctx context.Context , updateRun * placementv1beta1.ClusterStagedUpdateRun ) (string , error ) {
75+ // validatePlacement validates the Placement object (CRP or RP) referenced by the StagedUpdateRun .
76+ func (r * Reconciler ) validatePlacement (ctx context.Context , updateRun placementv1beta1.StagedUpdateRunObj ) (string , error ) {
7677 updateRunRef := klog .KObj (updateRun )
77- // Fetch the ClusterResourcePlacement object.
78- placementName := updateRun .Spec .PlacementName
79- var crp placementv1beta1.ClusterResourcePlacement
80- if err := r .Client .Get (ctx , client.ObjectKey {Name : placementName }, & crp ); err != nil {
81- klog .ErrorS (err , "Failed to get ClusterResourcePlacement" , "clusterResourcePlacement" , placementName , "clusterStagedUpdateRun" , updateRunRef )
78+
79+ // Get placement name from the updateRun spec using interface methods
80+ placementName := updateRun .GetStagedUpdateRunSpec ().PlacementName
81+
82+ // Create NamespacedName for the placement
83+ // For ClusterStagedUpdateRun, namespace will be empty (cluster-scoped)
84+ // For StagedUpdateRun, namespace will be the actual namespace (namespace-scoped)
85+ namespacedName := types.NamespacedName {
86+ Name : placementName ,
87+ Namespace : updateRun .GetNamespace (),
88+ }
89+
90+ // Fetch the placement object using the utility function
91+ // This automatically determines whether to fetch CRP or RP based on namespace presence
92+ placement , err := controller .FetchPlacementFromNamespacedName (ctx , r .Client , namespacedName )
93+ if err != nil {
8294 if apierrors .IsNotFound (err ) {
83- // we won't continue or retry the initialization if the ClusterResourcePlacement is not found.
84- crpNotFoundErr := controller . NewUserError ( fmt . Errorf ( "parent clusterResourcePlacement not found" ) )
85- return "" , fmt .Errorf ("%w: %s" , errInitializedFailed , crpNotFoundErr .Error ())
95+ placementNotFoundErr := controller . NewUserError ( fmt . Errorf ( "parent placement not found" ))
96+ klog . ErrorS ( err , "Failed to get placement" , "placement" , placementName , "namespace" , updateRun . GetNamespace (), "stagedUpdateRun" , updateRunRef )
97+ return "" , fmt .Errorf ("%w: %s" , errInitializedFailed , placementNotFoundErr .Error ())
8698 }
99+ klog .ErrorS (err , "Failed to get placement" , "placement" , placementName , "namespace" , updateRun .GetNamespace (), "stagedUpdateRun" , updateRunRef )
87100 return "" , controller .NewAPIServerError (true , err )
88101 }
89- // Check if the ClusterResourcePlacement has an external rollout strategy.
90- if crp .Spec .Strategy .Type != placementv1beta1 .ExternalRolloutStrategyType {
91- klog .V (2 ).InfoS ("The clusterResourcePlacement does not have an external rollout strategy" , "clusterResourcePlacement" , placementName , "clusterStagedUpdateRun" , updateRunRef )
92- wrongRolloutTypeErr := controller .NewUserError (errors .New ("parent clusterResourcePlacement does not have an external rollout strategy, current strategy: " + string (crp .Spec .Strategy .Type )))
102+
103+ // Check if the Placement has an external rollout strategy using interface methods
104+ placementSpec := placement .GetPlacementSpec ()
105+ if placementSpec .Strategy .Type != placementv1beta1 .ExternalRolloutStrategyType {
106+ klog .V (2 ).InfoS ("The placement does not have an external rollout strategy" , "placement" , placementName , "namespace" , updateRun .GetNamespace (), "stagedUpdateRun" , updateRunRef )
107+ wrongRolloutTypeErr := controller .NewUserError (errors .New ("parent placement does not have an external rollout strategy, current strategy: " + string (placementSpec .Strategy .Type )))
93108 return "" , fmt .Errorf ("%w: %s" , errInitializedFailed , wrongRolloutTypeErr .Error ())
94109 }
95- updateRun .Status .ApplyStrategy = crp .Spec .Strategy .ApplyStrategy
96- return crp .Name , nil
110+
111+ // Update the apply strategy in the updateRun status using interface methods
112+ updateRunStatus := updateRun .GetStagedUpdateRunStatus ()
113+ updateRunStatus .ApplyStrategy = placementSpec .Strategy .ApplyStrategy
114+ updateRun .SetStagedUpdateRunStatus (* updateRunStatus )
115+
116+ return placementName , nil
97117}
98118
99119// determinePolicySnapshot retrieves the latest policy snapshot associated with the ClusterResourcePlacement,
0 commit comments