-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathibmpowervsmachine_controller.go
386 lines (337 loc) · 15.4 KB
/
ibmpowervsmachine_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
/*
Copyright 2021 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"
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/IBM-Cloud/power-go-client/power/models"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/predicates"
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints"
capibmrecord "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record"
)
// IBMPowerVSMachineReconciler reconciles a IBMPowerVSMachine object.
type IBMPowerVSMachineReconciler struct {
client.Client
Log logr.Logger
Recorder record.EventRecorder
ServiceEndpoint []endpoints.ServiceEndpoint
Scheme *runtime.Scheme
}
// dhcpCacheStore is a cache store to hold the Power VS VM DHCP IP.
var dhcpCacheStore cache.Store
func init() {
dhcpCacheStore = powervs.InitialiseDHCPCacheStore()
}
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsmachines,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsmachines/status,verbs=get;update;patch
// Reconcile implements controller runtime Reconciler interface and handles reconcileation logic for IBMPowerVSMachine.
func (r *IBMPowerVSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
log := ctrl.LoggerFrom(ctx)
ibmPowerVSMachine := &infrav1beta2.IBMPowerVSMachine{}
err := r.Get(ctx, req.NamespacedName, ibmPowerVSMachine)
if err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
// Fetch the Machine.
machine, err := util.GetOwnerMachine(ctx, r.Client, ibmPowerVSMachine.ObjectMeta)
if err != nil {
return ctrl.Result{}, err
}
if machine == nil {
log.Info("Machine Controller has not yet set OwnerRef")
return ctrl.Result{}, nil
}
log = log.WithValues("ibmPowerVSMachine", machine.Name)
// Fetch the Cluster.
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, ibmPowerVSMachine.ObjectMeta)
if err != nil {
log.Info("Machine is missing cluster label or cluster does not exist")
return ctrl.Result{}, nil
}
log = log.WithValues("cluster", cluster.Name)
ibmCluster := &infrav1beta2.IBMPowerVSCluster{}
ibmPowerVSClusterName := client.ObjectKey{
Namespace: ibmPowerVSMachine.Namespace,
Name: cluster.Spec.InfrastructureRef.Name,
}
if err := r.Client.Get(ctx, ibmPowerVSClusterName, ibmCluster); err != nil {
log.Info("IBMPowerVSCluster is not available yet")
return ctrl.Result{}, nil
}
var ibmPowerVSImage *infrav1beta2.IBMPowerVSImage
if ibmPowerVSMachine.Spec.ImageRef != nil {
ibmPowerVSImage = &infrav1beta2.IBMPowerVSImage{}
ibmPowerVSImageName := client.ObjectKey{
Namespace: ibmPowerVSMachine.Namespace,
Name: ibmPowerVSMachine.Spec.ImageRef.Name,
}
if err := r.Client.Get(ctx, ibmPowerVSImageName, ibmPowerVSImage); err != nil {
log.Info("IBMPowerVSImage is not available yet", "IBMPowerVSImage", klog.KObj(ibmPowerVSImage))
return ctrl.Result{}, nil
}
}
// Create the machine scope.
machineScope, err := scope.NewPowerVSMachineScope(scope.PowerVSMachineScopeParams{
Client: r.Client,
Logger: log,
Cluster: cluster,
IBMPowerVSCluster: ibmCluster,
Machine: machine,
IBMPowerVSMachine: ibmPowerVSMachine,
IBMPowerVSImage: ibmPowerVSImage,
ServiceEndpoint: r.ServiceEndpoint,
DHCPIPCacheStore: dhcpCacheStore,
})
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create scope: %w", err)
}
// Always close the scope when exiting this function so we can persist any IBMPowerVSMachine changes.
defer func() {
if machineScope != nil {
if err := machineScope.Close(); err != nil && reterr == nil {
reterr = err
}
}
}()
// Handle deleted machines.
if !ibmPowerVSMachine.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(machineScope)
}
// Handle non-deleted machines.
return r.reconcileNormal(ctx, machineScope)
}
func (r *IBMPowerVSMachineReconciler) reconcileDelete(scope *scope.PowerVSMachineScope) (_ ctrl.Result, reterr error) {
scope.Info("Handling deleted IBMPowerVSMachine")
defer func() {
if reterr == nil {
// VSI is deleted so remove the finalizer.
controllerutil.RemoveFinalizer(scope.IBMPowerVSMachine, infrav1beta2.IBMPowerVSMachineFinalizer)
}
}()
if scope.IBMPowerVSMachine.Status.InstanceID == "" {
scope.Info("InstanceID is not yet set, hence not invoking the PowerVS API to delete the instance")
return ctrl.Result{}, nil
}
if err := scope.DeleteMachine(); err != nil {
scope.Info("error deleting IBMPowerVSMachine")
return ctrl.Result{}, fmt.Errorf("error deleting IBMPowerVSMachine %v: %w", klog.KObj(scope.IBMPowerVSMachine), err)
}
if err := scope.DeleteMachineIgnition(); err != nil {
scope.Info("error deleting IBMPowerVSMachine ignition")
return ctrl.Result{}, fmt.Errorf("error deleting IBMPowerVSMachine ignition %v: %w", klog.KObj(scope.IBMPowerVSMachine), err)
}
// Remove the cached VM IP
err := scope.DHCPIPCacheStore.Delete(powervs.VMip{Name: scope.IBMPowerVSMachine.Name})
if err != nil {
scope.Error(err, "failed to delete the VM entry from DHCP cache store", "VM", scope.IBMPowerVSMachine.Name)
}
return ctrl.Result{}, nil
}
func (r *IBMPowerVSMachineReconciler) getOrCreate(scope *scope.PowerVSMachineScope) (*models.PVMInstanceReference, error) {
instance, err := scope.CreateMachine()
return instance, err
}
// handleLoadBalancerPoolMemberConfiguration handles loadbalancer pool member creation flow.
func (r *IBMPowerVSMachineReconciler) handleLoadBalancerPoolMemberConfiguration(machineScope *scope.PowerVSMachineScope) (ctrl.Result, error) {
poolMember, err := machineScope.CreateVPCLoadBalancerPoolMember()
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create loadbalancer pool member %s: %w", machineScope.IBMPowerVSMachine.Name, err)
}
if poolMember != nil && *poolMember.ProvisioningStatus != string(infrav1beta2.VPCLoadBalancerStateActive) {
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}
return ctrl.Result{}, nil
}
func (r *IBMPowerVSMachineReconciler) reconcileNormal(ctx context.Context, machineScope *scope.PowerVSMachineScope) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
log.V(3).Info("Reconciling IBMPowerVSMachine")
if !machineScope.Cluster.Status.InfrastructureReady {
log.V(3).Info("Cluster infrastructure is not ready yet")
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.WaitingForClusterInfrastructureReason, capiv1beta1.ConditionSeverityInfo, "")
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}
if machineScope.IBMPowerVSImage != nil {
if !machineScope.IBMPowerVSImage.Status.Ready {
log.V(3).Info("IBMPowerVSImage is not ready yet")
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.WaitingForIBMPowerVSImageReason, capiv1beta1.ConditionSeverityInfo, "")
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}
}
// Make sure bootstrap data is available and populated.
if machineScope.Machine.Spec.Bootstrap.DataSecretName == nil {
log.V(3).Info("Bootstrap data secret reference is not yet available")
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.WaitingForBootstrapDataReason, capiv1beta1.ConditionSeverityInfo, "")
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}
if controllerutil.AddFinalizer(machineScope.IBMPowerVSMachine, infrav1beta2.IBMPowerVSMachineFinalizer) {
return ctrl.Result{}, nil
}
ins, err := r.getOrCreate(machineScope)
if err != nil {
log.V(3).Error(err, "Unable to create instance")
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.InstanceProvisionFailedReason, capiv1beta1.ConditionSeverityError, "%s", err.Error())
return ctrl.Result{}, fmt.Errorf("failed to reconcile VSI for IBMPowerVSMachine %s/%s: %w", machineScope.IBMPowerVSMachine.Namespace, machineScope.IBMPowerVSMachine.Name, err)
}
if ins != nil {
instance, err := machineScope.IBMPowerVSClient.GetInstance(*ins.PvmInstanceID)
if err != nil {
return ctrl.Result{}, err
}
if err := machineScope.SetProviderID(*ins.PvmInstanceID); err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to set provider id")
}
machineScope.SetInstanceID(instance.PvmInstanceID)
machineScope.SetAddresses(instance)
machineScope.SetHealth(instance.Health)
machineScope.SetInstanceState(instance.Status)
switch machineScope.GetInstanceState() {
case infrav1beta2.PowerVSInstanceStateBUILD:
machineScope.SetNotReady()
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.InstanceNotReadyReason, capiv1beta1.ConditionSeverityWarning, "")
case infrav1beta2.PowerVSInstanceStateSHUTOFF:
machineScope.SetNotReady()
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.InstanceStoppedReason, capiv1beta1.ConditionSeverityError, "")
return ctrl.Result{}, nil
case infrav1beta2.PowerVSInstanceStateACTIVE:
machineScope.SetReady()
conditions.MarkTrue(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition)
case infrav1beta2.PowerVSInstanceStateERROR:
msg := ""
if instance.Fault != nil {
msg = instance.Fault.Details
}
machineScope.SetNotReady()
machineScope.SetFailureReason(infrav1beta2.UpdateMachineError)
machineScope.SetFailureMessage(msg)
conditions.MarkFalse(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.InstanceErroredReason, capiv1beta1.ConditionSeverityError, "%s", msg)
capibmrecord.Warnf(machineScope.IBMPowerVSMachine, "FailedBuildInstance", "Failed to build the instance - %s", msg)
return ctrl.Result{}, nil
default:
machineScope.SetNotReady()
log.V(3).Info("PowerVS instance state is undefined", "state", *instance.Status, "instance-id", machineScope.GetInstanceID())
conditions.MarkUnknown(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, "", "")
}
} else {
machineScope.SetNotReady()
conditions.MarkUnknown(machineScope.IBMPowerVSMachine, infrav1beta2.InstanceReadyCondition, infrav1beta2.InstanceStateUnknownReason, "")
}
// Requeue after 2 minute if machine is not ready to update status of the machine properly.
if !machineScope.IsReady() {
return ctrl.Result{RequeueAfter: 2 * time.Minute}, nil
}
if machineScope.IBMPowerVSCluster.Spec.VPC == nil || machineScope.IBMPowerVSCluster.Spec.VPC.Region == nil {
log.V(3).Info("Skipping configuring machine to loadbalancer as VPC is not set")
return ctrl.Result{}, nil
}
// Register instance with load balancer
log.V(3).Info("Updating loadbalancer for machine", "name", machineScope.IBMPowerVSMachine.Name)
internalIP := machineScope.GetMachineInternalIP()
if internalIP == "" {
log.V(3).Info("Unable to update the LoadBalancer, Machine internal IP not yet set", "machineName", machineScope.IBMPowerVSMachine.Name)
return ctrl.Result{}, nil
}
if util.IsControlPlaneMachine(machineScope.Machine) {
log.V(3).Info("Configuring loadbalancer configuration for control plane machine", "machineName", machineScope.IBMPowerVSMachine.Name)
return r.handleLoadBalancerPoolMemberConfiguration(machineScope)
}
log.V(3).Info("skipping loadbalancer configuration for worker machine", "machineName", machineScope.IBMPowerVSMachine.Name)
return ctrl.Result{}, nil
}
// IBMPowerVSClusterToIBMPowerVSMachines is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation
// of IBMPowerVSMachines.
func (r *IBMPowerVSMachineReconciler) IBMPowerVSClusterToIBMPowerVSMachines(ctx context.Context) handler.MapFunc {
log := ctrl.LoggerFrom(ctx)
return func(mapCtx context.Context, o client.Object) []ctrl.Request {
result := []ctrl.Request{}
c, ok := o.(*infrav1beta2.IBMPowerVSCluster)
if !ok {
log.Error(errors.Errorf("expected a IBMPowerVSCluster but got a %T", o), "failed to get IBMPowerVSMachines for IBMPowerVSCluster")
return nil
}
cluster, err := util.GetOwnerCluster(mapCtx, r.Client, c.ObjectMeta)
switch {
case apierrors.IsNotFound(err) || cluster == nil:
return result
case err != nil:
log.Error(err, "failed to get owning cluster")
return result
}
labels := map[string]string{capiv1beta1.ClusterNameLabel: cluster.Name}
machineList := &capiv1beta1.MachineList{}
if err := r.List(mapCtx, machineList, client.InNamespace(c.Namespace), client.MatchingLabels(labels)); err != nil {
log.Error(err, "failed to list Machines")
return nil
}
for _, m := range machineList.Items {
if m.Spec.InfrastructureRef.Name == "" {
continue
}
name := client.ObjectKey{Namespace: m.Namespace, Name: m.Spec.InfrastructureRef.Name}
result = append(result, ctrl.Request{NamespacedName: name})
}
return result
}
}
// SetupWithManager creates a new IBMVPCMachine controller for a manager.
func (r *IBMPowerVSMachineReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
controller, err := ctrl.NewControllerManagedBy(mgr).
For(&infrav1beta2.IBMPowerVSMachine{}).
WithEventFilter(predicates.ResourceNotPaused(r.Scheme, ctrl.LoggerFrom(ctx))).
Watches(
&capiv1beta1.Machine{},
handler.EnqueueRequestsFromMapFunc(util.MachineToInfrastructureMapFunc(infrav1beta2.GroupVersion.WithKind("IBMPowerVSMachine"))),
).
Watches(
&infrav1beta2.IBMPowerVSCluster{},
handler.EnqueueRequestsFromMapFunc(r.IBMPowerVSClusterToIBMPowerVSMachines(ctx)),
).
Build(r)
if err != nil {
return errors.Wrap(err, "error creating controller")
}
clusterToObjectFunc, err := util.ClusterToTypedObjectsMapper(r.Client, &infrav1beta2.IBMPowerVSMachineList{}, mgr.GetScheme())
if err != nil {
return errors.Wrap(err, "failed to create mapper for Cluster to IBMPowerVSMachines")
}
// Add a watch on capiv1beta1.Cluster object for unpause & ready notifications.
if err := controller.Watch(
source.Kind[client.Object](mgr.GetCache(), &capiv1beta1.Cluster{},
handler.EnqueueRequestsFromMapFunc(clusterToObjectFunc),
predicates.ClusterPausedTransitionsOrInfrastructureReady(r.Scheme, ctrl.LoggerFrom(ctx)),
)); err != nil {
return errors.Wrap(err, "failed adding a watch for ready clusters")
}
return nil
}