-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathopenstackcluster_controller.go
672 lines (580 loc) · 25.9 KB
/
openstackcluster_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"errors"
"fmt"
"reflect"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/predicates"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/compute"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/loadbalancer"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
)
const (
BastionInstanceHashAnnotation = "infrastructure.cluster.x-k8s.io/bastion-hash"
)
// OpenStackClusterReconciler reconciles a OpenStackCluster object.
type OpenStackClusterReconciler struct {
Client client.Client
Recorder record.EventRecorder
WatchFilterValue string
ScopeFactory scope.Factory
CaCertificates []byte // PEM encoded ca certificates.
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=openstackclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=openstackclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status,verbs=get;list;watch
func (r *OpenStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
log := ctrl.LoggerFrom(ctx)
// Fetch the OpenStackCluster instance
openStackCluster := &infrav1.OpenStackCluster{}
err := r.Client.Get(ctx, req.NamespacedName, openStackCluster)
if err != nil {
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Fetch the Cluster.
cluster, err := util.GetOwnerCluster(ctx, r.Client, openStackCluster.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if cluster == nil {
log.Info("Cluster Controller has not yet set OwnerRef")
return reconcile.Result{}, nil
}
log = log.WithValues("cluster", cluster.Name)
if annotations.IsPaused(cluster, openStackCluster) {
log.Info("OpenStackCluster or linked Cluster is marked as paused. Not reconciling")
return reconcile.Result{}, nil
}
patchHelper, err := patch.NewHelper(openStackCluster, r.Client)
if err != nil {
return ctrl.Result{}, err
}
// Always patch the openStackCluster when exiting this function so we can persist any OpenStackCluster changes.
defer func() {
if err := patchHelper.Patch(ctx, openStackCluster); err != nil {
if reterr == nil {
reterr = fmt.Errorf("error patching OpenStackCluster %s/%s: %w", openStackCluster.Namespace, openStackCluster.Name, err)
}
}
}()
scope, err := r.ScopeFactory.NewClientScopeFromCluster(ctx, r.Client, openStackCluster, r.CaCertificates, log)
if err != nil {
return reconcile.Result{}, err
}
// Handle deleted clusters
if !openStackCluster.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, scope, cluster, openStackCluster)
}
// Handle non-deleted clusters
return reconcileNormal(scope, cluster, openStackCluster)
}
func (r *OpenStackClusterReconciler) reconcileDelete(ctx context.Context, scope scope.Scope, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster) (ctrl.Result, error) {
scope.Logger().Info("Reconciling Cluster delete")
// Wait for machines to be deleted before removing the finalizer as they
// depend on this resource to deprovision. Additionally it appears that
// allowing the Kubernetes API to vanish too quickly will upset the capi
// kubeadm control plane controller.
machines, err := collections.GetFilteredMachinesForCluster(ctx, r.Client, cluster)
if err != nil {
return ctrl.Result{}, err
}
if len(machines) != 0 {
scope.Logger().Info("Waiting for machines to be deleted", "remaining", len(machines))
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
}
if err := deleteBastion(scope, cluster, openStackCluster); err != nil {
return reconcile.Result{}, err
}
networkingService, err := networking.NewService(scope)
if err != nil {
return reconcile.Result{}, err
}
clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)
if openStackCluster.Spec.APIServerLoadBalancer.Enabled {
loadBalancerService, err := loadbalancer.NewService(scope)
if err != nil {
return reconcile.Result{}, err
}
if err = loadBalancerService.DeleteLoadBalancer(openStackCluster, clusterName); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete load balancer: %w", err))
return reconcile.Result{}, fmt.Errorf("failed to delete load balancer: %w", err)
}
}
if err = networkingService.DeleteSecurityGroups(openStackCluster, clusterName); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete security groups: %w", err))
return reconcile.Result{}, fmt.Errorf("failed to delete security groups: %w", err)
}
// if NodeCIDR was not set, no network was created.
if openStackCluster.Spec.NodeCIDR != "" {
if err = networkingService.DeleteRouter(openStackCluster, clusterName); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete router: %w", err))
return ctrl.Result{}, fmt.Errorf("failed to delete router: %w", err)
}
if err = networkingService.DeletePorts(openStackCluster); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete ports: %w", err))
return reconcile.Result{}, fmt.Errorf("failed to delete ports: %w", err)
}
if err = networkingService.DeleteNetwork(openStackCluster, clusterName); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete network: %w", err))
return ctrl.Result{}, fmt.Errorf("failed to delete network: %w", err)
}
}
// Cluster is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(openStackCluster, infrav1.ClusterFinalizer)
scope.Logger().Info("Reconciled Cluster deleted successfully")
return ctrl.Result{}, nil
}
func contains(arr []string, target string) bool {
for _, a := range arr {
if a == target {
return true
}
}
return false
}
func deleteBastion(scope scope.Scope, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster) error {
computeService, err := compute.NewService(scope)
if err != nil {
return err
}
networkingService, err := networking.NewService(scope)
if err != nil {
return err
}
if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.FloatingIP != "" {
// Floating IP could have been created but not associated to instance, attempt to delete it from saved status first
if err = networkingService.DeleteFloatingIP(openStackCluster, openStackCluster.Status.Bastion.FloatingIP); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete floating IP: %w", err))
return fmt.Errorf("failed to delete floating IP: %w", err)
}
}
var instanceStatus *compute.InstanceStatus
if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.ID != "" {
instanceStatus, err = computeService.GetInstanceStatus(openStackCluster.Status.Bastion.ID)
if err != nil {
return err
}
} else {
instanceName := fmt.Sprintf("%s-bastion", cluster.Name)
instanceStatus, err = computeService.GetInstanceStatusByName(openStackCluster, instanceName)
if err != nil {
return err
}
}
if instanceStatus != nil {
instanceNS, err := instanceStatus.NetworkStatus()
if err != nil {
return err
}
addresses := instanceNS.Addresses()
for _, address := range addresses {
if address.Type == corev1.NodeExternalIP {
if err = networkingService.DeleteFloatingIP(openStackCluster, address.Address); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete floating IP: %w", err))
return fmt.Errorf("failed to delete floating IP: %w", err)
}
}
}
instanceSpec := bastionToInstanceSpec(openStackCluster, cluster.Name)
if err = computeService.DeleteInstance(openStackCluster, openStackCluster, instanceStatus, instanceSpec); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete bastion: %w", err))
return fmt.Errorf("failed to delete bastion: %w", err)
}
}
openStackCluster.Status.Bastion = nil
if err = networkingService.DeleteBastionSecurityGroup(openStackCluster, fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete bastion security group: %w", err))
return fmt.Errorf("failed to delete bastion security group: %w", err)
}
openStackCluster.Status.BastionSecurityGroup = nil
delete(openStackCluster.ObjectMeta.Annotations, BastionInstanceHashAnnotation)
return nil
}
func reconcileNormal(scope scope.Scope, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster) (ctrl.Result, error) { //nolint:unparam
scope.Logger().Info("Reconciling Cluster")
// If the OpenStackCluster doesn't have our finalizer, add it.
if controllerutil.AddFinalizer(openStackCluster, infrav1.ClusterFinalizer) {
// Register the finalizer immediately to avoid orphaning OpenStack resources on delete
return reconcile.Result{}, nil
}
computeService, err := compute.NewService(scope)
if err != nil {
return reconcile.Result{}, err
}
err = reconcileNetworkComponents(scope, cluster, openStackCluster)
if err != nil {
return reconcile.Result{}, err
}
result, err := reconcileBastion(scope, cluster, openStackCluster)
if err != nil || !reflect.DeepEqual(result, reconcile.Result{}) {
return result, err
}
availabilityZones, err := computeService.GetAvailabilityZones()
if err != nil {
return ctrl.Result{}, err
}
// Create a new list in case any AZs have been removed from OpenStack
openStackCluster.Status.FailureDomains = make(clusterv1.FailureDomains)
for _, az := range availabilityZones {
// By default, the AZ is used or not used for control plane nodes depending on the flag
found := !openStackCluster.Spec.ControlPlaneOmitAvailabilityZone
// If explicit AZs for control plane nodes are given, they override the value
if len(openStackCluster.Spec.ControlPlaneAvailabilityZones) > 0 {
found = contains(openStackCluster.Spec.ControlPlaneAvailabilityZones, az.ZoneName)
}
// Add the AZ object to the failure domains for the cluster
openStackCluster.Status.FailureDomains[az.ZoneName] = clusterv1.FailureDomainSpec{
ControlPlane: found,
}
}
openStackCluster.Status.Ready = true
openStackCluster.Status.FailureMessage = nil
openStackCluster.Status.FailureReason = nil
scope.Logger().Info("Reconciled Cluster created successfully")
return reconcile.Result{}, nil
}
func reconcileBastion(scope scope.Scope, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster) (ctrl.Result, error) {
scope.Logger().Info("Reconciling Bastion")
if openStackCluster.Spec.Bastion == nil || !openStackCluster.Spec.Bastion.Enabled {
return reconcile.Result{}, deleteBastion(scope, cluster, openStackCluster)
}
computeService, err := compute.NewService(scope)
if err != nil {
return reconcile.Result{}, err
}
instanceSpec := bastionToInstanceSpec(openStackCluster, cluster.Name)
bastionHash, err := compute.HashInstanceSpec(instanceSpec)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed computing bastion hash from instance spec: %w", err)
} else if bastionHashHasChanged(bastionHash, openStackCluster.ObjectMeta.Annotations) {
if err := deleteBastion(scope, cluster, openStackCluster); err != nil {
return ctrl.Result{}, err
}
}
var instanceStatus *compute.InstanceStatus
if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.ID != "" {
if instanceStatus, err = computeService.GetInstanceStatus(openStackCluster.Status.Bastion.ID); err != nil {
return reconcile.Result{}, err
}
}
if instanceStatus == nil {
// Check if there is an existing instance with bastion name, in case where bastion ID would not have been properly stored in cluster status
if instanceStatus, err = computeService.GetInstanceStatusByName(openStackCluster, instanceSpec.Name); err != nil {
return reconcile.Result{}, err
}
}
if instanceStatus == nil {
instanceStatus, err = computeService.CreateInstance(openStackCluster, openStackCluster, instanceSpec, cluster.Name)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to create bastion: %w", err)
}
}
// Save hash & status as soon as we know we have an instance
bastion, err := instanceStatus.BastionStatus(openStackCluster)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to retrieve bastion status: %w", err)
}
openStackCluster.Status.Bastion = bastion
annotations.AddAnnotations(openStackCluster, map[string]string{BastionInstanceHashAnnotation: bastionHash})
// Make sure that bastion instance has a valid state
switch instanceStatus.State() {
case infrav1.InstanceStateError:
return ctrl.Result{}, fmt.Errorf("failed to reconcile bastion, instance state is ERROR")
case infrav1.InstanceStateBuild, infrav1.InstanceStateUndefined:
scope.Logger().Info("Waiting for bastion instance to become ACTIVE", "id", instanceStatus.ID(), "status", instanceStatus.State())
return ctrl.Result{RequeueAfter: waitForBuildingInstanceToReconcile}, nil
case infrav1.InstanceStateDeleted:
// This should normally be handled by deleteBastion
openStackCluster.Status.Bastion = nil
return ctrl.Result{}, nil
}
networkingService, err := networking.NewService(scope)
if err != nil {
return ctrl.Result{}, err
}
port, err := computeService.GetManagementPort(openStackCluster, instanceStatus)
if err != nil {
err = fmt.Errorf("getting management port for bastion: %w", err)
handleUpdateOSCError(openStackCluster, err)
return ctrl.Result{}, err
}
fp, err := networkingService.GetFloatingIPByPortID(port.ID)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to get or create floating IP for bastion: %w", err))
return ctrl.Result{}, fmt.Errorf("failed to get floating IP for bastion port: %w", err)
} else if fp != nil {
// Floating IP is already attached to bastion, no need to proceed
openStackCluster.Status.Bastion.FloatingIP = fp.FloatingIP
return ctrl.Result{}, nil
}
clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)
floatingIP := openStackCluster.Spec.Bastion.FloatingIP
if openStackCluster.Status.Bastion.FloatingIP != "" {
// Some floating IP has already been created for this bastion, make sure we re-use it
floatingIP = openStackCluster.Status.Bastion.FloatingIP
}
fp, err = networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster, clusterName, floatingIP)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to get or create floating IP for bastion: %w", err))
return ctrl.Result{}, fmt.Errorf("failed to get or create floating IP for bastion: %w", err)
}
openStackCluster.Status.Bastion.FloatingIP = fp.FloatingIP
err = networkingService.AssociateFloatingIP(openStackCluster, fp, port.ID)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to associate floating IP with bastion: %w", err))
return ctrl.Result{}, fmt.Errorf("failed to associate floating IP with bastion: %w", err)
}
return ctrl.Result{}, nil
}
func bastionToInstanceSpec(openStackCluster *infrav1.OpenStackCluster, clusterName string) *compute.InstanceSpec {
name := fmt.Sprintf("%s-bastion", clusterName)
instanceSpec := &compute.InstanceSpec{
Name: name,
Flavor: openStackCluster.Spec.Bastion.Instance.Flavor,
SSHKeyName: openStackCluster.Spec.Bastion.Instance.SSHKeyName,
Image: openStackCluster.Spec.Bastion.Instance.Image,
ImageUUID: openStackCluster.Spec.Bastion.Instance.ImageUUID,
FailureDomain: openStackCluster.Spec.Bastion.AvailabilityZone,
RootVolume: openStackCluster.Spec.Bastion.Instance.RootVolume,
}
instanceSpec.SecurityGroups = openStackCluster.Spec.Bastion.Instance.SecurityGroups
if openStackCluster.Spec.ManagedSecurityGroups {
if openStackCluster.Status.BastionSecurityGroup != nil {
instanceSpec.SecurityGroups = append(instanceSpec.SecurityGroups, infrav1.SecurityGroupFilter{
ID: openStackCluster.Status.BastionSecurityGroup.ID,
})
}
}
instanceSpec.Ports = openStackCluster.Spec.Bastion.Instance.Ports
return instanceSpec
}
// bastionHashHasChanged returns a boolean whether if the latest bastion hash, built from the instance spec, has changed or not.
func bastionHashHasChanged(computeHash string, clusterAnnotations map[string]string) bool {
latestHash, ok := clusterAnnotations[BastionInstanceHashAnnotation]
if !ok {
return false
}
return latestHash != computeHash
}
func reconcileNetworkComponents(scope scope.Scope, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster) error {
clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)
networkingService, err := networking.NewService(scope)
if err != nil {
return err
}
scope.Logger().Info("Reconciling network components")
err = networkingService.ReconcileExternalNetwork(openStackCluster)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to reconcile external network: %w", err))
return fmt.Errorf("failed to reconcile external network: %w", err)
}
if openStackCluster.Spec.NodeCIDR == "" {
scope.Logger().V(4).Info("No need to reconcile network, searching network and subnet instead")
netOpts := openStackCluster.Spec.Network.ToListOpt()
networkList, err := networkingService.GetNetworksByFilter(&netOpts)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to find network: %w", err))
return fmt.Errorf("failed to find network: %w", err)
}
if len(networkList) == 0 {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to find any network"))
return fmt.Errorf("failed to find any network")
}
if len(networkList) > 1 {
handleUpdateOSCError(openStackCluster, fmt.Errorf("found multiple networks (result: %v)", networkList))
return fmt.Errorf("found multiple networks (result: %v)", networkList)
}
if openStackCluster.Status.Network == nil {
openStackCluster.Status.Network = &infrav1.NetworkStatusWithSubnets{}
}
openStackCluster.Status.Network.ID = networkList[0].ID
openStackCluster.Status.Network.Name = networkList[0].Name
openStackCluster.Status.Network.Tags = networkList[0].Tags
subnet, err := networkingService.GetNetworkSubnetByFilter(openStackCluster.Status.Network.ID, &openStackCluster.Spec.Subnet)
if err != nil {
err = fmt.Errorf("failed to find subnet: %w", err)
// Set the cluster to failed if subnet filter is invalid
if errors.Is(err, networking.ErrFilterMatch) {
handleUpdateOSCError(openStackCluster, err)
}
return err
}
openStackCluster.Status.Network.Subnets = []infrav1.Subnet{
{
ID: subnet.ID,
Name: subnet.Name,
CIDR: subnet.CIDR,
Tags: subnet.Tags,
},
}
} else {
err := networkingService.ReconcileNetwork(openStackCluster, clusterName)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to reconcile network: %w", err))
return fmt.Errorf("failed to reconcile network: %w", err)
}
err = networkingService.ReconcileSubnet(openStackCluster, clusterName)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to reconcile subnets: %w", err))
return fmt.Errorf("failed to reconcile subnets: %w", err)
}
err = networkingService.ReconcileRouter(openStackCluster, clusterName)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to reconcile router: %w", err))
return fmt.Errorf("failed to reconcile router: %w", err)
}
}
err = networkingService.ReconcileSecurityGroups(openStackCluster, clusterName)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to reconcile security groups: %w", err))
return fmt.Errorf("failed to reconcile security groups: %w", err)
}
// Calculate the port that we will use for the API server
var apiServerPort int
switch {
case openStackCluster.Spec.ControlPlaneEndpoint.IsValid():
apiServerPort = int(openStackCluster.Spec.ControlPlaneEndpoint.Port)
case openStackCluster.Spec.APIServerPort != 0:
apiServerPort = openStackCluster.Spec.APIServerPort
default:
apiServerPort = 6443
}
if openStackCluster.Spec.APIServerLoadBalancer.Enabled {
loadBalancerService, err := loadbalancer.NewService(scope)
if err != nil {
return err
}
terminalFailure, err := loadBalancerService.ReconcileLoadBalancer(openStackCluster, clusterName, apiServerPort)
if err != nil {
// if it's terminalFailure (not Transient), set the Failure reason and message
if terminalFailure {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to reconcile load balancer: %w", err))
}
return fmt.Errorf("failed to reconcile load balancer: %w", err)
}
}
if !openStackCluster.Spec.ControlPlaneEndpoint.IsValid() {
var host string
// If there is a load balancer use the floating IP for it if set, falling back to the internal IP
switch {
case openStackCluster.Spec.APIServerLoadBalancer.Enabled:
if openStackCluster.Status.APIServerLoadBalancer.IP != "" {
host = openStackCluster.Status.APIServerLoadBalancer.IP
} else {
host = openStackCluster.Status.APIServerLoadBalancer.InternalIP
}
case !openStackCluster.Spec.DisableAPIServerFloatingIP:
// If floating IPs are not disabled, get one to use as the VIP for the control plane
fp, err := networkingService.GetOrCreateFloatingIP(openStackCluster, openStackCluster, clusterName, openStackCluster.Spec.APIServerFloatingIP)
if err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("floating IP cannot be got or created: %w", err))
return fmt.Errorf("floating IP cannot be got or created: %w", err)
}
host = fp.FloatingIP
case openStackCluster.Spec.APIServerFixedIP != "":
// If a fixed IP was specified, assume that the user is providing the extra configuration
// to use that IP as the VIP for the API server, e.g. using keepalived or kube-vip
host = openStackCluster.Spec.APIServerFixedIP
default:
// For now, we do not provide a managed VIP without either a load balancer or a floating IP
// In the future, we could manage a VIP port on the cluster network and set allowedAddressPairs
// accordingly when creating control plane machines
// However this would require us to deploy software on the control plane hosts to manage the
// VIP (e.g. keepalived/kube-vip)
return fmt.Errorf("unable to determine VIP for API server")
}
// Set APIEndpoints so the Cluster API Cluster Controller can pull them
openStackCluster.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{
Host: host,
Port: int32(apiServerPort),
}
}
return nil
}
func (r *OpenStackClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
clusterToInfraFn := util.ClusterToInfrastructureMapFunc(ctx, infrav1.GroupVersion.WithKind("OpenStackCluster"), mgr.GetClient(), &infrav1.OpenStackCluster{})
log := ctrl.LoggerFrom(ctx)
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
For(&infrav1.OpenStackCluster{},
builder.WithPredicates(
predicate.Funcs{
// Avoid reconciling if the event triggering the reconciliation is related to incremental status updates
UpdateFunc: func(e event.UpdateEvent) bool {
oldCluster := e.ObjectOld.(*infrav1.OpenStackCluster).DeepCopy()
newCluster := e.ObjectNew.(*infrav1.OpenStackCluster).DeepCopy()
oldCluster.Status = infrav1.OpenStackClusterStatus{}
newCluster.Status = infrav1.OpenStackClusterStatus{}
oldCluster.ObjectMeta.ResourceVersion = ""
newCluster.ObjectMeta.ResourceVersion = ""
return !reflect.DeepEqual(oldCluster, newCluster)
},
},
),
).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
requests := clusterToInfraFn(ctx, o)
if len(requests) < 1 {
return nil
}
c := &infrav1.OpenStackCluster{}
if err := r.Client.Get(ctx, requests[0].NamespacedName, c); err != nil {
log.V(4).Error(err, "Failed to get OpenStack cluster")
return nil
}
if annotations.IsExternallyManaged(c) {
log.V(4).Info("OpenStackCluster is externally managed, skipping mapping")
return nil
}
return requests
}),
builder.WithPredicates(predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx))),
).
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
WithEventFilter(predicates.ResourceIsNotExternallyManaged(ctrl.LoggerFrom(ctx))).
Complete(r)
}
func handleUpdateOSCError(openstackCluster *infrav1.OpenStackCluster, message error) {
err := capierrors.UpdateClusterError
openstackCluster.Status.FailureReason = &err
openstackCluster.Status.FailureMessage = pointer.String(message.Error())
}