@@ -48,6 +48,7 @@ import (
4848 nropv1 "github.com/openshift-kni/numaresources-operator/api/v1"
4949 "github.com/openshift-kni/numaresources-operator/internal/api/annotations"
5050 "github.com/openshift-kni/numaresources-operator/internal/platforminfo"
51+ intreconcile "github.com/openshift-kni/numaresources-operator/internal/reconcile"
5152 "github.com/openshift-kni/numaresources-operator/internal/relatedobjects"
5253 "github.com/openshift-kni/numaresources-operator/pkg/apply"
5354 "github.com/openshift-kni/numaresources-operator/pkg/hash"
@@ -115,23 +116,44 @@ func (r *NUMAResourcesSchedulerReconciler) Reconcile(ctx context.Context, req ct
115116 }
116117
117118 initialStatus := * instance .Status .DeepCopy ()
119+ if len (initialStatus .Conditions ) == 0 {
120+ instance .Status .Conditions = status .NewNUMAResourcesSchedulerBaseConditions ()
121+ }
118122
119123 if req .Name != objectnames .DefaultNUMAResourcesSchedulerCrName {
120124 message := fmt .Sprintf ("incorrect NUMAResourcesScheduler resource name: %s" , instance .Name )
121- return ctrl.Result {}, r .updateStatus (ctx , initialStatus , instance , status . ConditionDegraded , status .ConditionTypeIncorrectNUMAResourcesSchedulerResourceName , message )
125+ return ctrl.Result {}, r .degradeStatus (ctx , initialStatus , instance , status .ConditionTypeIncorrectNUMAResourcesSchedulerResourceName , message )
122126 }
123127
124128 if annotations .IsPauseReconciliationEnabled (instance .Annotations ) {
125129 klog .InfoS ("Pause reconciliation enabled" , "object" , req .NamespacedName )
126130 return ctrl.Result {}, nil
127131 }
128132
129- result , condition , err := r .reconcileResource (ctx , instance )
130- if err := r .updateStatus (ctx , initialStatus , instance , condition , status .ReasonFromError (err ), status .MessageFromError (err )); err != nil {
131- klog .InfoS ("Failed to update numaresourcesscheduler status" , "Desired condition" , condition , "error" , err )
133+ step := r .reconcileResource (ctx , instance )
134+ if err := r .updateStatus (ctx , initialStatus , instance ); err != nil {
135+ klog .InfoS ("Failed to update numaresourcesscheduler status" , "error" , err )
136+ }
137+
138+ return step .Result , step .Error
139+ }
140+
141+ func (r * NUMAResourcesSchedulerReconciler ) degradeStatus (ctx context.Context , initialStatus nropv1.NUMAResourcesSchedulerStatus , instance * nropv1.NUMAResourcesScheduler , reason string , message string ) error {
142+ condition := metav1.Condition {
143+ Type : status .ConditionDegraded ,
144+ Status : metav1 .ConditionTrue ,
145+ Reason : reason ,
146+ Message : message ,
147+ ObservedGeneration : instance .Generation ,
132148 }
133149
134- return result , err
150+ status .UpdateConditionsInPlace (instance .Status .Conditions , condition , time .Now ())
151+
152+ err := r .updateStatus (ctx , initialStatus , instance )
153+ if err != nil {
154+ klog .InfoS ("Failed to update numaresourcesoperator status" , "error" , err )
155+ }
156+ return err
135157}
136158
137159// SetupWithManager sets up the controller with the Manager.
@@ -183,24 +205,30 @@ func (r *NUMAResourcesSchedulerReconciler) nodeToNUMAResourcesScheduler(ctx cont
183205 return requests
184206}
185207
186- func (r * NUMAResourcesSchedulerReconciler ) reconcileResource (ctx context.Context , instance * nropv1.NUMAResourcesScheduler ) (reconcile. Result , string , error ) {
208+ func (r * NUMAResourcesSchedulerReconciler ) reconcileResource (ctx context.Context , instance * nropv1.NUMAResourcesScheduler ) intreconcile. Step {
187209 schedStatus , err := r .syncNUMASchedulerResources (ctx , instance )
188210 if err != nil {
189- return ctrl.Result {}, status .ConditionDegraded , fmt .Errorf ("FailedSchedulerSync: %w" , err )
211+ step := intreconcile .StepFailed (fmt .Errorf ("FailedSchedulerSync: %w" , err ))
212+ status .UpdateConditionsInPlace (instance .Status .Conditions , step .ConditionInfo .ToMetav1Condition (instance .Generation ), time .Now ())
213+ return step
190214 }
191215
192216 instance .Status = schedStatus
193217 instance .Status .RelatedObjects = relatedobjects .Scheduler (r .Namespace , instance .Status .Deployment )
194218
195219 ok , err := isDeploymentRunning (ctx , r .Client , schedStatus .Deployment )
196220 if err != nil {
197- return ctrl.Result {}, status .ConditionDegraded , err
221+ step := intreconcile .StepFailed (err )
222+ status .UpdateConditionsInPlace (instance .Status .Conditions , step .ConditionInfo .ToMetav1Condition (instance .Generation ), time .Now ())
223+ return step
198224 }
199225 if ! ok {
200- return ctrl.Result {RequeueAfter : 5 * time .Second }, status .ConditionProgressing , nil
226+ step := intreconcile .StepOngoing (5 * time .Second )
227+ status .UpdateConditionsInPlace (instance .Status .Conditions , step .ConditionInfo .ToMetav1Condition (instance .Generation ), time .Now ())
228+ return step
201229 }
202230
203- return ctrl. Result {}, status . ConditionAvailable , nil
231+ return intreconcile . StepSuccess ()
204232}
205233
206234func isDeploymentRunning (ctx context.Context , c client.Client , key nropv1.NamespacedName ) (bool , error ) {
@@ -303,7 +331,7 @@ func (r *NUMAResourcesSchedulerReconciler) syncNUMASchedulerResources(ctx contex
303331 Duration : cacheResyncPeriod ,
304332 }
305333
306- schedStatus . Conditions = updateDedicatedInformerCondition (status . NewNUMAResourcesSchedulerBaseConditions () , * instance , schedSpec )
334+ updateDedicatedInformerCondition (schedStatus . Conditions , * instance , schedSpec )
307335
308336 r .SchedulerManifests .Deployment .Spec .Replicas = schedSpec .Replicas
309337 klog .V (4 ).InfoS ("using scheduler replicas" , "replicas" , * r .SchedulerManifests .Deployment .Spec .Replicas )
@@ -367,52 +395,28 @@ func platformNormalize(spec *nropv1.NUMAResourcesSchedulerSpec, platInfo platfor
367395 klog .V (4 ).InfoS ("SchedulerInformer default is overridden" , "Platform" , platInfo .Platform , "PlatformVersion" , platInfo .Version .String (), "SchedulerInformer" , * spec .SchedulerInformer )
368396}
369397
370- func updateDedicatedInformerCondition (conds []metav1.Condition , instance nropv1.NUMAResourcesScheduler , normalized nropv1.NUMAResourcesSchedulerSpec ) []metav1.Condition {
371- condition := status .FindCondition (conds , status .ConditionDedicatedInformerActive )
372- if condition == nil { // should never happen
373- klog .InfoS ("missing condition: %q" , status .ConditionDedicatedInformerActive )
374- return conds
375- }
376- if normalized .SchedulerInformer == nil { // should never happen
377- klog .InfoS ("nil SchedulerInformer in spec" )
378- condition .Status = metav1 .ConditionUnknown
379- return conds
380- }
398+ func updateDedicatedInformerCondition (conds []metav1.Condition , instance nropv1.NUMAResourcesScheduler , normalized nropv1.NUMAResourcesSchedulerSpec ) {
399+ dedicatedStatus := metav1 .ConditionFalse
381400 if * normalized .SchedulerInformer == nropv1 .SchedulerInformerDedicated {
382- condition .Status = metav1 .ConditionTrue
383- } else {
384- condition .Status = metav1 .ConditionFalse
401+ dedicatedStatus = metav1 .ConditionTrue
385402 }
386- condition .ObservedGeneration = instance .Generation
387- return conds
388- }
389403
390- func (r * NUMAResourcesSchedulerReconciler ) updateStatus (ctx context.Context , initialStatus nropv1.NUMAResourcesSchedulerStatus , sched * nropv1.NUMAResourcesScheduler , condition string , reason string , message string ) error {
391- conds := status .CloneConditions (sched .Status .Conditions )
392- if len (conds ) == 0 {
393- conds = status .NewNUMAResourcesSchedulerBaseConditions ()
394- }
395- ok := status .UpdateConditionsInPlace (conds , metav1.Condition {
396- Type : condition ,
397- Status : metav1 .ConditionTrue ,
398- ObservedGeneration : sched .Generation ,
399- Reason : reason ,
400- Message : message ,
401- }, time .Now ())
402- if ! ok {
403- klog .InfoS ("fail to update condition" , "conditionType" , condition , "reason" , reason , "message" , message )
404+ informerCondition := metav1.Condition {
405+ Type : status .ConditionDedicatedInformerActive ,
406+ Status : dedicatedStatus ,
407+ Reason : status .ConditionDedicatedInformerActive ,
408+ ObservedGeneration : instance .Generation ,
409+ LastTransitionTime : metav1 .Now (),
404410 }
405- // we need to set something anyway
406- sched .Status .Conditions = conds
407411
412+ status .UpdateConditionsInPlace (conds , informerCondition , time.Time {})
413+ }
414+
415+ func (r * NUMAResourcesSchedulerReconciler ) updateStatus (ctx context.Context , initialStatus nropv1.NUMAResourcesSchedulerStatus , sched * nropv1.NUMAResourcesScheduler ) error {
408416 if ! status .NUMAResourcesSchedulerNeedsUpdate (initialStatus , sched .Status ) {
409417 return nil
410418 }
411419
412- if status .EqualConditions (initialStatus .Conditions , sched .Status .Conditions ) {
413- sched .Status .Conditions = initialStatus .Conditions
414- }
415-
416420 if err := r .Client .Status ().Update (ctx , sched ); err != nil {
417421 return fmt .Errorf ("could not update status for object %s: %w" , client .ObjectKeyFromObject (sched ), err )
418422 }
0 commit comments