From 790f254e06ab03af3d0c313e7e1d1e5e016ac82f Mon Sep 17 00:00:00 2001 From: Carlo Lobrano Date: Fri, 9 Feb 2024 12:27:59 +0100 Subject: [PATCH] Use medik8s/common API for remediation cannot start event Signed-off-by: Carlo Lobrano --- .../machinedeletionremediation_controller.go | 36 +++++++++--------- ...hinedeletionremediation_controller_test.go | 38 +++++++++---------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/controllers/machinedeletionremediation_controller.go b/controllers/machinedeletionremediation_controller.go index 2d6430b6..e9f218e9 100644 --- a/controllers/machinedeletionremediation_controller.go +++ b/controllers/machinedeletionremediation_controller.go @@ -59,8 +59,8 @@ const ( noMachineAnnotationError = "failed to find openshift machine annotation on node name: %s" invalidValueMachineAnnotationError = "failed to extract Machine Name and Machine Namespace from machine annotation on the node for node name: %s" failedToDeleteMachineError = "failed to delete machine of node name: %s" - nodeNotFoundErrorMsg = "failed to fetch node" - machineNotFoundErrorMsg = "failed to fetch machine of node" + nodeNotFoundErrorMsg = "could not get the node" + machineNotFoundErrorMsg = "could not get node's machine" noControllerOwnerErrorMsg = "ignoring remediation of the machine: the machine has no controller owner" // Cluster Provider messages machineDeletedOnCloudProviderMessage = "Machine will be deleted and the unhealthy node replaced. This is a Cloud cluster provider: the new node is expected to have a new name" @@ -71,13 +71,13 @@ const ( type conditionChangeReason string const ( - remediationStarted conditionChangeReason = "RemediationStarted" - remediationTimedOutByNhc conditionChangeReason = "RemediationStoppedByNHC" - remediationFinishedMachineDeleted conditionChangeReason = "MachineDeleted" - remediationSkippedNodeNotFound conditionChangeReason = "RemediationSkippedNodeNotFound" - remediationSkippedMachineNotFound conditionChangeReason = "RemediationSkippedMachineNotFound" - remediationSkippedNoControllerOwner conditionChangeReason = "RemediationSkippedNoControllerOwner" - remediationFailed conditionChangeReason = "RemediationFailed" + remediationStarted conditionChangeReason = "RemediationStarted" + remediationTimedOutByNhc conditionChangeReason = "RemediationStoppedByNHC" + remediationFinishedMachineDeleted conditionChangeReason = "MachineDeleted" + remediationCannotStartNodeNotFound conditionChangeReason = "RemediationCannotStartNodeNotFound" + remediationCannotStartMachineNotFound conditionChangeReason = "RemediationCannotStartMachineNotFound" + remediationCannotStartNoControllerOwner conditionChangeReason = "RemediationCannotStartNoControllerOwner" + remediationFailed conditionChangeReason = "RemediationFailed" ) var ( @@ -156,11 +156,11 @@ func (r *MachineDeletionRemediationReconciler) Reconcile(ctx context.Context, re // situation. An error is returned only if it does not match the following custom errors, or // updateConditions fails. if err == nodeNotFoundError { - commonevents.WarningEvent(r.Recorder, mdr, string(remediationSkippedNodeNotFound), nodeNotFoundErrorMsg) - _, err = r.updateConditions(remediationSkippedNodeNotFound, mdr) + commonevents.GetTargetNodeFailed(r.Recorder, mdr) + _, err = r.updateConditions(remediationCannotStartNodeNotFound, mdr) } else if err == machineNotFoundError { - commonevents.WarningEvent(r.Recorder, mdr, string(remediationSkippedMachineNotFound), machineNotFoundErrorMsg) - _, err = r.updateConditions(remediationSkippedMachineNotFound, mdr) + commonevents.WarningEvent(r.Recorder, mdr, string(remediationCannotStartMachineNotFound), machineNotFoundErrorMsg) + _, err = r.updateConditions(remediationCannotStartMachineNotFound, mdr) } else if err == unrecoverableError { commonevents.WarningEvent(r.Recorder, mdr, string(remediationFailed), unrecoverableError.Error()) _, err = r.updateConditions(remediationFailed, mdr) @@ -233,8 +233,8 @@ func (r *MachineDeletionRemediationReconciler) Reconcile(ctx context.Context, re if !hasControllerOwner(machine) { log.Info(noControllerOwnerErrorMsg, "machine", machine.GetName(), "remediation name", mdr.Name) - commonevents.WarningEvent(r.Recorder, mdr, string(remediationSkippedNoControllerOwner), noControllerOwnerErrorMsg) - _, err = r.updateConditions(remediationSkippedNoControllerOwner, mdr) + commonevents.WarningEvent(r.Recorder, mdr, string(remediationCannotStartNoControllerOwner), noControllerOwnerErrorMsg) + _, err = r.updateConditions(remediationCannotStartNoControllerOwner, mdr) return ctrl.Result{}, err } @@ -418,9 +418,9 @@ func (r *MachineDeletionRemediationReconciler) updateConditions(reason condition processingConditionStatus = metav1.ConditionFalse succeededConditionStatus = metav1.ConditionTrue case remediationTimedOutByNhc, - remediationSkippedNoControllerOwner, - remediationSkippedNodeNotFound, - remediationSkippedMachineNotFound, + remediationCannotStartNoControllerOwner, + remediationCannotStartNodeNotFound, + remediationCannotStartMachineNotFound, remediationFailed: processingConditionStatus = metav1.ConditionFalse succeededConditionStatus = metav1.ConditionFalse diff --git a/controllers/machinedeletionremediation_controller_test.go b/controllers/machinedeletionremediation_controller_test.go index 66fd8600..24d92506 100644 --- a/controllers/machinedeletionremediation_controller_test.go +++ b/controllers/machinedeletionremediation_controller_test.go @@ -143,11 +143,11 @@ var _ = Describe("Machine Deletion Remediation CR", func() { verifyMachineNotDeleted(workerNodeMachineName) verifyMachineNotDeleted(masterNodeMachineName) verifyConditionsMatch([]expectedCondition{ - {commonconditions.ProcessingType, metav1.ConditionFalse, remediationSkippedNodeNotFound}, - {commonconditions.SucceededType, metav1.ConditionFalse, remediationSkippedNodeNotFound}}) + {commonconditions.ProcessingType, metav1.ConditionFalse, remediationCannotStartNodeNotFound}, + {commonconditions.SucceededType, metav1.ConditionFalse, remediationCannotStartNodeNotFound}}) verifyConditionUnset(commonconditions.PermanentNodeDeletionExpectedType) verifyEvents([]expectedEvent{ - {v1.EventTypeWarning, "RemediationSkippedNodeNotFound", "failed to fetch node", true}, + {v1.EventTypeWarning, "RemediationCannotStart", "Could not get remediation target Node", true}, {v1.EventTypeNormal, "RemediationStarted", "Remediation started", false}, }) }) @@ -162,12 +162,12 @@ var _ = Describe("Machine Deletion Remediation CR", func() { verifyMachineNotDeleted(workerNodeMachineName) verifyMachineNotDeleted(masterNodeMachineName) verifyConditionsMatch([]expectedCondition{ - {commonconditions.ProcessingType, metav1.ConditionFalse, remediationSkippedNoControllerOwner}, - {commonconditions.SucceededType, metav1.ConditionFalse, remediationSkippedNoControllerOwner}, + {commonconditions.ProcessingType, metav1.ConditionFalse, remediationCannotStartNoControllerOwner}, + {commonconditions.SucceededType, metav1.ConditionFalse, remediationCannotStartNoControllerOwner}, // Cluster provider is not set in this test {commonconditions.PermanentNodeDeletionExpectedType, metav1.ConditionUnknown, v1alpha1.MachineDeletionOnUndefinedProviderReason}}) verifyEvents([]expectedEvent{ - {v1.EventTypeWarning, "RemediationSkippedNoControllerOwner", noControllerOwnerErrorMsg, true}, + {v1.EventTypeWarning, string(remediationCannotStartNoControllerOwner), noControllerOwnerErrorMsg, true}, {v1.EventTypeNormal, "RemediationStarted", "Remediation started", false}, }) }) @@ -184,12 +184,12 @@ var _ = Describe("Machine Deletion Remediation CR", func() { verifyMachineNotDeleted(workerNodeMachineName) verifyMachineNotDeleted(masterNodeMachineName) verifyConditionsMatch([]expectedCondition{ - {commonconditions.ProcessingType, metav1.ConditionFalse, remediationSkippedNoControllerOwner}, - {commonconditions.SucceededType, metav1.ConditionFalse, remediationSkippedNoControllerOwner}, + {commonconditions.ProcessingType, metav1.ConditionFalse, remediationCannotStartNoControllerOwner}, + {commonconditions.SucceededType, metav1.ConditionFalse, remediationCannotStartNoControllerOwner}, // Cluster provider is not set in this test {commonconditions.PermanentNodeDeletionExpectedType, metav1.ConditionUnknown, v1alpha1.MachineDeletionOnUndefinedProviderReason}}) verifyEvents([]expectedEvent{ - {v1.EventTypeWarning, "RemediationSkippedNoControllerOwner", noControllerOwnerErrorMsg, true}, + {v1.EventTypeWarning, "RemediationCannotStartNoControllerOwner", noControllerOwnerErrorMsg, true}, {v1.EventTypeNormal, "RemediationStarted", "Remediation started", false}, }) }) @@ -207,13 +207,13 @@ var _ = Describe("Machine Deletion Remediation CR", func() { verifyMachineNotDeleted(workerNodeMachineName) verifyMachineNotDeleted(masterNodeMachineName) verifyConditionsMatch([]expectedCondition{ - {commonconditions.ProcessingType, metav1.ConditionFalse, remediationSkippedNoControllerOwner}, - {commonconditions.SucceededType, metav1.ConditionFalse, remediationSkippedNoControllerOwner}, + {commonconditions.ProcessingType, metav1.ConditionFalse, remediationCannotStartNoControllerOwner}, + {commonconditions.SucceededType, metav1.ConditionFalse, remediationCannotStartNoControllerOwner}, // Cluster provider is not set in this test {commonconditions.PermanentNodeDeletionExpectedType, metav1.ConditionUnknown, v1alpha1.MachineDeletionOnUndefinedProviderReason}}) verifyEvents([]expectedEvent{ - {v1.EventTypeWarning, "RemediationSkippedNoControllerOwner", noControllerOwnerErrorMsg, true}, + {v1.EventTypeWarning, "RemediationCannotStartNoControllerOwner", noControllerOwnerErrorMsg, true}, {v1.EventTypeNormal, "RemediationStarted", "Remediation started", false}, }) }) @@ -366,11 +366,11 @@ var _ = Describe("Machine Deletion Remediation CR", func() { }, 30*time.Second, 1*time.Second).Should(BeTrue()) verifyConditionsMatch([]expectedCondition{ - {commonconditions.ProcessingType, metav1.ConditionFalse, remediationSkippedNodeNotFound}, - {commonconditions.SucceededType, metav1.ConditionFalse, remediationSkippedNodeNotFound}}) + {commonconditions.ProcessingType, metav1.ConditionFalse, remediationCannotStartNodeNotFound}, + {commonconditions.SucceededType, metav1.ConditionFalse, remediationCannotStartNodeNotFound}}) verifyConditionUnset(commonconditions.PermanentNodeDeletionExpectedType) verifyEvents([]expectedEvent{ - {v1.EventTypeWarning, "RemediationSkippedNodeNotFound", "failed to fetch node", true}, + {v1.EventTypeWarning, "RemediationCannotStart", "Could not get remediation target Node", true}, {v1.EventTypeNormal, "RemediationStarted", "Remediation started", false}, }) }) @@ -452,17 +452,17 @@ var _ = Describe("Machine Deletion Remediation CR", func() { Expect(k8sClient.Update(context.Background(), masterNode)).ToNot(HaveOccurred()) }) - It("failed to fetch machine error", func() { + It("failed to get machine error", func() { Eventually(func() bool { return plogs.Contains(machineNotFoundErrorMsg) }, 30*time.Second, 1*time.Second).Should(BeTrue()) verifyConditionsMatch([]expectedCondition{ - {commonconditions.ProcessingType, metav1.ConditionFalse, remediationSkippedMachineNotFound}, - {commonconditions.SucceededType, metav1.ConditionFalse, remediationSkippedMachineNotFound}}) + {commonconditions.ProcessingType, metav1.ConditionFalse, remediationCannotStartMachineNotFound}, + {commonconditions.SucceededType, metav1.ConditionFalse, remediationCannotStartMachineNotFound}}) verifyConditionUnset(commonconditions.PermanentNodeDeletionExpectedType) verifyEvents([]expectedEvent{ - {v1.EventTypeWarning, "RemediationSkippedMachineNotFound", "failed to fetch machine of node", true}, + {v1.EventTypeWarning, "RemediationCannotStartMachineNotFound", "could not get node's machine", true}, {v1.EventTypeNormal, "RemediationStarted", "Remediation started", false}, }) })