Skip to content

Commit 7f1be54

Browse files
committed
use const string to replace string literal
1 parent 7592ee5 commit 7f1be54

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

controllers/cloudstackmachine_controller.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ var (
4848
failuredomainMatcher = regexp.MustCompile(`ds\.meta_data\.failuredomain`)
4949
)
5050

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+
5164
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines,verbs=get;list;watch;create;update;patch;delete
5265
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/status,verbs=get;update;patch
5366
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/finalizers,verbs=update
@@ -187,8 +200,8 @@ func (r *CloudStackMachineReconciliationRunner) DeleteMachineIfFailuredomainNotE
187200
// Implicitly it also fetches its bootstrap secret in order to create said instance.
188201
func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes ctrl.Result, reterr error) {
189202
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 + ".")
192205
}
193206
r.Log.Info("Got Bootstrap DataSecretName.")
194207

@@ -207,11 +220,11 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes
207220
err := r.CSUser.GetOrCreateVMInstance(r.ReconciliationSubject, r.CAPIMachine, r.CSCluster, r.FailureDomain, r.AffinityGroup, userData)
208221

209222
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())
211224
}
212225
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)
215228
}
216229
// Always add the finalizer regardless. It can't be added twice anyway.
217230
controllerutil.AddFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer)
@@ -229,19 +242,19 @@ func processCustomMetadata(data []byte, r *CloudStackMachineReconciliationRunner
229242
// ConfirmVMStatus checks the Instance's status for running state and requeues otherwise.
230243
func (r *CloudStackMachineReconciliationRunner) RequeueIfInstanceNotRunning() (retRes ctrl.Result, reterr error) {
231244
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)
234247
r.ReconciliationSubject.Status.Ready = true
235248
} 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())
238251
if err := r.K8sClient.Delete(r.RequestCtx, r.CAPIMachine); err != nil {
239252
return ctrl.Result{}, err
240253
}
241254
return ctrl.Result{RequeueAfter: utils.RequeueTimeout}, nil
242255
} 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))
245258
return ctrl.Result{RequeueAfter: utils.RequeueTimeout}, nil
246259
}
247260
return ctrl.Result{}, nil
@@ -272,10 +285,10 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker()
272285
}
273286

274287
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)
277290
}
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)
279292
return r.GetObjectByName(*checkerName, r.StateChecker)()
280293
}
281294

@@ -290,11 +303,12 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
290303
fmt.Sprintf(" If this VM has already been deleted, please remove the finalizer named %s from object %s",
291304
"cloudstackmachine.infrastructure.cluster.x-k8s.io", r.ReconciliationSubject.Name))
292305
// 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))
294308
return ctrl.Result{}, err
295309
}
296310
}
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)
298312
r.Log.Info("Deleting instance", "instance-id", r.ReconciliationSubject.Spec.InstanceID)
299313
// Use CSClient instead of CSUser here to expunge as admin.
300314
// The CloudStack-Go API does not return an error, but the VM won't delete with Expunge set if requested by

controllers/cloudstackmachine_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,12 @@ var _ = Describe("CloudStackMachineReconciler", func() {
252252
res, err := MachineReconciler.Reconcile(ctx, ctrl.Request{NamespacedName: requestNamespacedName})
253253
Ω(err).ShouldNot(HaveOccurred())
254254
Ω(res.RequeueAfter).Should(BeZero())
255-
255+
256256
Eventually(func() bool {
257257
for event := range fakeRecorder.Events {
258258
return strings.Contains(event, "Normal Created CloudStack instance Created") ||
259259
strings.Contains(event, "Normal Running Machine instance is Running...") ||
260-
strings.Contains(event, "Normal Machine State Checker CloudStackMachineStateChecker Instance1 created")
260+
strings.Contains(event, "Normal Machine State Checker CloudStackMachineStateChecker created")
261261
}
262262
return false
263263
}, timeout).Should(BeTrue())

0 commit comments

Comments
 (0)