-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlinodecluster_controller.go
More file actions
543 lines (477 loc) · 21.7 KB
/
Copy pathlinodecluster_controller.go
File metadata and controls
543 lines (477 loc) · 21.7 KB
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
/*
Copyright 2023 Akamai Technologies, Inc.
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 controller
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/events"
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
kutil "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/paused"
"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"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
"github.com/linode/cluster-api-provider-linode/cloud/scope"
wrappedruntimeclient "github.com/linode/cluster-api-provider-linode/observability/wrappers/runtimeclient"
wrappedruntimereconciler "github.com/linode/cluster-api-provider-linode/observability/wrappers/runtimereconciler"
"github.com/linode/cluster-api-provider-linode/util"
"github.com/linode/cluster-api-provider-linode/util/reconciler"
)
const (
lbTypeDNS string = "dns"
lbTypeExternal string = "external"
lbTypeNB string = "NodeBalancer"
ConditionPreflightLinodeVPCReady string = "PreflightLinodeVPCReady"
ConditionPreflightLinodeNBFirewallReady string = "PreflightLinodeNBFirewallReady"
)
// LinodeClusterReconciler reconciles a LinodeCluster object
type LinodeClusterReconciler struct {
client.Client
Recorder events.EventRecorder
LinodeClientConfig scope.ClientConfig
DnsClientConfig scope.ClientConfig
WatchFilterValue string
ReconcileTimeout time.Duration
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodeclusters/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *LinodeClusterReconciler) Reconcile(ctx context.Context, linodeCluster *infrav1alpha2.LinodeCluster) (ctrl.Result, error) {
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(r.ReconcileTimeout))
defer cancel()
logger := ctrl.LoggerFrom(ctx).WithName("LinodeClusterReconciler").WithValues("name", linodeCluster.Name, "namespace", linodeCluster.Namespace)
cluster, err := kutil.GetOwnerCluster(ctx, r.TracedClient(), linodeCluster.ObjectMeta)
if err != nil {
logger.Info("Failed to get owner cluster", "error", err.Error())
return ctrl.Result{}, client.IgnoreNotFound(err)
} else if cluster == nil {
logger.Info("Cluster Controller has not yet set OwnerRef, skipping reconciliation")
return ctrl.Result{}, nil
}
// Create the cluster scope.
clusterScope, err := scope.NewClusterScope(
ctx,
r.LinodeClientConfig,
r.DnsClientConfig,
scope.ClusterScopeParams{
Client: r.TracedClient(),
Cluster: cluster,
LinodeCluster: linodeCluster,
LinodeMachineList: infrav1alpha2.LinodeMachineList{},
},
)
if err != nil {
logger.Info("Failed to create cluster scope", "error", err.Error())
return ctrl.Result{}, fmt.Errorf("failed to create cluster scope: %w", err)
}
isPaused, _, err := paused.EnsurePausedCondition(ctx, clusterScope.Client, clusterScope.Cluster, clusterScope.LinodeCluster)
if err != nil {
return ctrl.Result{}, err
}
if isPaused {
logger.Info("linodeCluster or linked cluster is marked as paused, won't reconcile.")
return ctrl.Result{}, nil
}
return r.reconcile(ctx, clusterScope, logger)
}
//nolint:cyclop // can't make it simpler with existing API
func (r *LinodeClusterReconciler) reconcile(
ctx context.Context,
clusterScope *scope.ClusterScope,
logger logr.Logger,
) (res ctrl.Result, reterr error) {
res = ctrl.Result{}
clusterScope.LinodeCluster.Status.Ready = false
clusterScope.LinodeCluster.Status.FailureReason = nil
clusterScope.LinodeCluster.Status.FailureMessage = util.Pointer("")
// Always close the scope when exiting this function so we can persist any LinodeCluster changes.
defer func() {
// Filter out any IsNotFound message since client.IgnoreNotFound does not handle aggregate errors
if err := clusterScope.Close(ctx); utilerrors.FilterOut(util.UnwrapError(err), apierrors.IsNotFound) != nil && reterr == nil {
logger.Error(err, "failed to patch LinodeCluster")
reterr = err
}
}()
labels := map[string]string{
clusterv1.MachineControlPlaneLabel: "",
}
if clusterScope.Cluster != nil {
labels[clusterv1.ClusterNameLabel] = clusterScope.Cluster.Name
}
if err := r.TracedClient().List(ctx, &clusterScope.LinodeMachines, client.InNamespace(clusterScope.LinodeCluster.Namespace), client.MatchingLabels(labels)); err != nil {
return res, err
}
if err := clusterScope.SetCredentialRefTokenForLinodeClients(ctx); err != nil {
logger.Error(err, "failed to update linode client token from Credential Ref")
return res, err
}
// Handle deleted clusters
if !clusterScope.LinodeCluster.DeletionTimestamp.IsZero() {
if err := r.reconcileDelete(ctx, logger, clusterScope); err != nil {
if !reconciler.HasStaleCondition(clusterScope.LinodeCluster.GetCondition(string(clusterv1.ReadyCondition)),
reconciler.DefaultTimeout(r.ReconcileTimeout, reconciler.DefaultClusterControllerReconcileTimeout)) {
logger.Info("re-queuing cluster/nb deletion")
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
return res, err
}
return res, nil
}
if err := clusterScope.AddFinalizer(ctx); err != nil {
logger.Error(err, "failed to update cluster finalizer")
return res, err
}
// Perform preflight checks - check if the NB firewall and VPC are created successfully
if res, err := r.performPreflightChecks(ctx, logger, clusterScope); err != nil || !res.IsZero() {
return res, err
}
// Create
if clusterScope.LinodeCluster.Spec.ControlPlaneEndpoint.Host == "" {
if err := r.reconcileCreate(ctx, logger, clusterScope); err != nil {
if !reconciler.HasStaleCondition(clusterScope.LinodeCluster.GetCondition(clusterv1.ReadyCondition),
reconciler.DefaultTimeout(r.ReconcileTimeout, reconciler.DefaultClusterControllerReconcileTimeout)) {
logger.Info("re-queuing cluster/load-balancer creation")
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
return res, err
}
}
clusterScope.LinodeCluster.Status.Ready = true
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionTrue,
Reason: "LoadBalancerReady", // We have to set the reason to not fail object patching
})
for _, eachMachine := range clusterScope.LinodeMachines.Items {
if len(eachMachine.Status.Addresses) == 0 {
return res, nil
}
}
if err := addMachineToLB(ctx, clusterScope); err != nil {
if errors.Is(err, util.ErrReconcileAgain) {
logger.Info("re-queuing adding machine to loadbalancer")
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultMachineControllerRetryDelay)}, nil
}
logger.Error(err, "Failed to add Linode machine to loadbalancer option")
return retryIfTransient(err, logger)
}
return res, nil
}
func (r *LinodeClusterReconciler) performPreflightChecks(ctx context.Context, logger logr.Logger, clusterScope *scope.ClusterScope) (ctrl.Result, error) {
// Check VPC configuration - either direct ID or reference
if clusterScope.LinodeCluster.Spec.VPCID != nil || clusterScope.LinodeCluster.Spec.VPCRef != nil {
if !reconciler.ConditionTrue(clusterScope.LinodeCluster.GetCondition(ConditionPreflightLinodeVPCReady)) {
res, err := r.reconcilePreflightLinodeVPCCheck(ctx, logger, clusterScope)
if err != nil || !res.IsZero() {
// The condition is already set in reconcilePreflightLinodeVPCCheck, so we don't need to set it again
return res, err
}
}
}
if clusterScope.LinodeCluster.Spec.NodeBalancerFirewallRef != nil {
if !reconciler.ConditionTrue(clusterScope.LinodeCluster.GetCondition(ConditionPreflightLinodeNBFirewallReady)) {
res, err := r.reconcilePreflightLinodeFirewallCheck(ctx, logger, clusterScope)
if err != nil || !res.IsZero() {
return res, err
}
}
}
return ctrl.Result{}, nil
}
func (r *LinodeClusterReconciler) reconcilePreflightLinodeFirewallCheck(ctx context.Context, logger logr.Logger, clusterScope *scope.ClusterScope) (ctrl.Result, error) {
// If NodeBalancerFirewallID is directly specified, check if it exists
if clusterScope.LinodeCluster.Spec.Network.NodeBalancerFirewallID != nil {
firewallID := *clusterScope.LinodeCluster.Spec.Network.NodeBalancerFirewallID
logger.Info("Verifying direct NodeBalancerFirewallID", "firewallID", firewallID)
_, err := clusterScope.LinodeClient.GetFirewall(ctx, firewallID)
if err != nil {
logger.Error(err, "Failed to get NodeBalancer firewall with provided ID", "firewallID", firewallID)
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeNBFirewallReady,
Status: metav1.ConditionFalse,
Reason: util.CreateError,
Message: err.Error(),
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeNBFirewallReady,
Status: metav1.ConditionTrue,
Reason: "LinodeFirewallReady", // We have to set the reason to not fail object patching
})
return ctrl.Result{}, nil
}
name := clusterScope.LinodeCluster.Spec.NodeBalancerFirewallRef.Name
namespace := clusterScope.LinodeCluster.Spec.NodeBalancerFirewallRef.Namespace
if namespace == "" {
namespace = clusterScope.LinodeCluster.Namespace
}
linodeFirewall := &infrav1alpha2.LinodeFirewall{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
objectKey := client.ObjectKeyFromObject(linodeFirewall)
err := clusterScope.Client.Get(ctx, objectKey, linodeFirewall)
if err != nil {
logger.Error(err, "Failed to fetch LinodeFirewall")
if reconciler.HasStaleCondition(clusterScope.LinodeCluster.GetCondition(ConditionPreflightLinodeNBFirewallReady),
reconciler.DefaultTimeout(r.ReconcileTimeout, reconciler.DefaultClusterControllerReconcileTimeout)) {
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeNBFirewallReady,
Status: metav1.ConditionFalse,
Reason: util.CreateError,
Message: err.Error(),
})
return ctrl.Result{}, err
}
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeNBFirewallReady,
Status: metav1.ConditionFalse,
Reason: "LinodeFirewallNotYetAvailable", // We have to set the reason to not fail object patching
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
if linodeFirewall.Spec.FirewallID == nil {
logger.Info("Linode firewall not yet available")
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeNBFirewallReady,
Status: metav1.ConditionFalse,
Reason: "LinodeFirewallNotYetAvailable", // We have to set the reason to not fail object patching
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
// Only set to true if there was no error
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeNBFirewallReady,
Status: metav1.ConditionTrue,
Reason: "LinodeFirewallReady", // We have to set the reason to not fail object patching
})
return ctrl.Result{}, nil
}
func (r *LinodeClusterReconciler) reconcilePreflightLinodeVPCCheck(ctx context.Context, logger logr.Logger, clusterScope *scope.ClusterScope) (ctrl.Result, error) {
// If VPCID is directly specified, check if it exists
if clusterScope.LinodeCluster.Spec.VPCID != nil {
vpcID := *clusterScope.LinodeCluster.Spec.VPCID
vpc, err := clusterScope.LinodeClient.GetVPC(ctx, vpcID)
if err != nil {
logger.Error(err, "Failed to get VPC with provided ID", "vpcID", vpcID)
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionFalse,
Reason: util.CreateError,
Message: fmt.Sprintf("VPC with ID %d not found: %v", vpcID, err),
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
// VPC exists, verify it has at least one subnet
if len(vpc.Subnets) == 0 {
err := fmt.Errorf("VPC with ID %d has no subnets", vpcID)
logger.Error(err, "Failed preflight check: VPC has no subnets")
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionFalse,
Reason: util.CreateError,
Message: err.Error(),
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
// Only set to true if there was no error
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionTrue,
Reason: "LinodeVPCReady", // We have to set the reason to not fail object patching
})
return ctrl.Result{}, nil
}
// Otherwise, check for VPCRef
name := clusterScope.LinodeCluster.Spec.VPCRef.Name
namespace := clusterScope.LinodeCluster.Spec.VPCRef.Namespace
if namespace == "" {
namespace = clusterScope.LinodeCluster.Namespace
}
linodeVPC := infrav1alpha2.LinodeVPC{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
if err := clusterScope.Client.Get(ctx, client.ObjectKeyFromObject(&linodeVPC), &linodeVPC); err != nil {
logger.Error(err, "Failed to fetch LinodeVPC")
if reconciler.HasStaleCondition(clusterScope.LinodeCluster.GetCondition(ConditionPreflightLinodeVPCReady),
reconciler.DefaultTimeout(r.ReconcileTimeout, reconciler.DefaultClusterControllerReconcileTimeout)) {
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionFalse,
Reason: util.CreateError,
Message: err.Error(),
})
return ctrl.Result{}, err
}
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionFalse,
Reason: "LinodeVPCNotYetAvailable", // We have to set the reason to not fail object patching
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
} else if !linodeVPC.Status.Ready {
logger.Info("LinodeVPC is not yet available")
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionFalse,
Reason: "LinodeVPCNotYetAvailable", // We have to set the reason to not fail object patching
})
return ctrl.Result{RequeueAfter: reconciler.WithJitter(reconciler.DefaultClusterControllerReconcileDelay)}, nil
}
// Only set to true if there was no error
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: ConditionPreflightLinodeVPCReady,
Status: metav1.ConditionTrue,
Reason: "LinodeVPCReady", // We have to set the reason to not fail object patching
})
return ctrl.Result{}, nil
}
func (r *LinodeClusterReconciler) setFailureReason(clusterScope *scope.ClusterScope, failureReason, failureMessage string) {
clusterScope.LinodeCluster.Status.FailureReason = util.Pointer(failureReason)
clusterScope.LinodeCluster.Status.FailureMessage = util.Pointer(failureMessage)
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: failureReason,
Message: failureMessage,
})
}
func (r *LinodeClusterReconciler) reconcileCreate(ctx context.Context, logger logr.Logger, clusterScope *scope.ClusterScope) error {
if err := clusterScope.AddCredentialsRefFinalizer(ctx); err != nil {
logger.Error(err, "failed to update credentials finalizer")
r.setFailureReason(clusterScope, util.CreateError, err.Error())
return err
}
// handle creation for the loadbalancer for the control plane
if clusterScope.LinodeCluster.Spec.Network.LoadBalancerType == lbTypeDNS {
handleDNS(clusterScope)
} else {
if err := handleNBCreate(ctx, logger, clusterScope); err != nil {
return err
}
}
return nil
}
func (r *LinodeClusterReconciler) reconcileDelete(ctx context.Context, logger logr.Logger, clusterScope *scope.ClusterScope) error {
logger.Info("deleting cluster")
switch {
case clusterScope.LinodeCluster.Spec.Network.LoadBalancerType == lbTypeExternal:
logger.Info("LoadBalacing managed externally, nothing to do.")
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: clusterv1.DeletionCompletedReason,
Message: "Deletion in progress",
})
case clusterScope.LinodeCluster.Spec.Network.LoadBalancerType == lbTypeDNS:
if err := removeMachineFromDNS(ctx, logger, clusterScope); err != nil {
return fmt.Errorf("remove machine from loadbalancer: %w", err)
}
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: clusterv1.DeletionCompletedReason,
Message: "Load balancing for Type DNS deleted",
})
case clusterScope.LinodeCluster.Spec.Network.LoadBalancerType == lbTypeNB && clusterScope.LinodeCluster.Spec.Network.NodeBalancerID == nil:
logger.Info("NodeBalancer ID is missing for Type NodeBalancer, nothing to do")
case clusterScope.LinodeCluster.Spec.Network.LoadBalancerType == lbTypeNB && clusterScope.LinodeCluster.Spec.Network.NodeBalancerID != nil:
if err := removeMachineFromNB(ctx, logger, clusterScope); err != nil {
return fmt.Errorf("remove machine from loadbalancer: %w", err)
}
err := clusterScope.LinodeClient.DeleteNodeBalancer(ctx, *clusterScope.LinodeCluster.Spec.Network.NodeBalancerID)
if util.IgnoreLinodeAPIError(err, http.StatusNotFound) != nil {
logger.Error(err, "failed to delete NodeBalancer")
r.setFailureReason(clusterScope, util.DeleteError, err.Error())
r.Recorder.Eventf(
clusterScope.LinodeCluster,
nil,
corev1.EventTypeWarning,
util.DeleteError,
"DeleteNodeBalancer",
err.Error(),
)
return err
}
clusterScope.LinodeCluster.SetCondition(metav1.Condition{
Type: clusterv1.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: clusterv1.DeletionCompletedReason,
Message: "Load balancer for Type NodeBalancer deleted",
})
clusterScope.LinodeCluster.Spec.Network.NodeBalancerID = nil
clusterScope.LinodeCluster.Spec.Network.ApiserverNodeBalancerConfigID = nil
clusterScope.LinodeCluster.Spec.Network.AdditionalPorts = []infrav1alpha2.LinodeNBPortConfig{}
}
if len(clusterScope.LinodeMachines.Items) > 0 {
return errors.New("waiting for associated LinodeMachine objects to be deleted")
}
util.DeleteClusterIPs(clusterScope.Cluster.Name, clusterScope.Cluster.Namespace)
if err := clusterScope.RemoveCredentialsRefFinalizer(ctx); err != nil {
logger.Error(err, "failed to remove credentials finalizer")
r.setFailureReason(clusterScope, util.DeleteError, err.Error())
return err
}
controllerutil.RemoveFinalizer(clusterScope.LinodeCluster, infrav1alpha2.ClusterFinalizer)
if controllerutil.ContainsFinalizer(clusterScope.LinodeCluster, infrav1alpha2.GroupVersion.String()) {
controllerutil.RemoveFinalizer(clusterScope.LinodeCluster, infrav1alpha2.GroupVersion.String())
}
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *LinodeClusterReconciler) SetupWithManager(mgr ctrl.Manager, options crcontroller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
For(&infrav1alpha2.LinodeCluster{}).
WithOptions(options).
// we care about reconciling on metadata updates for LinodeClusters because the OwnerRef for the Cluster is needed
WithEventFilter(predicates.ResourceHasFilterLabel(mgr.GetScheme(), mgr.GetLogger(), r.WatchFilterValue)).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(
kutil.ClusterToInfrastructureMapFunc(context.TODO(), infrav1alpha2.GroupVersion.WithKind("LinodeCluster"), mgr.GetClient(), &infrav1alpha2.LinodeCluster{}),
),
builder.WithPredicates(predicates.ClusterPausedTransitionsOrInfrastructureProvisioned(mgr.GetScheme(), mgr.GetLogger())),
).
Watches(
&infrav1alpha2.LinodeMachine{},
handler.EnqueueRequestsFromMapFunc(linodeMachineToLinodeCluster(r.TracedClient(), mgr.GetLogger())),
).Complete(reconciler.AsReconcilerWithTracing(r.TracedClient(), r))
if err != nil {
return fmt.Errorf("failed to build controller: %w", err)
}
return nil
}
func (r *LinodeClusterReconciler) TracedClient() client.Client {
return wrappedruntimeclient.NewRuntimeClientWithTracing(r.Client, wrappedruntimereconciler.DefaultDecorator())
}