@@ -21,8 +21,10 @@ import (
21
21
"fmt"
22
22
23
23
"github.com/go-logr/logr"
24
+ "github.com/gophercloud/gophercloud"
24
25
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
25
26
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
27
+ "github.com/gophercloud/utils/openstack/clientconfig"
26
28
"github.com/pkg/errors"
27
29
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
30
"k8s.io/client-go/tools/record"
@@ -158,7 +160,8 @@ func (r *OpenStackClusterReconciler) reconcileDelete(ctx context.Context, log lo
158
160
log .Info ("OpenStack router deleted successfully" )
159
161
}
160
162
161
- if openStackCluster .Status .Network != nil {
163
+ // if NodeCIDR was not set, no network was created.
164
+ if openStackCluster .Status .Network != nil && openStackCluster .Spec .NodeCIDR != "" {
162
165
log .Info ("Deleting network" , "name" , openStackCluster .Status .Network .Name )
163
166
if err := networkingService .DeleteNetwork (openStackCluster .Status .Network ); err != nil {
164
167
return ctrl.Result {}, errors .Errorf ("failed to delete network: %v" , err )
@@ -189,8 +192,6 @@ func contains(arr []string, target string) bool {
189
192
func (r * OpenStackClusterReconciler ) reconcileNormal (ctx context.Context , log logr.Logger , patchHelper * patch.Helper , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) (ctrl.Result , error ) {
190
193
log .Info ("Reconciling Cluster" )
191
194
192
- clusterName := fmt .Sprintf ("%s-%s" , cluster .Namespace , cluster .Name )
193
-
194
195
// If the OpenStackCluster doesn't have our finalizer, add it.
195
196
controllerutil .AddFinalizer (openStackCluster , infrav1 .ClusterFinalizer )
196
197
// Register the finalizer immediately to avoid orphaning OpenStack resources on delete
@@ -208,73 +209,11 @@ func (r *OpenStackClusterReconciler) reconcileNormal(ctx context.Context, log lo
208
209
return reconcile.Result {}, err
209
210
}
210
211
211
- networkingService , err := networking .NewService (osProviderClient , clientOpts , log )
212
- if err != nil {
213
- return reconcile.Result {}, err
214
- }
215
-
216
- loadBalancerService , err := loadbalancer .NewService (osProviderClient , clientOpts , log , openStackCluster .Spec .UseOctavia )
212
+ err = r .reconcileNetworkComponents (log , osProviderClient , clientOpts , cluster , openStackCluster )
217
213
if err != nil {
218
214
return reconcile.Result {}, err
219
215
}
220
216
221
- log .Info ("Reconciling network components" )
222
- if openStackCluster .Spec .NodeCIDR == "" {
223
- log .V (4 ).Info ("No need to reconcile network, searching network and subnet instead" )
224
-
225
- netOpts := networks .ListOpts (openStackCluster .Spec .Network )
226
- networkList , err := networkingService .GetNetworksByFilter (& netOpts )
227
- if err != nil && len (networkList ) == 0 {
228
- return reconcile.Result {}, errors .Errorf ("failed to find network: %v" , err )
229
- }
230
- if len (networkList ) > 1 {
231
- return reconcile.Result {}, errors .Errorf ("failed to find only one network (result: %v): %v" , networkList , err )
232
- }
233
- openStackCluster .Status .Network = & infrav1.Network {
234
- ID : networkList [0 ].ID ,
235
- Name : networkList [0 ].Name ,
236
- }
237
-
238
- subnetOpts := subnets .ListOpts (openStackCluster .Spec .Subnet )
239
- subnetOpts .NetworkID = networkList [0 ].ID
240
- subnetList , err := networkingService .GetSubnetsByFilter (& subnetOpts )
241
- if err != nil || len (subnetList ) == 0 {
242
- return reconcile.Result {}, errors .Errorf ("failed to find subnet: %v" , err )
243
- }
244
- if len (subnetList ) > 1 {
245
- return reconcile.Result {}, errors .Errorf ("failed to find only one subnet (result: %v): %v" , subnetList , err )
246
- }
247
- openStackCluster .Status .Network .Subnet = & infrav1.Subnet {
248
- ID : subnetList [0 ].ID ,
249
- Name : subnetList [0 ].Name ,
250
- CIDR : subnetList [0 ].CIDR ,
251
- }
252
- } else {
253
- err := networkingService .ReconcileNetwork (clusterName , openStackCluster )
254
- if err != nil {
255
- return reconcile.Result {}, errors .Errorf ("failed to reconcile network: %v" , err )
256
- }
257
- err = networkingService .ReconcileSubnet (clusterName , openStackCluster )
258
- if err != nil {
259
- return reconcile.Result {}, errors .Errorf ("failed to reconcile subnets: %v" , err )
260
- }
261
- err = networkingService .ReconcileRouter (clusterName , openStackCluster )
262
- if err != nil {
263
- return reconcile.Result {}, errors .Errorf ("failed to reconcile router: %v" , err )
264
- }
265
- if openStackCluster .Spec .ManagedAPIServerLoadBalancer {
266
- err = loadBalancerService .ReconcileLoadBalancer (clusterName , openStackCluster )
267
- if err != nil {
268
- return reconcile.Result {}, errors .Errorf ("failed to reconcile load balancer: %v" , err )
269
- }
270
- }
271
- }
272
-
273
- err = networkingService .ReconcileSecurityGroups (clusterName , openStackCluster )
274
- if err != nil {
275
- return reconcile.Result {}, errors .Errorf ("failed to reconcile security groups: %v" , err )
276
- }
277
-
278
217
// Set APIEndpoints so the Cluster API Cluster Controller can pull them
279
218
if openStackCluster .Spec .ManagedAPIServerLoadBalancer {
280
219
openStackCluster .Spec .ControlPlaneEndpoint = clusterv1.APIEndpoint {
@@ -339,6 +278,80 @@ func (r *OpenStackClusterReconciler) reconcileNormal(ctx context.Context, log lo
339
278
return ctrl.Result {}, nil
340
279
}
341
280
281
+ func (r * OpenStackClusterReconciler ) reconcileNetworkComponents (log logr.Logger , osProviderClient * gophercloud.ProviderClient , clientOpts * clientconfig.ClientOpts , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
282
+ clusterName := fmt .Sprintf ("%s-%s" , cluster .Namespace , cluster .Name )
283
+
284
+ networkingService , err := networking .NewService (osProviderClient , clientOpts , log )
285
+ if err != nil {
286
+ return err
287
+ }
288
+
289
+ loadBalancerService , err := loadbalancer .NewService (osProviderClient , clientOpts , log , openStackCluster .Spec .UseOctavia )
290
+ if err != nil {
291
+ return err
292
+ }
293
+
294
+ log .Info ("Reconciling network components" )
295
+ if openStackCluster .Spec .NodeCIDR == "" {
296
+ log .V (4 ).Info ("No need to reconcile network, searching network and subnet instead" )
297
+
298
+ netOpts := networks .ListOpts (openStackCluster .Spec .Network )
299
+ networkList , err := networkingService .GetNetworksByFilter (& netOpts )
300
+ if err != nil && len (networkList ) == 0 {
301
+ return errors .Errorf ("failed to find network: %v" , err )
302
+ }
303
+ if len (networkList ) > 1 {
304
+ return errors .Errorf ("failed to find only one network (result: %v): %v" , networkList , err )
305
+ }
306
+ openStackCluster .Status .Network = & infrav1.Network {
307
+ ID : networkList [0 ].ID ,
308
+ Name : networkList [0 ].Name ,
309
+ }
310
+
311
+ subnetOpts := subnets .ListOpts (openStackCluster .Spec .Subnet )
312
+ subnetOpts .NetworkID = networkList [0 ].ID
313
+ subnetList , err := networkingService .GetSubnetsByFilter (& subnetOpts )
314
+ if err != nil || len (subnetList ) == 0 {
315
+ return errors .Errorf ("failed to find subnet: %v" , err )
316
+ }
317
+ if len (subnetList ) > 1 {
318
+ return errors .Errorf ("failed to find only one subnet (result: %v): %v" , subnetList , err )
319
+ }
320
+ openStackCluster .Status .Network .Subnet = & infrav1.Subnet {
321
+ ID : subnetList [0 ].ID ,
322
+ Name : subnetList [0 ].Name ,
323
+ CIDR : subnetList [0 ].CIDR ,
324
+ }
325
+ } else {
326
+ err := networkingService .ReconcileNetwork (clusterName , openStackCluster )
327
+ if err != nil {
328
+ return errors .Errorf ("failed to reconcile network: %v" , err )
329
+ }
330
+ err = networkingService .ReconcileSubnet (clusterName , openStackCluster )
331
+ if err != nil {
332
+ return errors .Errorf ("failed to reconcile subnets: %v" , err )
333
+ }
334
+ err = networkingService .ReconcileRouter (clusterName , openStackCluster )
335
+ if err != nil {
336
+ return errors .Errorf ("failed to reconcile router: %v" , err )
337
+ }
338
+ }
339
+
340
+ if openStackCluster .Spec .ManagedAPIServerLoadBalancer {
341
+ err = loadBalancerService .ReconcileLoadBalancer (clusterName , openStackCluster )
342
+ if err != nil {
343
+ return errors .Errorf ("failed to reconcile load balancer: %v" , err )
344
+ }
345
+ }
346
+
347
+ err = networkingService .ReconcileSecurityGroups (clusterName , openStackCluster )
348
+ if err != nil {
349
+ return errors .Errorf ("failed to reconcile security groups: %v" , err )
350
+ }
351
+
352
+ return nil
353
+ }
354
+
342
355
func (r * OpenStackClusterReconciler ) SetupWithManager (mgr ctrl.Manager , options controller.Options ) error {
343
356
return ctrl .NewControllerManagedBy (mgr ).
344
357
WithOptions (options ).
0 commit comments