@@ -15,6 +15,7 @@ import (
1515 "google.golang.org/grpc/status"
1616
1717 "github.com/ironcore-dev/controller-utils/clientutils"
18+ "github.com/ironcore-dev/controller-utils/conditionutils"
1819 commonv1alpha1 "github.com/ironcore-dev/ironcore/api/common/v1alpha1"
1920 computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
2021 ipamv1alpha1 "github.com/ironcore-dev/ironcore/api/ipam/v1alpha1"
@@ -502,43 +503,42 @@ func (r *MachineReconciler) updateMachineStatus(ctx context.Context, machine *co
502503 machine .Status .ObservedGeneration = generation
503504 machine .Status .Volumes = volumeStatuses
504505 machine .Status .NetworkInterfaces = nicStatuses
505- machine .Status .Conditions = r .computeMachineConditions (state , volumeStatuses , nicStatuses , now )
506+ if err := ComputeMachineConditions (& machine .Status .Conditions , state , volumeStatuses , nicStatuses ); err != nil {
507+ return fmt .Errorf ("error computing machine conditions: %w" , err )
508+ }
506509
507510 if err := r .Status ().Patch (ctx , machine , client .MergeFrom (base )); err != nil {
508511 return fmt .Errorf ("error patching status: %w" , err )
509512 }
510513 return nil
511514}
512515
513- // computeMachineConditions computes the conditions for the machine based on its current state.
514- func ( r * MachineReconciler ) computeMachineConditions (
516+ func ComputeMachineConditions (
517+ conditions * []computev1alpha1. MachineCondition ,
515518 state computev1alpha1.MachineState ,
516519 volumeStatuses []computev1alpha1.VolumeStatus ,
517520 nicStatuses []computev1alpha1.NetworkInterfaceStatus ,
518- now metav1.Time ,
519- ) []computev1alpha1.MachineCondition {
520- var conditions []computev1alpha1.MachineCondition
521-
522- conditions = append (conditions , r .computeMachineReadyCondition (state , now ))
523-
521+ ) error {
522+ desired := []computev1alpha1.MachineCondition {computeMachineReadyCondition (state )}
524523 if len (volumeStatuses ) > 0 {
525- if c := r .computeVolumesReadyCondition (volumeStatuses , now ); c .Type != "" {
526- conditions = append (conditions , c )
527- }
524+ desired = append (desired , computeVolumesReadyCondition (volumeStatuses ))
528525 }
529-
530526 if len (nicStatuses ) > 0 {
531- if c := r .computeNetworkInterfacesReadyCondition (nicStatuses , now ); c .Type != "" {
532- conditions = append (conditions , c )
533- }
527+ desired = append (desired , computeNetworkInterfacesReadyCondition (nicStatuses ))
534528 }
535529
536- return conditions
530+ for _ , cond := range desired {
531+ if err := conditionutils .UpdateSlice (conditions , string (cond .Type ),
532+ conditionutils.UpdateFromCondition {Condition : cond },
533+ ); err != nil {
534+ return fmt .Errorf ("error updating %s condition: %w" , cond .Type , err )
535+ }
536+ }
537+ return nil
537538}
538539
539- func ( r * MachineReconciler ) computeMachineReadyCondition (state computev1alpha1.MachineState , now metav1. Time ) computev1alpha1.MachineCondition {
540+ func computeMachineReadyCondition (state computev1alpha1.MachineState ) computev1alpha1.MachineCondition {
540541 status , reason , message := corev1 .ConditionFalse , "NotReady" , "Machine is not ready"
541-
542542 switch state {
543543 case computev1alpha1 .MachineStateRunning :
544544 status , reason , message = corev1 .ConditionTrue , "Running" , "Machine is running"
@@ -547,23 +547,11 @@ func (r *MachineReconciler) computeMachineReadyCondition(state computev1alpha1.M
547547 case computev1alpha1 .MachineStateTerminating , computev1alpha1 .MachineStateTerminated :
548548 status , reason , message = corev1 .ConditionFalse , "Terminating" , "Machine is terminating or terminated"
549549 }
550-
551- return computev1alpha1.MachineCondition {
552- Type : "Ready" ,
553- Status : status ,
554- Reason : reason ,
555- Message : message ,
556- LastTransitionTime : now ,
557- }
550+ return computev1alpha1.MachineCondition {Type : "Ready" , Status : status , Reason : reason , Message : message }
558551}
559552
560- func (r * MachineReconciler ) computeVolumesReadyCondition (volumeStatuses []computev1alpha1.VolumeStatus , now metav1.Time ) computev1alpha1.MachineCondition {
561- if len (volumeStatuses ) == 0 {
562- return computev1alpha1.MachineCondition {}
563- }
564-
553+ func computeVolumesReadyCondition (volumeStatuses []computev1alpha1.VolumeStatus ) computev1alpha1.MachineCondition {
565554 status , reason , message := corev1 .ConditionTrue , "VolumesReady" , "All volumes are ready"
566-
567555 for _ , vs := range volumeStatuses {
568556 if vs .State != computev1alpha1 .VolumeStateAttached {
569557 status = corev1 .ConditionFalse
@@ -572,23 +560,11 @@ func (r *MachineReconciler) computeVolumesReadyCondition(volumeStatuses []comput
572560 break
573561 }
574562 }
575-
576- return computev1alpha1.MachineCondition {
577- Type : computev1alpha1 .MachineConditionType ("VolumesReady" ),
578- Status : status ,
579- Reason : reason ,
580- Message : message ,
581- LastTransitionTime : now ,
582- }
563+ return computev1alpha1.MachineCondition {Type : "VolumesReady" , Status : status , Reason : reason , Message : message }
583564}
584565
585- func (r * MachineReconciler ) computeNetworkInterfacesReadyCondition (nicStatuses []computev1alpha1.NetworkInterfaceStatus , now metav1.Time ) computev1alpha1.MachineCondition {
586- if len (nicStatuses ) == 0 {
587- return computev1alpha1.MachineCondition {}
588- }
589-
566+ func computeNetworkInterfacesReadyCondition (nicStatuses []computev1alpha1.NetworkInterfaceStatus ) computev1alpha1.MachineCondition {
590567 status , reason , message := corev1 .ConditionTrue , "NetworkInterfacesReady" , "All network interfaces are ready"
591-
592568 for _ , nicStatus := range nicStatuses {
593569 if nicStatus .State != computev1alpha1 .NetworkInterfaceStateAttached {
594570 status = corev1 .ConditionFalse
@@ -597,14 +573,7 @@ func (r *MachineReconciler) computeNetworkInterfacesReadyCondition(nicStatuses [
597573 break
598574 }
599575 }
600-
601- return computev1alpha1.MachineCondition {
602- Type : computev1alpha1 .MachineConditionType ("NetworkInterfacesReady" ),
603- Status : status ,
604- Reason : reason ,
605- Message : message ,
606- LastTransitionTime : now ,
607- }
576+ return computev1alpha1.MachineCondition {Type : "NetworkInterfacesReady" , Status : status , Reason : reason , Message : message }
608577}
609578
610579func (r * MachineReconciler ) prepareIRIPower (power computev1alpha1.Power ) (iri.Power , error ) {
0 commit comments