48
48
failuredomainMatcher = regexp .MustCompile (`ds\.meta_data\.failuredomain` )
49
49
)
50
50
51
+ const (
52
+ BootstrapDataNotReady = "Bootstrap DataSecretName not yet available"
53
+ CSMachineCreationSuccess = "CloudStack instance Created"
54
+ CSMachineCreationFailed = "Creating CloudStack machine failed: %s"
55
+ MachineInstanceRunning = "Machine instance is Running..."
56
+ MachineInErrorMessage = "CloudStackMachine VM in error state. Deleting associated Machine"
57
+ MachineNotReadyMessage = "Instance not ready, is %s"
58
+ CSMachineStateCheckerCreationFailed = "error encountered when creating CloudStackMachineStateChecker"
59
+ CSMachineStateCheckerCreationSuccess = "CloudStackMachineStateChecker created"
60
+ CSMachineDeletionMessage = "Deleting CloudStack Machine %s"
61
+ CSMachineDeletionInstanceIDNotFoundMessage = "Deleting CloudStack Machine %s instanceID not found"
62
+ )
63
+
51
64
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines,verbs=get;list;watch;create;update;patch;delete
52
65
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/status,verbs=get;update;patch
53
66
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/finalizers,verbs=update
@@ -187,8 +200,8 @@ func (r *CloudStackMachineReconciliationRunner) DeleteMachineIfFailuredomainNotE
187
200
// Implicitly it also fetches its bootstrap secret in order to create said instance.
188
201
func (r * CloudStackMachineReconciliationRunner ) GetOrCreateVMInstance () (retRes ctrl.Result , reterr error ) {
189
202
if r .CAPIMachine .Spec .Bootstrap .DataSecretName == nil {
190
- r .Recorder .Event (r .ReconciliationSubject , "Normal" , "Creating" , "Bootstrap DataSecretName not yet available" )
191
- return r .RequeueWithMessage ("Bootstrap DataSecretName not yet available ." )
203
+ r .Recorder .Event (r .ReconciliationSubject , "Normal" , "Creating" , BootstrapDataNotReady )
204
+ return r .RequeueWithMessage (BootstrapDataNotReady + " ." )
192
205
}
193
206
r .Log .Info ("Got Bootstrap DataSecretName." )
194
207
@@ -207,11 +220,11 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes
207
220
err := r .CSUser .GetOrCreateVMInstance (r .ReconciliationSubject , r .CAPIMachine , r .CSCluster , r .FailureDomain , r .AffinityGroup , userData )
208
221
209
222
if err != nil {
210
- r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , "Creating" , "Creating CloudStack machine failed: %s" , err .Error ())
223
+ r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , "Creating" , CSMachineCreationFailed , err .Error ())
211
224
}
212
225
if err == nil && ! controllerutil .ContainsFinalizer (r .ReconciliationSubject , infrav1 .MachineFinalizer ) { // Fetched or Created?
213
- r .Recorder .Eventf (r .ReconciliationSubject , "Normal" , "Created" , "CloudStack instance Created" )
214
- r .Log .Info ("CloudStack instance Created" , "instanceStatus" , r .ReconciliationSubject .Status )
226
+ r .Recorder .Eventf (r .ReconciliationSubject , "Normal" , "Created" , CSMachineCreationSuccess )
227
+ r .Log .Info (CSMachineCreationSuccess , "instanceStatus" , r .ReconciliationSubject .Status )
215
228
}
216
229
// Always add the finalizer regardless. It can't be added twice anyway.
217
230
controllerutil .AddFinalizer (r .ReconciliationSubject , infrav1 .MachineFinalizer )
@@ -229,19 +242,19 @@ func processCustomMetadata(data []byte, r *CloudStackMachineReconciliationRunner
229
242
// ConfirmVMStatus checks the Instance's status for running state and requeues otherwise.
230
243
func (r * CloudStackMachineReconciliationRunner ) RequeueIfInstanceNotRunning () (retRes ctrl.Result , reterr error ) {
231
244
if r .ReconciliationSubject .Status .InstanceState == "Running" {
232
- r .Recorder .Event (r .ReconciliationSubject , "Normal" , "Running" , "Machine instance is Running..." )
233
- r .Log .Info ("Machine instance is Running..." )
245
+ r .Recorder .Event (r .ReconciliationSubject , "Normal" , "Running" , MachineInstanceRunning )
246
+ r .Log .Info (MachineInstanceRunning )
234
247
r .ReconciliationSubject .Status .Ready = true
235
248
} else if r .ReconciliationSubject .Status .InstanceState == "Error" {
236
- r .Recorder .Event (r .ReconciliationSubject , "Warning" , "Error" , "CloudStackMachine VM in error state. Deleting associated Machine" )
237
- r .Log .Info ("CloudStackMachine VM in error state. Deleting associated Machine." , "csMachine" , r .ReconciliationSubject .GetName ())
249
+ r .Recorder .Event (r .ReconciliationSubject , "Warning" , "Error" , MachineInErrorMessage )
250
+ r .Log .Info (MachineInErrorMessage , "csMachine" , r .ReconciliationSubject .GetName ())
238
251
if err := r .K8sClient .Delete (r .RequestCtx , r .CAPIMachine ); err != nil {
239
252
return ctrl.Result {}, err
240
253
}
241
254
return ctrl.Result {RequeueAfter : utils .RequeueTimeout }, nil
242
255
} else {
243
- r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , r .ReconciliationSubject .Status .InstanceState , "Instance not ready, is %s" , r .ReconciliationSubject .Status .InstanceState )
244
- r .Log .Info (fmt .Sprintf ("Instance not ready, is %s." , r .ReconciliationSubject .Status .InstanceState ))
256
+ r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , r .ReconciliationSubject .Status .InstanceState , MachineNotReadyMessage , r .ReconciliationSubject .Status .InstanceState )
257
+ r .Log .Info (fmt .Sprintf (MachineNotReadyMessage , r .ReconciliationSubject .Status .InstanceState ))
245
258
return ctrl.Result {RequeueAfter : utils .RequeueTimeout }, nil
246
259
}
247
260
return ctrl.Result {}, nil
@@ -272,10 +285,10 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker()
272
285
}
273
286
274
287
if err := r .K8sClient .Create (r .RequestCtx , csMachineStateChecker ); err != nil && ! utils .ContainsAlreadyExistsSubstring (err ) {
275
- r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , "Machine State Checker" , "error encountered when creating CloudStackMachineStateChecker: %s" , err . Error () )
276
- return r .ReturnWrappedError (err , "error encountered when creating CloudStackMachineStateChecker" )
288
+ r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , "Machine State Checker" , CSMachineStateCheckerCreationFailed )
289
+ return r .ReturnWrappedError (err , CSMachineStateCheckerCreationFailed )
277
290
}
278
- r .Recorder .Eventf (r .ReconciliationSubject , "Normal" , "Machine State Checker" , "CloudStackMachineStateChecker %s created" , * checkerName )
291
+ r .Recorder .Eventf (r .ReconciliationSubject , "Normal" , "Machine State Checker" , CSMachineStateCheckerCreationSuccess )
279
292
return r .GetObjectByName (* checkerName , r .StateChecker )()
280
293
}
281
294
@@ -290,11 +303,12 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
290
303
fmt .Sprintf (" If this VM has already been deleted, please remove the finalizer named %s from object %s" ,
291
304
"cloudstackmachine.infrastructure.cluster.x-k8s.io" , r .ReconciliationSubject .Name ))
292
305
// Cloudstack VM may be not found or more than one found by name
293
- r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , "Deleting" , "Deleting CloudStack Machine %s instanceID not found" , r .ReconciliationSubject .Name )
306
+ r .Recorder .Eventf (r .ReconciliationSubject , "Warning" , "Deleting" , CSMachineDeletionInstanceIDNotFoundMessage , r .ReconciliationSubject .Name )
307
+ r .Log .Error (err , fmt .Sprintf (CSMachineDeletionInstanceIDNotFoundMessage , r .ReconciliationSubject .Name ))
294
308
return ctrl.Result {}, err
295
309
}
296
310
}
297
- r .Recorder .Eventf (r .ReconciliationSubject , "Normal" , "Deleting" , "Deleting CloudStack Machine %s" , r .ReconciliationSubject .Name )
311
+ r .Recorder .Eventf (r .ReconciliationSubject , "Normal" , "Deleting" , CSMachineDeletionMessage , r .ReconciliationSubject .Name )
298
312
r .Log .Info ("Deleting instance" , "instance-id" , r .ReconciliationSubject .Spec .InstanceID )
299
313
// Use CSClient instead of CSUser here to expunge as admin.
300
314
// The CloudStack-Go API does not return an error, but the VM won't delete with Expunge set if requested by
0 commit comments