Skip to content

Commit 3bde85f

Browse files
committed
feat: add Scopes to pass data to services
1 parent f7a0c9f commit 3bde85f

File tree

13 files changed

+220
-173
lines changed

13 files changed

+220
-173
lines changed

controllers/openstackcluster_controller.go

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import (
2121
"fmt"
2222
"reflect"
2323

24-
"github.com/go-logr/logr"
25-
"github.com/gophercloud/gophercloud"
26-
"github.com/gophercloud/utils/openstack/clientconfig"
2724
"github.com/pkg/errors"
2825
corev1 "k8s.io/api/core/v1"
2926
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -51,6 +48,7 @@ import (
5148
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/loadbalancer"
5249
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
5350
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/provider"
51+
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
5452
)
5553

5654
// OpenStackClusterReconciler reconciles a OpenStackCluster object.
@@ -109,36 +107,42 @@ func (r *OpenStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
109107
}
110108
}()
111109

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+
112121
// Handle deleted clusters
113122
if !openStackCluster.DeletionTimestamp.IsZero() {
114-
return reconcileDelete(ctx, log, r.Client, patchHelper, cluster, openStackCluster)
123+
return reconcileDelete(ctx, scope, patchHelper, cluster, openStackCluster)
115124
}
116125

117126
// Handle non-deleted clusters
118-
return reconcileNormal(ctx, log, r.Client, patchHelper, cluster, openStackCluster)
127+
return reconcileNormal(ctx, scope, patchHelper, cluster, openStackCluster)
119128
}
120129

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")
128132

129-
if err = deleteBastion(log, osProviderClient, clientOpts, cluster, openStackCluster); err != nil {
133+
if err := deleteBastion(scope, cluster, openStackCluster); err != nil {
130134
return reconcile.Result{}, err
131135
}
132136

133-
networkingService, err := networking.NewService(osProviderClient, clientOpts, log)
137+
networkingService, err := networking.NewService(scope)
134138
if err != nil {
135139
return reconcile.Result{}, err
136140
}
137141

138142
clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)
139143

140144
if openStackCluster.Spec.ManagedAPIServerLoadBalancer {
141-
loadBalancerService, err := loadbalancer.NewService(osProviderClient, clientOpts, log)
145+
loadBalancerService, err := loadbalancer.NewService(scope)
142146
if err != nil {
143147
return reconcile.Result{}, err
144148
}
@@ -169,7 +173,7 @@ func reconcileDelete(ctx context.Context, log logr.Logger, client client.Client,
169173

170174
// Cluster is deleted so remove the finalizer.
171175
controllerutil.RemoveFinalizer(openStackCluster, infrav1.ClusterFinalizer)
172-
log.Info("Reconciled Cluster delete successfully")
176+
scope.Logger.Info("Reconciled Cluster delete successfully")
173177
if err := patchHelper.Patch(ctx, openStackCluster); err != nil {
174178
return ctrl.Result{}, err
175179
}
@@ -185,12 +189,12 @@ func contains(arr []string, target string) bool {
185189
return false
186190
}
187191

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)
190194
if err != nil {
191195
return err
192196
}
193-
networkingService, err := networking.NewService(osProviderClient, clientOpts, log)
197+
networkingService, err := networking.NewService(scope)
194198
if err != nil {
195199
return err
196200
}
@@ -235,8 +239,8 @@ func deleteBastion(log logr.Logger, osProviderClient *gophercloud.ProviderClient
235239
return nil
236240
}
237241

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")
240244

241245
// If the OpenStackCluster doesn't have our finalizer, add it.
242246
controllerutil.AddFinalizer(openStackCluster, infrav1.ClusterFinalizer)
@@ -245,22 +249,17 @@ func reconcileNormal(ctx context.Context, log logr.Logger, client client.Client,
245249
return reconcile.Result{}, err
246250
}
247251

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)
254253
if err != nil {
255254
return reconcile.Result{}, err
256255
}
257256

258-
err = reconcileNetworkComponents(log, osProviderClient, clientOpts, cluster, openStackCluster)
257+
err = reconcileNetworkComponents(scope, cluster, openStackCluster)
259258
if err != nil {
260259
return reconcile.Result{}, err
261260
}
262261

263-
if err = reconcileBastion(log, osProviderClient, clientOpts, cluster, openStackCluster); err != nil {
262+
if err = reconcileBastion(scope, cluster, openStackCluster); err != nil {
264263
return reconcile.Result{}, err
265264
}
266265

@@ -292,18 +291,18 @@ func reconcileNormal(ctx context.Context, log logr.Logger, client client.Client,
292291
openStackCluster.Status.Ready = true
293292
openStackCluster.Status.FailureMessage = nil
294293
openStackCluster.Status.FailureReason = nil
295-
log.Info("Reconciled Cluster create successfully")
294+
scope.Logger.Info("Reconciled Cluster create successfully")
296295
return reconcile.Result{}, nil
297296
}
298297

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")
301300

302301
if openStackCluster.Spec.Bastion == nil || !openStackCluster.Spec.Bastion.Enabled {
303-
return deleteBastion(log, osProviderClient, clientOpts, cluster, openStackCluster)
302+
return deleteBastion(scope, cluster, openStackCluster)
304303
}
305304

306-
computeService, err := compute.NewService(osProviderClient, clientOpts, log)
305+
computeService, err := compute.NewService(scope)
307306
if err != nil {
308307
return err
309308
}
@@ -326,7 +325,7 @@ func reconcileBastion(log logr.Logger, osProviderClient *gophercloud.ProviderCli
326325
return errors.Errorf("failed to reconcile bastion: %v", err)
327326
}
328327

329-
networkingService, err := networking.NewService(osProviderClient, clientOpts, log)
328+
networkingService, err := networking.NewService(scope)
330329
if err != nil {
331330
return err
332331
}
@@ -357,15 +356,15 @@ func reconcileBastion(log logr.Logger, osProviderClient *gophercloud.ProviderCli
357356
return nil
358357
}
359358

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 {
361360
clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)
362361

363-
networkingService, err := networking.NewService(osProviderClient, clientOpts, log)
362+
networkingService, err := networking.NewService(scope)
364363
if err != nil {
365364
return err
366365
}
367366

368-
log.Info("Reconciling network components")
367+
scope.Logger.Info("Reconciling network components")
369368

370369
err = networkingService.ReconcileExternalNetwork(openStackCluster)
371370
if err != nil {
@@ -374,7 +373,7 @@ func reconcileNetworkComponents(log logr.Logger, osProviderClient *gophercloud.P
374373
}
375374

376375
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")
378377

379378
netOpts := openStackCluster.Spec.Network.ToListOpt()
380379
networkList, err := networkingService.GetNetworksByFilter(&netOpts)
@@ -450,7 +449,7 @@ func reconcileNetworkComponents(log logr.Logger, osProviderClient *gophercloud.P
450449
}
451450

452451
if openStackCluster.Spec.ManagedAPIServerLoadBalancer {
453-
loadBalancerService, err := loadbalancer.NewService(osProviderClient, clientOpts, log)
452+
loadBalancerService, err := loadbalancer.NewService(scope)
454453
if err != nil {
455454
return err
456455
}

0 commit comments

Comments
 (0)