@@ -37,6 +37,7 @@ import (
3737
3838 kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
3939 "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
40+ "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/utils"
4041)
4142
4243const (
@@ -56,27 +57,24 @@ type AggregatesController struct {
5657// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;list;watch;create;update;patch;delete
5758
5859func (ac * AggregatesController ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
59- log := logger .FromContext (ctx ).WithName (req .Name )
60- ctx = logger .IntoContext (ctx , log )
61-
60+ log := logger .FromContext (ctx )
6261 hv := & kvmv1.Hypervisor {}
6362 if err := ac .Get (ctx , req .NamespacedName , hv ); err != nil {
6463 return ctrl.Result {}, k8sclient .IgnoreNotFound (err )
6564 }
6665
67- // apply traits only when lifecycle management is enabled
68- if ! hv .Spec .LifecycleEnabled {
69- return ctrl.Result {}, nil
70- }
71-
7266 if slices .Equal (hv .Spec .Aggregates , hv .Status .Aggregates ) {
7367 // Nothing to be done
7468 return ctrl.Result {}, nil
7569 }
7670
7771 aggs , err := aggregatesByName (ctx , ac .computeClient )
7872 if err != nil {
79- return ctrl.Result {}, ac .trackError (ctx , hv , "failed fetching aggregates" , err )
73+ err = fmt .Errorf ("failed listing aggregates: %w" , err )
74+ if err2 := ac .setErrorCondition (ctx , hv , err .Error ()); err2 != nil {
75+ return ctrl.Result {}, errors .Join (err , err2 )
76+ }
77+ return ctrl.Result {}, err
8078 }
8179
8280 toAdd := Difference (hv .Status .Aggregates , hv .Spec .Aggregates )
@@ -91,8 +89,7 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
9189 if len (toAdd ) > 0 {
9290 log .Info ("Adding" , "aggregates" , toAdd )
9391 for item := range slices .Values (toAdd ) {
94- err = addToAggregate (ctx , ac .computeClient , aggs , hv .Name , item , "" )
95- if err != nil {
92+ if err = addToAggregate (ctx , ac .computeClient , aggs , hv .Name , item , "" ); err != nil {
9693 errs = append (errs , err )
9794 }
9895 }
@@ -101,15 +98,18 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
10198 if len (toRemove ) > 0 {
10299 log .Info ("Removing" , "aggregates" , toRemove )
103100 for item := range slices .Values (toRemove ) {
104- err = removeFromAggregate (ctx , ac .computeClient , aggs , hv .Name , item )
105- if err != nil {
101+ if err = removeFromAggregate (ctx , ac .computeClient , aggs , hv .Name , item ); err != nil {
106102 errs = append (errs , err )
107103 }
108104 }
109105 }
110106
111107 if errs != nil {
112- return ctrl.Result {}, ac .trackError (ctx , hv , "failed updating aggregates" , errs ... )
108+ err = fmt .Errorf ("encountered errors during aggregate update: %w" , errors .Join (errs ... ))
109+ if err2 := ac .setErrorCondition (ctx , hv , err .Error ()); err2 != nil {
110+ return ctrl.Result {}, errors .Join (err , err2 )
111+ }
112+ return ctrl.Result {}, err
113113 }
114114
115115 hv .Status .Aggregates = hv .Spec .Aggregates
@@ -122,29 +122,22 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
122122 return ctrl.Result {}, ac .Status ().Update (ctx , hv )
123123}
124124
125- func (ac * AggregatesController ) trackError (ctx context.Context , hv * kvmv1.Hypervisor , msg string , errs ... error ) error {
126- err := errors .Join (errs ... )
127- if err == nil {
128- return nil
129- }
130-
125+ // setErrorCondition sets the error condition on the Hypervisor status, returns error if update fails
126+ func (ac * AggregatesController ) setErrorCondition (ctx context.Context , hv * kvmv1.Hypervisor , msg string ) error {
131127 condition := metav1.Condition {
132128 Type : ConditionTypeAggregatesUpdated ,
133129 Status : metav1 .ConditionFalse ,
134130 Reason : ConditionAggregatesFailed ,
135- Message : err . Error () ,
131+ Message : msg ,
136132 }
137133
138134 if meta .SetStatusCondition (& hv .Status .Conditions , condition ) {
139- if err2 := ac .Status ().Update (ctx , hv ); err2 != nil {
140- return errors . Join ( err , err2 )
135+ if err := ac .Status ().Update (ctx , hv ); err != nil {
136+ return err
141137 }
142- logger .FromContext (ctx ).
143- WithCallDepth (1 ). // Where did we call trackError() from?
144- Error (err , msg )
145138 }
146139
147- return err
140+ return nil
148141}
149142
150143// SetupWithManager sets up the controller with the Manager.
@@ -160,7 +153,8 @@ func (ac *AggregatesController) SetupWithManager(mgr ctrl.Manager) error {
160153
161154 return ctrl .NewControllerManagedBy (mgr ).
162155 Named (AggregatesControllerName ).
163- For (& kvmv1.Hypervisor {}, builder .WithPredicates (predicate.GenerationChangedPredicate {})).
156+ For (& kvmv1.Hypervisor {}, builder .WithPredicates (utils .LifecycleEnabledPredicate )).
157+ WithEventFilter (predicate.GenerationChangedPredicate {}).
164158 Complete (ac )
165159}
166160
0 commit comments