Skip to content

Commit

Permalink
use const string to replace string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyufe committed Dec 5, 2022
1 parent 7592ee5 commit 7f1be54
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
46 changes: 30 additions & 16 deletions controllers/cloudstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ var (
failuredomainMatcher = regexp.MustCompile(`ds\.meta_data\.failuredomain`)
)

const (
BootstrapDataNotReady = "Bootstrap DataSecretName not yet available"
CSMachineCreationSuccess = "CloudStack instance Created"
CSMachineCreationFailed = "Creating CloudStack machine failed: %s"
MachineInstanceRunning = "Machine instance is Running..."
MachineInErrorMessage = "CloudStackMachine VM in error state. Deleting associated Machine"
MachineNotReadyMessage = "Instance not ready, is %s"
CSMachineStateCheckerCreationFailed = "error encountered when creating CloudStackMachineStateChecker"
CSMachineStateCheckerCreationSuccess = "CloudStackMachineStateChecker created"
CSMachineDeletionMessage = "Deleting CloudStack Machine %s"
CSMachineDeletionInstanceIDNotFoundMessage = "Deleting CloudStack Machine %s instanceID not found"
)

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=cloudstackmachines/finalizers,verbs=update
Expand Down Expand Up @@ -187,8 +200,8 @@ func (r *CloudStackMachineReconciliationRunner) DeleteMachineIfFailuredomainNotE
// Implicitly it also fetches its bootstrap secret in order to create said instance.
func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes ctrl.Result, reterr error) {
if r.CAPIMachine.Spec.Bootstrap.DataSecretName == nil {
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Creating", "Bootstrap DataSecretName not yet available")
return r.RequeueWithMessage("Bootstrap DataSecretName not yet available.")
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Creating", BootstrapDataNotReady)
return r.RequeueWithMessage(BootstrapDataNotReady + ".")
}
r.Log.Info("Got Bootstrap DataSecretName.")

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

if err != nil {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Creating", "Creating CloudStack machine failed: %s", err.Error())
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Creating", CSMachineCreationFailed, err.Error())
}
if err == nil && !controllerutil.ContainsFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer) { // Fetched or Created?
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Created", "CloudStack instance Created")
r.Log.Info("CloudStack instance Created", "instanceStatus", r.ReconciliationSubject.Status)
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Created", CSMachineCreationSuccess)
r.Log.Info(CSMachineCreationSuccess, "instanceStatus", r.ReconciliationSubject.Status)
}
// Always add the finalizer regardless. It can't be added twice anyway.
controllerutil.AddFinalizer(r.ReconciliationSubject, infrav1.MachineFinalizer)
Expand All @@ -229,19 +242,19 @@ func processCustomMetadata(data []byte, r *CloudStackMachineReconciliationRunner
// ConfirmVMStatus checks the Instance's status for running state and requeues otherwise.
func (r *CloudStackMachineReconciliationRunner) RequeueIfInstanceNotRunning() (retRes ctrl.Result, reterr error) {
if r.ReconciliationSubject.Status.InstanceState == "Running" {
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Running", "Machine instance is Running...")
r.Log.Info("Machine instance is Running...")
r.Recorder.Event(r.ReconciliationSubject, "Normal", "Running", MachineInstanceRunning)
r.Log.Info(MachineInstanceRunning)
r.ReconciliationSubject.Status.Ready = true
} else if r.ReconciliationSubject.Status.InstanceState == "Error" {
r.Recorder.Event(r.ReconciliationSubject, "Warning", "Error", "CloudStackMachine VM in error state. Deleting associated Machine")
r.Log.Info("CloudStackMachine VM in error state. Deleting associated Machine.", "csMachine", r.ReconciliationSubject.GetName())
r.Recorder.Event(r.ReconciliationSubject, "Warning", "Error", MachineInErrorMessage)
r.Log.Info(MachineInErrorMessage, "csMachine", r.ReconciliationSubject.GetName())
if err := r.K8sClient.Delete(r.RequestCtx, r.CAPIMachine); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: utils.RequeueTimeout}, nil
} else {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", r.ReconciliationSubject.Status.InstanceState, "Instance not ready, is %s", r.ReconciliationSubject.Status.InstanceState)
r.Log.Info(fmt.Sprintf("Instance not ready, is %s.", r.ReconciliationSubject.Status.InstanceState))
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", r.ReconciliationSubject.Status.InstanceState, MachineNotReadyMessage, r.ReconciliationSubject.Status.InstanceState)
r.Log.Info(fmt.Sprintf(MachineNotReadyMessage, r.ReconciliationSubject.Status.InstanceState))
return ctrl.Result{RequeueAfter: utils.RequeueTimeout}, nil
}
return ctrl.Result{}, nil
Expand Down Expand Up @@ -272,10 +285,10 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker()
}

if err := r.K8sClient.Create(r.RequestCtx, csMachineStateChecker); err != nil && !utils.ContainsAlreadyExistsSubstring(err) {
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Machine State Checker", "error encountered when creating CloudStackMachineStateChecker: %s", err.Error())
return r.ReturnWrappedError(err, "error encountered when creating CloudStackMachineStateChecker")
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Machine State Checker", CSMachineStateCheckerCreationFailed)
return r.ReturnWrappedError(err, CSMachineStateCheckerCreationFailed)
}
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Machine State Checker", "CloudStackMachineStateChecker %s created", *checkerName)
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Machine State Checker", CSMachineStateCheckerCreationSuccess)
return r.GetObjectByName(*checkerName, r.StateChecker)()
}

Expand All @@ -290,11 +303,12 @@ func (r *CloudStackMachineReconciliationRunner) ReconcileDelete() (retRes ctrl.R
fmt.Sprintf(" If this VM has already been deleted, please remove the finalizer named %s from object %s",
"cloudstackmachine.infrastructure.cluster.x-k8s.io", r.ReconciliationSubject.Name))
// Cloudstack VM may be not found or more than one found by name
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Deleting", "Deleting CloudStack Machine %s instanceID not found", r.ReconciliationSubject.Name)
r.Recorder.Eventf(r.ReconciliationSubject, "Warning", "Deleting", CSMachineDeletionInstanceIDNotFoundMessage, r.ReconciliationSubject.Name)
r.Log.Error(err, fmt.Sprintf(CSMachineDeletionInstanceIDNotFoundMessage, r.ReconciliationSubject.Name))
return ctrl.Result{}, err
}
}
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Deleting", "Deleting CloudStack Machine %s", r.ReconciliationSubject.Name)
r.Recorder.Eventf(r.ReconciliationSubject, "Normal", "Deleting", CSMachineDeletionMessage, r.ReconciliationSubject.Name)
r.Log.Info("Deleting instance", "instance-id", r.ReconciliationSubject.Spec.InstanceID)
// Use CSClient instead of CSUser here to expunge as admin.
// The CloudStack-Go API does not return an error, but the VM won't delete with Expunge set if requested by
Expand Down
4 changes: 2 additions & 2 deletions controllers/cloudstackmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ var _ = Describe("CloudStackMachineReconciler", func() {
res, err := MachineReconciler.Reconcile(ctx, ctrl.Request{NamespacedName: requestNamespacedName})
Ω(err).ShouldNot(HaveOccurred())
Ω(res.RequeueAfter).Should(BeZero())

Eventually(func() bool {
for event := range fakeRecorder.Events {
return strings.Contains(event, "Normal Created CloudStack instance Created") ||
strings.Contains(event, "Normal Running Machine instance is Running...") ||
strings.Contains(event, "Normal Machine State Checker CloudStackMachineStateChecker Instance1 created")
strings.Contains(event, "Normal Machine State Checker CloudStackMachineStateChecker created")
}
return false
}, timeout).Should(BeTrue())
Expand Down

0 comments on commit 7f1be54

Please sign in to comment.