-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathvspherevm_controller.go
612 lines (546 loc) · 21.8 KB
/
vspherevm_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
/*
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 (
goctx "context"
"fmt"
"reflect"
"strings"
"time"
"github.com/pkg/errors"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/remote"
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1alpha1"
clusterutilv1 "sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
ctrlutil "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/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
"sigs.k8s.io/cluster-api-provider-vsphere/feature"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/clustermodule"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/identity"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/record"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/services"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/services/govmomi"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
)
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=vspherevms,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=vspherevms/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinedeployments;machinesets,verbs=get;list;watch
// +kubebuilder:rbac:groups=controlplane.cluster.x-k8s.io,resources=kubeadmcontrolplanes,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch
// +kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;delete
// +kubebuilder:rbac:groups=ipam.cluster.x-k8s.io,resources=ipaddressclaims,verbs=get;create;update;watch;list
// +kubebuilder:rbac:groups=ipam.cluster.x-k8s.io,resources=ipaddresses,verbs=get;list;watch
// AddVMControllerToManager adds the VM controller to the provided manager.
//
//nolint:forcetypeassert
func AddVMControllerToManager(ctx *context.ControllerManagerContext, mgr manager.Manager) error {
var (
controlledType = &infrav1.VSphereVM{}
controlledTypeName = reflect.TypeOf(controlledType).Elem().Name()
controlledTypeGVK = infrav1.GroupVersion.WithKind(controlledTypeName)
controllerNameShort = fmt.Sprintf("%s-controller", strings.ToLower(controlledTypeName))
controllerNameLong = fmt.Sprintf("%s/%s/%s", ctx.Namespace, ctx.Name, controllerNameShort)
)
// Build the controller context.
controllerContext := &context.ControllerContext{
ControllerManagerContext: ctx,
Name: controllerNameShort,
Recorder: record.New(mgr.GetEventRecorderFor(controllerNameLong)),
Logger: ctx.Logger.WithName(controllerNameShort),
}
r := vmReconciler{
ControllerContext: controllerContext,
VMService: &govmomi.VMService{},
}
controller, err := ctrl.NewControllerManagedBy(mgr).
// Watch the controlled, infrastructure resource.
For(controlledType).
// Watch a GenericEvent channel for the controlled resource.
//
// This is useful when there are events outside of Kubernetes that
// should cause a resource to be synchronized, such as a goroutine
// waiting on some asynchronous, external task to complete.
Watches(
&source.Channel{Source: ctx.GetGenericEventChannelFor(controlledTypeGVK)},
&handler.EnqueueRequestForObject{},
).
WithOptions(controller.Options{MaxConcurrentReconciles: ctx.MaxConcurrentReconciles}).
Build(r)
if err != nil {
return err
}
err = controller.Watch(
&source.Kind{Type: &clusterv1.Cluster{}},
handler.EnqueueRequestsFromMapFunc(r.clusterToVSphereVMs),
predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldCluster := e.ObjectOld.(*clusterv1.Cluster)
newCluster := e.ObjectNew.(*clusterv1.Cluster)
return oldCluster.Spec.Paused && !newCluster.Spec.Paused
},
CreateFunc: func(e event.CreateEvent) bool {
if _, ok := e.Object.GetAnnotations()[clusterv1.PausedAnnotation]; !ok {
return false
}
return true
},
})
if err != nil {
return err
}
err = controller.Watch(
&source.Kind{Type: &infrav1.VSphereCluster{}},
handler.EnqueueRequestsFromMapFunc(r.vsphereClusterToVSphereVMs),
predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldCluster := e.ObjectOld.(*infrav1.VSphereCluster)
newCluster := e.ObjectNew.(*infrav1.VSphereCluster)
return !clustermodule.Compare(oldCluster.Spec.ClusterModules, newCluster.Spec.ClusterModules)
},
CreateFunc: func(e event.CreateEvent) bool { return false },
DeleteFunc: func(e event.DeleteEvent) bool { return false },
GenericFunc: func(e event.GenericEvent) bool { return false },
})
if err != nil {
return err
}
return nil
}
type vmReconciler struct {
*context.ControllerContext
VMService services.VirtualMachineService
}
// Reconcile ensures the back-end state reflects the Kubernetes resource state intent.
func (r vmReconciler) Reconcile(ctx goctx.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
// Get the VSphereVM resource for this request.
vsphereVM := &infrav1.VSphereVM{}
if err := r.Client.Get(r, req.NamespacedName, vsphereVM); err != nil {
if apierrors.IsNotFound(err) {
r.Logger.Info("VSphereVM not found, won't reconcile", "key", req.NamespacedName)
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Create the patch helper.
patchHelper, err := patch.NewHelper(vsphereVM, r.Client)
if err != nil {
return reconcile.Result{}, errors.Wrapf(
err,
"failed to init patch helper for %s %s/%s",
vsphereVM.GroupVersionKind(),
vsphereVM.Namespace,
vsphereVM.Name)
}
authSession, err := r.retrieveVcenterSession(ctx, vsphereVM)
if err != nil {
conditions.MarkFalse(vsphereVM, infrav1.VCenterAvailableCondition, infrav1.VCenterUnreachableReason, clusterv1.ConditionSeverityError, err.Error())
return reconcile.Result{}, err
}
conditions.MarkTrue(vsphereVM, infrav1.VCenterAvailableCondition)
// Fetch the owner VSphereMachine.
vsphereMachine, err := util.GetOwnerVSphereMachine(r, r.Client, vsphereVM.ObjectMeta)
// vsphereMachine can be nil in cases where custom mover other than clusterctl
// moves the resources without ownerreferences set
// in that case nil vsphereMachine can cause panic and CrashLoopBackOff the pod
// preventing vspheremachine_controller from setting the ownerref
if err != nil || vsphereMachine == nil {
r.Logger.Info("Owner VSphereMachine not found, won't reconcile", "key", req.NamespacedName)
return reconcile.Result{}, nil
}
vsphereCluster, err := util.GetVSphereClusterFromVSphereMachine(r, r.Client, vsphereMachine)
if err != nil || vsphereCluster == nil {
r.Logger.Info("VSphereCluster not found, won't reconcile", "key", ctrlclient.ObjectKeyFromObject(vsphereMachine))
return reconcile.Result{}, nil
}
// Fetch the CAPI Machine.
machine, err := clusterutilv1.GetOwnerMachine(r, r.Client, vsphereMachine.ObjectMeta)
if err != nil {
return reconcile.Result{}, err
}
if machine == nil {
r.Logger.Info("Waiting for OwnerRef to be set on VSphereMachine", "key", vsphereMachine.Name)
return reconcile.Result{}, nil
}
var vsphereFailureDomain *infrav1.VSphereFailureDomain
if failureDomain := machine.Spec.FailureDomain; failureDomain != nil {
vsphereDeploymentZone := &infrav1.VSphereDeploymentZone{}
if err := r.Client.Get(r, apitypes.NamespacedName{Name: *failureDomain}, vsphereDeploymentZone); err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to find vsphere deployment zone %s", *failureDomain)
}
vsphereFailureDomain = &infrav1.VSphereFailureDomain{}
if err := r.Client.Get(r, apitypes.NamespacedName{Name: vsphereDeploymentZone.Spec.FailureDomain}, vsphereFailureDomain); err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to find vsphere failure domain %s", vsphereDeploymentZone.Spec.FailureDomain)
}
}
// Create the VM context for this request.
vmContext := &context.VMContext{
ControllerContext: r.ControllerContext,
VSphereVM: vsphereVM,
VSphereFailureDomain: vsphereFailureDomain,
Session: authSession,
Logger: r.Logger.WithName(req.Namespace).WithName(req.Name),
PatchHelper: patchHelper,
}
// Print the task-ref upon entry and upon exit.
vmContext.Logger.V(4).Info(
"VSphereVM.Status.TaskRef OnEntry",
"task-ref", vmContext.VSphereVM.Status.TaskRef)
defer func() {
vmContext.Logger.V(4).Info(
"VSphereVM.Status.TaskRef OnExit",
"task-ref", vmContext.VSphereVM.Status.TaskRef)
}()
// Always issue a patch when exiting this function so changes to the
// resource are patched back to the API server.
defer func() {
// always update the readyCondition.
conditions.SetSummary(vmContext.VSphereVM,
conditions.WithConditions(
infrav1.VMProvisionedCondition,
infrav1.VCenterAvailableCondition,
),
)
// Patch the VSphereVM resource.
if err := vmContext.Patch(); err != nil {
if reterr == nil {
reterr = err
}
vmContext.Logger.Error(err, "patch failed", "vm", vmContext.String())
}
}()
cluster, err := clusterutilv1.GetClusterFromMetadata(r.ControllerContext, r.Client, vsphereVM.ObjectMeta)
if err == nil {
if annotations.IsPaused(cluster, vsphereVM) {
r.Logger.V(4).Info("VSphereVM %s/%s linked to a cluster that is paused",
vsphereVM.Namespace, vsphereVM.Name)
return reconcile.Result{}, nil
}
}
return r.reconcile(vmContext, fetchClusterModuleInput{
VSphereCluster: vsphereCluster,
Machine: machine,
})
}
// reconcile encases the behavior of the controller around cluster module information
// retrieval depending upon inputs passed.
//
// This logic was moved to a smaller function outside of the main Reconcile() loop
// for the ease of testing.
func (r vmReconciler) reconcile(ctx *context.VMContext, input fetchClusterModuleInput) (reconcile.Result, error) {
if feature.Gates.Enabled(feature.NodeAntiAffinity) {
clusterModuleInfo, err := r.fetchClusterModuleInfo(input)
// If cluster module information cannot be fetched for a VM being deleted,
// we should not block VM deletion since the cluster module is updated
// once the VM gets removed.
if err != nil && ctx.VSphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
return reconcile.Result{}, err
}
ctx.ClusterModuleInfo = clusterModuleInfo
}
// Handle deleted machines
if !ctx.VSphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx)
}
// Handle non-deleted machines
return r.reconcileNormal(ctx)
}
func (r vmReconciler) reconcileDelete(ctx *context.VMContext) (reconcile.Result, error) {
ctx.Logger.Info("Handling deleted VSphereVM")
conditions.MarkFalse(ctx.VSphereVM, infrav1.VMProvisionedCondition, clusterv1.DeletingReason, clusterv1.ConditionSeverityInfo, "")
vm, err := r.VMService.DestroyVM(ctx)
if err != nil {
conditions.MarkFalse(ctx.VSphereVM, infrav1.VMProvisionedCondition, "DeletionFailed", clusterv1.ConditionSeverityWarning, err.Error())
return reconcile.Result{}, errors.Wrapf(err, "failed to destroy VM")
}
// Requeue the operation until the VM is "notfound".
if vm.State != infrav1.VirtualMachineStateNotFound {
ctx.Logger.Info("vm state is not reconciled", "expected-vm-state", infrav1.VirtualMachineStateNotFound, "actual-vm-state", vm.State)
return reconcile.Result{}, nil
}
// Attempt to delete the node corresponding to the vsphere VM
err = r.deleteNode(ctx, vm.Name)
if err != nil {
r.Logger.V(6).Info("unable to delete node", "err", err)
}
// Remove finalizers from any ipam claims
for devIdx, device := range ctx.VSphereVM.Spec.Network.Devices {
for poolRefIdx := range device.AddressesFromPools {
// check if claim exists
ipAddrClaim := &ipamv1.IPAddressClaim{}
ipAddrClaimName := govmomi.IPAddressClaimName(ctx.VSphereVM.Name, devIdx, poolRefIdx)
ctx.Logger.Info("removing finalizer", "IPAddressClaim", ipAddrClaimName)
ipAddrClaimKey := apitypes.NamespacedName{
Namespace: ctx.VSphereVM.Namespace,
Name: ipAddrClaimName,
}
if err := ctx.Client.Get(ctx, ipAddrClaimKey, ipAddrClaim); err != nil {
if apierrors.IsNotFound(err) {
continue
}
return reconcile.Result{}, errors.Wrapf(err, fmt.Sprintf("failed to find IPAddressClaim %q to remove the finalizer", ipAddrClaimName))
}
if ctrlutil.RemoveFinalizer(ipAddrClaim, infrav1.IPAddressClaimFinalizer) {
if err := ctx.Client.Update(ctx, ipAddrClaim); err != nil {
return reconcile.Result{}, errors.Wrapf(err, fmt.Sprintf("failed to update IPAddressClaim %q", ipAddrClaimName))
}
}
}
}
// The VM is deleted so remove the finalizer.
ctrlutil.RemoveFinalizer(ctx.VSphereVM, infrav1.VMFinalizer)
return reconcile.Result{}, nil
}
// deleteNode attempts to find and best effort delete the node corresponding to the VM
// This is necessary since CAPI does not the nodeRef field on the owner Machine object
// until the node moves to Ready state. Hence, on Machine deletion it is unable to delete
// the kubernetes node corresponding to the VM.
func (r vmReconciler) deleteNode(ctx *context.VMContext, name string) error {
// Fetching the cluster object from the VSphereVM object to create a remote client to the cluster
cluster, err := clusterutilv1.GetClusterFromMetadata(r.ControllerContext, r.Client, ctx.VSphereVM.ObjectMeta)
if err != nil {
return err
}
clusterClient, err := remote.NewClusterClient(ctx, r.ControllerContext.Name, r.Client, ctrlclient.ObjectKeyFromObject(cluster))
if err != nil {
return err
}
// Attempt to delete the corresponding node
node := &apiv1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
if err := clusterClient.Delete(ctx, node); err != nil {
return err
}
return nil
}
func (r vmReconciler) reconcileNormal(ctx *context.VMContext) (reconcile.Result, error) {
if ctx.VSphereVM.Status.FailureReason != nil || ctx.VSphereVM.Status.FailureMessage != nil {
r.Logger.Info("VM is failed, won't reconcile", "namespace", ctx.VSphereVM.Namespace, "name", ctx.VSphereVM.Name)
return reconcile.Result{}, nil
}
// If the VSphereVM doesn't have our finalizer, add it.
ctrlutil.AddFinalizer(ctx.VSphereVM, infrav1.VMFinalizer)
if r.isWaitingForStaticIPAllocation(ctx) {
conditions.MarkFalse(ctx.VSphereVM, infrav1.VMProvisionedCondition, infrav1.WaitingForStaticIPAllocationReason, clusterv1.ConditionSeverityInfo, "")
ctx.Logger.Info("vm is waiting for static ip to be available")
return reconcile.Result{}, nil
}
// Get or create the VM.
vm, err := r.VMService.ReconcileVM(ctx)
if err != nil {
ctx.Logger.Error(err, "error reconciling VM")
return reconcile.Result{}, errors.Wrapf(err, "failed to reconcile VM")
}
// Do not proceed until the backend VM is marked ready.
if vm.State != infrav1.VirtualMachineStateReady {
ctx.Logger.Info(
"VM state is not reconciled",
"expected-vm-state", infrav1.VirtualMachineStateReady,
"actual-vm-state", vm.State)
return reconcile.Result{}, nil
}
// Update the VSphereVM's BIOS UUID.
ctx.Logger.Info("vm bios-uuid", "biosuuid", vm.BiosUUID)
// defensive check to ensure we are not removing the biosUUID
if vm.BiosUUID != "" {
ctx.VSphereVM.Spec.BiosUUID = vm.BiosUUID
} else {
return reconcile.Result{}, errors.Errorf("bios uuid is empty while VM is ready")
}
// Update the VSphereVM's network status.
r.reconcileNetwork(ctx, vm)
// we didn't get any addresses, requeue
if len(ctx.VSphereVM.Status.Addresses) == 0 {
conditions.MarkFalse(ctx.VSphereVM, infrav1.VMProvisionedCondition, infrav1.WaitingForIPAllocationReason, clusterv1.ConditionSeverityInfo, "")
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil
}
// Once the network is online the VM is considered ready.
ctx.VSphereVM.Status.Ready = true
conditions.MarkTrue(ctx.VSphereVM, infrav1.VMProvisionedCondition)
ctx.Logger.Info("VSphereVM is ready")
return reconcile.Result{}, nil
}
// isWaitingForStaticIPAllocation checks whether the VM should wait for a static IP
// to be allocated.
// It checks the state of both DHCP4 and DHCP6 for all the network devices and if
// any static IP addresses or IPAM Pools are specified.
func (r vmReconciler) isWaitingForStaticIPAllocation(ctx *context.VMContext) bool {
devices := ctx.VSphereVM.Spec.Network.Devices
for _, dev := range devices {
if !dev.DHCP4 && !dev.DHCP6 && len(dev.IPAddrs) == 0 && len(dev.AddressesFromPools) == 0 {
// Static IP is not available yet
return true
}
}
return false
}
func (r vmReconciler) reconcileNetwork(ctx *context.VMContext, vm infrav1.VirtualMachine) {
ctx.VSphereVM.Status.Network = vm.Network
ipAddrs := make([]string, 0, len(vm.Network))
for _, netStatus := range ctx.VSphereVM.Status.Network {
ipAddrs = append(ipAddrs, netStatus.IPAddrs...)
}
ctx.VSphereVM.Status.Addresses = ipAddrs
}
func (r vmReconciler) clusterToVSphereVMs(a ctrlclient.Object) []reconcile.Request {
requests := []reconcile.Request{}
vms := &infrav1.VSphereVMList{}
err := r.Client.List(goctx.Background(), vms, ctrlclient.MatchingLabels(
map[string]string{
clusterv1.ClusterLabelName: a.GetName(),
},
))
if err != nil {
return requests
}
for _, vm := range vms.Items {
r := reconcile.Request{
NamespacedName: apitypes.NamespacedName{
Name: vm.Name,
Namespace: vm.Namespace,
},
}
requests = append(requests, r)
}
return requests
}
func (r vmReconciler) vsphereClusterToVSphereVMs(a ctrlclient.Object) []reconcile.Request {
vsphereCluster, ok := a.(*infrav1.VSphereCluster)
if !ok {
return nil
}
clusterName, ok := vsphereCluster.Labels[clusterv1.ClusterLabelName]
if !ok {
return nil
}
requests := []reconcile.Request{}
vms := &infrav1.VSphereVMList{}
err := r.Client.List(goctx.Background(), vms, ctrlclient.MatchingLabels(
map[string]string{
clusterv1.ClusterLabelName: clusterName,
},
))
if err != nil {
return requests
}
for _, vm := range vms.Items {
r := reconcile.Request{
NamespacedName: apitypes.NamespacedName{
Name: vm.Name,
Namespace: vm.Namespace,
},
}
requests = append(requests, r)
}
return requests
}
func (r vmReconciler) retrieveVcenterSession(ctx goctx.Context, vsphereVM *infrav1.VSphereVM) (*session.Session, error) {
// Get cluster object and then get VSphereCluster object
params := session.NewParams().
WithServer(vsphereVM.Spec.Server).
WithDatacenter(vsphereVM.Spec.Datacenter).
WithUserInfo(r.ControllerContext.Username, r.ControllerContext.Password).
WithThumbprint(vsphereVM.Spec.Thumbprint).
WithFeatures(session.Feature{
KeepAliveDuration: r.KeepAliveDuration,
})
cluster, err := clusterutilv1.GetClusterFromMetadata(r.ControllerContext, r.Client, vsphereVM.ObjectMeta)
if err != nil {
r.Logger.Info("VsphereVM is missing cluster label or cluster does not exist")
return session.GetOrCreate(r.Context,
params)
}
key := ctrlclient.ObjectKey{
Namespace: cluster.Namespace,
Name: cluster.Spec.InfrastructureRef.Name,
}
vsphereCluster := &infrav1.VSphereCluster{}
err = r.Client.Get(r, key, vsphereCluster)
if err != nil {
r.Logger.Info("VSphereCluster couldn't be retrieved")
return session.GetOrCreate(r.Context,
params)
}
if vsphereCluster.Spec.IdentityRef != nil {
creds, err := identity.GetCredentials(ctx, r.Client, vsphereCluster, r.Namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve credentials from IdentityRef")
}
params = params.WithUserInfo(creds.Username, creds.Password)
return session.GetOrCreate(r.Context,
params)
}
// Fallback to using credentials provided to the manager
return session.GetOrCreate(r.Context,
params)
}
func (r vmReconciler) fetchClusterModuleInfo(clusterModInput fetchClusterModuleInput) (*string, error) {
var (
owner ctrlclient.Object
err error
)
machine := clusterModInput.Machine
logger := r.Logger.WithName(machine.Namespace).WithName(machine.Name)
input := util.FetchObjectInput{
Context: r.Context,
Client: r.Client,
Object: machine,
}
// TODO (srm09): Figure out a way to find the latest version of the CRD
if util.IsControlPlaneMachine(machine) {
owner, err = util.FetchControlPlaneOwnerObject(input)
} else {
owner, err = util.FetchMachineDeploymentOwnerObject(input)
}
if err != nil {
// If the owner objects cannot be traced, we can assume that the objects
// have been deleted in which case we do not want cluster module info populated
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
for _, mod := range clusterModInput.VSphereCluster.Spec.ClusterModules {
if mod.TargetObjectName == owner.GetName() {
logger.Info("cluster module with UUID found", "moduleUUID", mod.ModuleUUID)
return pointer.String(mod.ModuleUUID), nil
}
}
logger.V(4).Info("no cluster module found")
return nil, nil
}
type fetchClusterModuleInput struct {
VSphereCluster *infrav1.VSphereCluster
Machine *clusterv1.Machine
}