@@ -21,9 +21,6 @@ import (
21
21
"fmt"
22
22
"reflect"
23
23
24
- "github.com/go-logr/logr"
25
- "github.com/gophercloud/gophercloud"
26
- "github.com/gophercloud/utils/openstack/clientconfig"
27
24
"github.com/pkg/errors"
28
25
corev1 "k8s.io/api/core/v1"
29
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -51,6 +48,7 @@ import (
51
48
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/loadbalancer"
52
49
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
53
50
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/provider"
51
+ "sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
54
52
)
55
53
56
54
// OpenStackClusterReconciler reconciles a OpenStackCluster object.
@@ -109,36 +107,42 @@ func (r *OpenStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
109
107
}
110
108
}()
111
109
110
+ osProviderClient , clientOpts , err := provider .NewClientFromCluster (ctx , r .Client , openStackCluster )
111
+ if err != nil {
112
+ return reconcile.Result {}, err
113
+ }
114
+
115
+ scope := & scope.Scope {
116
+ ProviderClient : osProviderClient ,
117
+ ProviderClientOpts : clientOpts ,
118
+ Logger : log ,
119
+ }
120
+
112
121
// Handle deleted clusters
113
122
if ! openStackCluster .DeletionTimestamp .IsZero () {
114
- return reconcileDelete (ctx , log , r . Client , patchHelper , cluster , openStackCluster )
123
+ return reconcileDelete (ctx , scope , patchHelper , cluster , openStackCluster )
115
124
}
116
125
117
126
// Handle non-deleted clusters
118
- return reconcileNormal (ctx , log , r . Client , patchHelper , cluster , openStackCluster )
127
+ return reconcileNormal (ctx , scope , patchHelper , cluster , openStackCluster )
119
128
}
120
129
121
- func reconcileDelete (ctx context.Context , log logr.Logger , client client.Client , patchHelper * patch.Helper , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) (ctrl.Result , error ) {
122
- log .Info ("Reconciling Cluster delete" )
123
-
124
- osProviderClient , clientOpts , err := provider .NewClientFromCluster (ctx , client , openStackCluster )
125
- if err != nil {
126
- return reconcile.Result {}, err
127
- }
130
+ func reconcileDelete (ctx context.Context , scope * scope.Scope , patchHelper * patch.Helper , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) (ctrl.Result , error ) {
131
+ scope .Logger .Info ("Reconciling Cluster delete" )
128
132
129
- if err = deleteBastion (log , osProviderClient , clientOpts , cluster , openStackCluster ); err != nil {
133
+ if err : = deleteBastion (scope , cluster , openStackCluster ); err != nil {
130
134
return reconcile.Result {}, err
131
135
}
132
136
133
- networkingService , err := networking .NewService (osProviderClient , clientOpts , log )
137
+ networkingService , err := networking .NewService (scope )
134
138
if err != nil {
135
139
return reconcile.Result {}, err
136
140
}
137
141
138
142
clusterName := fmt .Sprintf ("%s-%s" , cluster .Namespace , cluster .Name )
139
143
140
144
if openStackCluster .Spec .ManagedAPIServerLoadBalancer {
141
- loadBalancerService , err := loadbalancer .NewService (osProviderClient , clientOpts , log )
145
+ loadBalancerService , err := loadbalancer .NewService (scope )
142
146
if err != nil {
143
147
return reconcile.Result {}, err
144
148
}
@@ -169,7 +173,7 @@ func reconcileDelete(ctx context.Context, log logr.Logger, client client.Client,
169
173
170
174
// Cluster is deleted so remove the finalizer.
171
175
controllerutil .RemoveFinalizer (openStackCluster , infrav1 .ClusterFinalizer )
172
- log .Info ("Reconciled Cluster delete successfully" )
176
+ scope . Logger .Info ("Reconciled Cluster delete successfully" )
173
177
if err := patchHelper .Patch (ctx , openStackCluster ); err != nil {
174
178
return ctrl.Result {}, err
175
179
}
@@ -185,12 +189,12 @@ func contains(arr []string, target string) bool {
185
189
return false
186
190
}
187
191
188
- func deleteBastion (log logr. Logger , osProviderClient * gophercloud. ProviderClient , clientOpts * clientconfig. ClientOpts , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
189
- computeService , err := compute .NewService (osProviderClient , clientOpts , log )
192
+ func deleteBastion (scope * scope. Scope , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
193
+ computeService , err := compute .NewService (scope )
190
194
if err != nil {
191
195
return err
192
196
}
193
- networkingService , err := networking .NewService (osProviderClient , clientOpts , log )
197
+ networkingService , err := networking .NewService (scope )
194
198
if err != nil {
195
199
return err
196
200
}
@@ -235,8 +239,8 @@ func deleteBastion(log logr.Logger, osProviderClient *gophercloud.ProviderClient
235
239
return nil
236
240
}
237
241
238
- func reconcileNormal (ctx context.Context , log logr. Logger , client client. Client , patchHelper * patch.Helper , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) (ctrl.Result , error ) {
239
- log .Info ("Reconciling Cluster" )
242
+ func reconcileNormal (ctx context.Context , scope * scope. Scope , patchHelper * patch.Helper , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) (ctrl.Result , error ) {
243
+ scope . Logger .Info ("Reconciling Cluster" )
240
244
241
245
// If the OpenStackCluster doesn't have our finalizer, add it.
242
246
controllerutil .AddFinalizer (openStackCluster , infrav1 .ClusterFinalizer )
@@ -245,22 +249,17 @@ func reconcileNormal(ctx context.Context, log logr.Logger, client client.Client,
245
249
return reconcile.Result {}, err
246
250
}
247
251
248
- osProviderClient , clientOpts , err := provider .NewClientFromCluster (ctx , client , openStackCluster )
249
- if err != nil {
250
- return reconcile.Result {}, err
251
- }
252
-
253
- computeService , err := compute .NewService (osProviderClient , clientOpts , log )
252
+ computeService , err := compute .NewService (scope )
254
253
if err != nil {
255
254
return reconcile.Result {}, err
256
255
}
257
256
258
- err = reconcileNetworkComponents (log , osProviderClient , clientOpts , cluster , openStackCluster )
257
+ err = reconcileNetworkComponents (scope , cluster , openStackCluster )
259
258
if err != nil {
260
259
return reconcile.Result {}, err
261
260
}
262
261
263
- if err = reconcileBastion (log , osProviderClient , clientOpts , cluster , openStackCluster ); err != nil {
262
+ if err = reconcileBastion (scope , cluster , openStackCluster ); err != nil {
264
263
return reconcile.Result {}, err
265
264
}
266
265
@@ -292,18 +291,18 @@ func reconcileNormal(ctx context.Context, log logr.Logger, client client.Client,
292
291
openStackCluster .Status .Ready = true
293
292
openStackCluster .Status .FailureMessage = nil
294
293
openStackCluster .Status .FailureReason = nil
295
- log .Info ("Reconciled Cluster create successfully" )
294
+ scope . Logger .Info ("Reconciled Cluster create successfully" )
296
295
return reconcile.Result {}, nil
297
296
}
298
297
299
- func reconcileBastion (log logr. Logger , osProviderClient * gophercloud. ProviderClient , clientOpts * clientconfig. ClientOpts , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
300
- log .Info ("Reconciling Bastion" )
298
+ func reconcileBastion (scope * scope. Scope , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
299
+ scope . Logger .Info ("Reconciling Bastion" )
301
300
302
301
if openStackCluster .Spec .Bastion == nil || ! openStackCluster .Spec .Bastion .Enabled {
303
- return deleteBastion (log , osProviderClient , clientOpts , cluster , openStackCluster )
302
+ return deleteBastion (scope , cluster , openStackCluster )
304
303
}
305
304
306
- computeService , err := compute .NewService (osProviderClient , clientOpts , log )
305
+ computeService , err := compute .NewService (scope )
307
306
if err != nil {
308
307
return err
309
308
}
@@ -326,7 +325,7 @@ func reconcileBastion(log logr.Logger, osProviderClient *gophercloud.ProviderCli
326
325
return errors .Errorf ("failed to reconcile bastion: %v" , err )
327
326
}
328
327
329
- networkingService , err := networking .NewService (osProviderClient , clientOpts , log )
328
+ networkingService , err := networking .NewService (scope )
330
329
if err != nil {
331
330
return err
332
331
}
@@ -357,15 +356,15 @@ func reconcileBastion(log logr.Logger, osProviderClient *gophercloud.ProviderCli
357
356
return nil
358
357
}
359
358
360
- func reconcileNetworkComponents (log logr. Logger , osProviderClient * gophercloud. ProviderClient , clientOpts * clientconfig. ClientOpts , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
359
+ func reconcileNetworkComponents (scope * scope. Scope , cluster * clusterv1.Cluster , openStackCluster * infrav1.OpenStackCluster ) error {
361
360
clusterName := fmt .Sprintf ("%s-%s" , cluster .Namespace , cluster .Name )
362
361
363
- networkingService , err := networking .NewService (osProviderClient , clientOpts , log )
362
+ networkingService , err := networking .NewService (scope )
364
363
if err != nil {
365
364
return err
366
365
}
367
366
368
- log .Info ("Reconciling network components" )
367
+ scope . Logger .Info ("Reconciling network components" )
369
368
370
369
err = networkingService .ReconcileExternalNetwork (openStackCluster )
371
370
if err != nil {
@@ -374,7 +373,7 @@ func reconcileNetworkComponents(log logr.Logger, osProviderClient *gophercloud.P
374
373
}
375
374
376
375
if openStackCluster .Spec .NodeCIDR == "" {
377
- log .V (4 ).Info ("No need to reconcile network, searching network and subnet instead" )
376
+ scope . Logger .V (4 ).Info ("No need to reconcile network, searching network and subnet instead" )
378
377
379
378
netOpts := openStackCluster .Spec .Network .ToListOpt ()
380
379
networkList , err := networkingService .GetNetworksByFilter (& netOpts )
@@ -450,7 +449,7 @@ func reconcileNetworkComponents(log logr.Logger, osProviderClient *gophercloud.P
450
449
}
451
450
452
451
if openStackCluster .Spec .ManagedAPIServerLoadBalancer {
453
- loadBalancerService , err := loadbalancer .NewService (osProviderClient , clientOpts , log )
452
+ loadBalancerService , err := loadbalancer .NewService (scope )
454
453
if err != nil {
455
454
return err
456
455
}
0 commit comments