88
99 corev1 "k8s.io/api/core/v1"
1010 "k8s.io/apimachinery/pkg/api/errors"
11+ "k8s.io/apimachinery/pkg/api/meta"
1112 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1213 "k8s.io/apimachinery/pkg/util/wait"
1314 clientset "k8s.io/client-go/kubernetes"
@@ -331,14 +332,25 @@ func (ctrl *Controller) syncInternalReleaseImage(key string) error {
331332 return nil
332333 }
333334
335+ // Track sync error to update status at the end
336+ var syncErr error
337+
334338 cconfig , err := ctrl .ccLister .Get (ctrlcommon .ControllerConfigName )
335339 if err != nil {
336- return fmt .Errorf ("could not get ControllerConfig %w" , err )
340+ syncErr = fmt .Errorf ("could not get ControllerConfig: %w" , err )
341+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
342+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
343+ }
344+ return syncErr
337345 }
338346
339347 iriSecret , err := ctrl .secretLister .Secrets (ctrlcommon .MCONamespace ).Get (ctrlcommon .InternalReleaseImageTLSSecretName )
340348 if err != nil {
341- return fmt .Errorf ("could not get Secret %s: %w" , ctrlcommon .InternalReleaseImageTLSSecretName , err )
349+ syncErr = fmt .Errorf ("could not get Secret %s: %w" , ctrlcommon .InternalReleaseImageTLSSecretName , err )
350+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
351+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
352+ }
353+ return syncErr
342354 }
343355
344356 for _ , role := range SupportedRoles {
@@ -347,31 +359,60 @@ func (ctrl *Controller) syncInternalReleaseImage(key string) error {
347359 mc , err := ctrl .mcLister .Get (r .GetMachineConfigName ())
348360 isNotFound := errors .IsNotFound (err )
349361 if err != nil && ! isNotFound {
350- return err // syncStatus, could not find MachineConfig
362+ syncErr = fmt .Errorf ("could not get MachineConfig: %w" , err )
363+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
364+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
365+ }
366+ return syncErr
351367 }
352368 if isNotFound {
353369 mc , err = r .CreateEmptyMachineConfig ()
354370 if err != nil {
355- return err // syncStatusOnly, could not create MachineConfig
371+ syncErr = fmt .Errorf ("could not create MachineConfig: %w" , err )
372+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
373+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
374+ }
375+ return syncErr
356376 }
357377 }
358378
359379 err = r .RenderAndSetIgnition (mc )
360380 if err != nil {
361- return err // syncStatus, could not generate IRI configs
381+ syncErr = fmt .Errorf ("could not generate IRI configs: %w" , err )
382+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
383+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
384+ }
385+ return syncErr
362386 }
363387 err = ctrl .createOrUpdateMachineConfig (isNotFound , mc )
364388 if err != nil {
365- return err // syncStatus, could not Create/Update MachineConfig
389+ syncErr = fmt .Errorf ("could not create/update MachineConfig: %w" , err )
390+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
391+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
392+ }
393+ return syncErr
366394 }
367395 if err := ctrl .addFinalizerToInternalReleaseImage (iri , mc ); err != nil {
368- return err // syncStatus , could not add finalizers
396+ syncErr = fmt .Errorf ("could not add finalizer: %w" , err )
397+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
398+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
399+ }
400+ return syncErr
369401 }
370402 }
371403
372404 // Initialize status if empty
373405 if err := ctrl .initializeInternalReleaseImageStatus (iri ); err != nil {
374- return err
406+ syncErr = fmt .Errorf ("could not initialize status: %w" , err )
407+ if statusErr := ctrl .updateInternalReleaseImageStatus (iri , syncErr ); statusErr != nil {
408+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , statusErr )
409+ }
410+ return syncErr
411+ }
412+
413+ // Update status to indicate successful sync (Degraded=False)
414+ if err := ctrl .updateInternalReleaseImageStatus (iri , nil ); err != nil {
415+ klog .Warningf ("Error updating InternalReleaseImage status: %v" , err )
375416 }
376417
377418 return nil
@@ -431,6 +472,48 @@ func (ctrl *Controller) initializeInternalReleaseImageStatus(iri *mcfgv1alpha1.I
431472 return nil
432473}
433474
475+ // updateInternalReleaseImageStatus updates the InternalReleaseImage status conditions
476+ // based on the provided error. If err is nil, it sets Degraded=False, otherwise Degraded=True.
477+ func (ctrl * Controller ) updateInternalReleaseImageStatus (iri * mcfgv1alpha1.InternalReleaseImage , err error ) error {
478+ return retry .RetryOnConflict (updateBackoff , func () error {
479+ // Get the latest version of the IRI to avoid conflicts
480+ latestIRI , getErr := ctrl .iriLister .Get (iri .Name )
481+ if getErr != nil {
482+ return getErr
483+ }
484+ newIRI := latestIRI .DeepCopy ()
485+
486+ // Prepare the condition based on error state
487+ var condition metav1.Condition
488+ if err != nil {
489+ // Set Degraded=True when there's an error
490+ condition = metav1.Condition {
491+ Type : string (mcfgv1alpha1 .InternalReleaseImageStatusConditionTypeDegraded ),
492+ Status : metav1 .ConditionTrue ,
493+ Reason : "SyncError" ,
494+ Message : fmt .Sprintf ("Error syncing InternalReleaseImage: %v" , err ),
495+ ObservedGeneration : newIRI .Generation ,
496+ }
497+ } else {
498+ // Set Degraded=False when sync is successful
499+ condition = metav1.Condition {
500+ Type : string (mcfgv1alpha1 .InternalReleaseImageStatusConditionTypeDegraded ),
501+ Status : metav1 .ConditionFalse ,
502+ Reason : "AsExpected" ,
503+ Message : "InternalReleaseImage controller sync successful" ,
504+ ObservedGeneration : newIRI .Generation ,
505+ }
506+ }
507+
508+ // Update the condition
509+ meta .SetStatusCondition (& newIRI .Status .Conditions , condition )
510+
511+ // Update the status subresource
512+ _ , updateErr := ctrl .client .MachineconfigurationV1alpha1 ().InternalReleaseImages ().UpdateStatus (context .TODO (), newIRI , metav1.UpdateOptions {})
513+ return updateErr
514+ })
515+ }
516+
434517func (ctrl * Controller ) createOrUpdateMachineConfig (isNotFound bool , mc * mcfgv1.MachineConfig ) error {
435518 return retry .RetryOnConflict (updateBackoff , func () error {
436519 var err error
0 commit comments