@@ -32,7 +32,6 @@ import (
3232 logger "sigs.k8s.io/controller-runtime/pkg/log"
3333
3434 "github.com/gophercloud/gophercloud/v2"
35- "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/aggregates"
3635
3736 kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
3837 "github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
@@ -70,7 +69,7 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
7069 return ctrl.Result {}, nil
7170 }
7271
73- aggs , err := aggregatesByName (ctx , ac .computeClient )
72+ aggs , err := openstack . GetAggregatesByName (ctx , ac .computeClient )
7473 if err != nil {
7574 err = fmt .Errorf ("failed listing aggregates: %w" , err )
7675 if err2 := ac .setErrorCondition (ctx , hv , err .Error ()); err2 != nil {
@@ -91,7 +90,7 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
9190 if len (toAdd ) > 0 {
9291 log .Info ("Adding" , "aggregates" , toAdd )
9392 for item := range slices .Values (toAdd ) {
94- if err = addToAggregate (ctx , ac .computeClient , aggs , hv .Name , item , "" ); err != nil {
93+ if err = openstack . AddToAggregate (ctx , ac .computeClient , aggs , hv .Name , item , "" ); err != nil {
9594 errs = append (errs , err )
9695 }
9796 }
@@ -100,7 +99,7 @@ func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request)
10099 if len (toRemove ) > 0 {
101100 log .Info ("Removing" , "aggregates" , toRemove )
102101 for item := range slices .Values (toRemove ) {
103- if err = removeFromAggregate (ctx , ac .computeClient , aggs , hv .Name , item ); err != nil {
102+ if err = openstack . RemoveFromAggregate (ctx , ac .computeClient , aggs , hv .Name , item ); err != nil {
104103 errs = append (errs , err )
105104 }
106105 }
@@ -162,81 +161,3 @@ func (ac *AggregatesController) SetupWithManager(mgr ctrl.Manager) error {
162161 For (& kvmv1.Hypervisor {}, builder .WithPredicates (utils .LifecycleEnabledPredicate )).
163162 Complete (ac )
164163}
165-
166- func aggregatesByName (ctx context.Context , serviceClient * gophercloud.ServiceClient ) (map [string ]* aggregates.Aggregate , error ) {
167- pages , err := aggregates .List (serviceClient ).AllPages (ctx )
168- if err != nil {
169- return nil , fmt .Errorf ("cannot list aggregates due to %w" , err )
170- }
171-
172- aggs , err := aggregates .ExtractAggregates (pages )
173- if err != nil {
174- return nil , fmt .Errorf ("cannot list aggregates due to %w" , err )
175- }
176-
177- aggregateMap := make (map [string ]* aggregates.Aggregate , len (aggs ))
178- for _ , aggregate := range aggs {
179- aggregateMap [aggregate .Name ] = & aggregate
180- }
181- return aggregateMap , nil
182- }
183-
184- func addToAggregate (ctx context.Context , serviceClient * gophercloud.ServiceClient , aggs map [string ]* aggregates.Aggregate , host , name , zone string ) (err error ) {
185- aggregate , found := aggs [name ]
186- log := logger .FromContext (ctx )
187- if ! found {
188- aggregate , err = aggregates .Create (ctx , serviceClient ,
189- aggregates.CreateOpts {
190- Name : name ,
191- AvailabilityZone : zone ,
192- }).Extract ()
193- if err != nil {
194- return fmt .Errorf ("failed to create aggregate %v due to %w" , name , err )
195- }
196- aggs [name ] = aggregate
197- }
198-
199- if slices .Contains (aggregate .Hosts , host ) {
200- log .Info ("Found host in aggregate" , "host" , host , "name" , name )
201- return nil
202- }
203-
204- result , err := aggregates .AddHost (ctx , serviceClient , aggregate .ID , aggregates.AddHostOpts {Host : host }).Extract ()
205- if err != nil {
206- return fmt .Errorf ("failed to add host %v to aggregate %v due to %w" , host , name , err )
207- }
208- log .Info ("Added host to aggregate" , "host" , host , "name" , name )
209- aggs [name ] = result
210-
211- return nil
212- }
213-
214- func removeFromAggregate (ctx context.Context , serviceClient * gophercloud.ServiceClient , aggs map [string ]* aggregates.Aggregate , host , name string ) error {
215- aggregate , found := aggs [name ]
216- log := logger .FromContext (ctx )
217- if ! found {
218- log .Info ("cannot find aggregate" , "name" , name )
219- return nil
220- }
221-
222- found = false
223- for _ , aggHost := range aggregate .Hosts {
224- if aggHost == host {
225- found = true
226- }
227- }
228-
229- if ! found {
230- log .Info ("cannot find host in aggregate" , "host" , host , "name" , name )
231- return nil
232- }
233-
234- result , err := aggregates .RemoveHost (ctx , serviceClient , aggregate .ID , aggregates.RemoveHostOpts {Host : host }).Extract ()
235- if err != nil {
236- return fmt .Errorf ("failed to add host %v to aggregate %v due to %w" , host , name , err )
237- }
238- aggs [name ] = result
239- log .Info ("removed host from aggregate" , "host" , host , "name" , name )
240-
241- return nil
242- }
0 commit comments