@@ -19,15 +19,18 @@ package controller
1919
2020import (
2121 "context"
22+ "errors"
2223 "fmt"
2324 "slices"
2425
2526 "k8s.io/apimachinery/pkg/api/meta"
2627 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728 "k8s.io/apimachinery/pkg/runtime"
2829 ctrl "sigs.k8s.io/controller-runtime"
30+ "sigs.k8s.io/controller-runtime/pkg/builder"
2931 k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
3032 logger "sigs.k8s.io/controller-runtime/pkg/log"
33+ "sigs.k8s.io/controller-runtime/pkg/predicate"
3134
3235 "github.com/gophercloud/gophercloud/v2"
3336 "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/aggregates"
@@ -73,30 +76,19 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
7376
7477 aggs , err := aggregatesByName (ctx , ac .computeClient )
7578 if err != nil {
76- meta .SetStatusCondition (& hv .Status .Conditions , metav1.Condition {
77- Type : ConditionTypeAggregatesUpdated ,
78- Status : metav1 .ConditionFalse ,
79- Reason : ConditionAggregatesFailed ,
80- Message : err .Error (),
81- })
82- return ctrl.Result {}, ac .Status ().Update (ctx , hv )
79+ return ac .errorCondition (ctx , hv , err )
8380 }
8481
8582 toAdd := Difference (hv .Status .Aggregates , hv .Spec .Aggregates )
8683 toRemove := Difference (hv .Spec .Aggregates , hv .Status .Aggregates )
8784
85+ var errs []error
8886 if len (toAdd ) > 0 {
8987 log .Info ("Adding" , "aggregates" , toAdd )
9088 for item := range slices .Values (toAdd ) {
9189 err = addToAggregate (ctx , ac .computeClient , aggs , hv .Name , item , "" )
9290 if err != nil {
93- meta .SetStatusCondition (& hv .Status .Conditions , metav1.Condition {
94- Type : ConditionTypeAggregatesUpdated ,
95- Status : metav1 .ConditionFalse ,
96- Reason : ConditionAggregatesFailed ,
97- Message : err .Error (),
98- })
99- return ctrl.Result {}, ac .Status ().Update (ctx , hv )
91+ errs = append (errs , err )
10092 }
10193 }
10294 }
@@ -106,17 +98,16 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
10698 for item := range slices .Values (toRemove ) {
10799 err = removeFromAggregate (ctx , ac .computeClient , aggs , hv .Name , item )
108100 if err != nil {
109- meta .SetStatusCondition (& hv .Status .Conditions , metav1.Condition {
110- Type : ConditionTypeAggregatesUpdated ,
111- Status : metav1 .ConditionFalse ,
112- Reason : ConditionAggregatesFailed ,
113- Message : err .Error (),
114- })
115- return ctrl.Result {}, ac .Status ().Update (ctx , hv )
101+ errs = append (errs , err )
116102 }
117103 }
118104 }
119105
106+ err = errors .Join (errs ... )
107+ if err != nil {
108+ return ac .errorCondition (ctx , hv , err )
109+ }
110+
120111 hv .Status .Aggregates = hv .Spec .Aggregates
121112 meta .SetStatusCondition (& hv .Status .Conditions , metav1.Condition {
122113 Type : ConditionTypeAggregatesUpdated ,
@@ -127,6 +118,23 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
127118 return ctrl.Result {}, ac .Status ().Update (ctx , hv )
128119}
129120
121+ func (ac * AggregatesController ) errorCondition (ctx context.Context , hv * kvmv1.Hypervisor , err error ) (ctrl.Result , error ) {
122+ condition := metav1.Condition {
123+ Type : ConditionTypeAggregatesUpdated ,
124+ Status : metav1 .ConditionFalse ,
125+ Reason : ConditionAggregatesFailed ,
126+ Message : err .Error (),
127+ }
128+
129+ if meta .SetStatusCondition (& hv .Status .Conditions , condition ) {
130+ if err2 := ac .Status ().Update (ctx , hv ); err2 != nil {
131+ return ctrl.Result {}, err2
132+ }
133+ }
134+
135+ return ctrl.Result {RequeueAfter : defaultWaitTime }, nil
136+ }
137+
130138// SetupWithManager sets up the controller with the Manager.
131139func (ac * AggregatesController ) SetupWithManager (mgr ctrl.Manager ) error {
132140 ctx := context .Background ()
@@ -140,7 +148,7 @@ func (ac *AggregatesController) SetupWithManager(mgr ctrl.Manager) error {
140148
141149 return ctrl .NewControllerManagedBy (mgr ).
142150 Named (AggregatesControllerName ).
143- For (& kvmv1.Hypervisor {}).
151+ For (& kvmv1.Hypervisor {}, builder . WithPredicates (predicate. GenerationChangedPredicate {}) ).
144152 Complete (ac )
145153}
146154
0 commit comments