Skip to content

Commit

Permalink
Refactor cleanupFar and check CR deletion event
Browse files Browse the repository at this point in the history
Seek for FAR CR deletion event only when there isn't an API 'is not found' error'
  • Loading branch information
razo7 committed Jan 24, 2024
1 parent 89db554 commit bfa13bf
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions controllers/fenceagentsremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ var _ = Describe("FAR Controller", func() {
DeferCleanup(k8sClient.Delete, context.Background(), node)

Expect(k8sClient.Create(context.Background(), underTestFAR)).To(Succeed())
DeferCleanup(cleanupFar(), context.Background(), underTestFAR)
DeferCleanup(func() {
Expect(cleanupFar(context.Background(), underTestFAR)).To(Succeed())
deleteErr := k8sClient.Get(ctx, client.ObjectKeyFromObject(underTestFAR), &v1alpha1.FenceAgentsRemediation{})
if !apierrors.IsNotFound(deleteErr) {
verifyEvent(corev1.EventTypeNormal, utils.EventReasonRemoveRemediationCR, utils.EventMessageRemoveRemediationCR)
}
clearEvents()
})

// Sleep for a second to ensure dummy reconciliation has begun running before the unit tests
time.Sleep(1 * time.Second)
Expand Down Expand Up @@ -564,35 +571,31 @@ func verifyRemediationConditions(far *v1alpha1.FenceAgentsRemediation, nodeName
}

// cleanupFar deletes the FAR CR and waits until it is deleted. The function ignores if the CR is already deleted.
func cleanupFar() func(ctx context.Context, far *v1alpha1.FenceAgentsRemediation) error {
return func(ctx context.Context, far *v1alpha1.FenceAgentsRemediation) error {
cr := &v1alpha1.FenceAgentsRemediation{}
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
func cleanupFar(ctx context.Context, far *v1alpha1.FenceAgentsRemediation) error {
cr := &v1alpha1.FenceAgentsRemediation{}
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}

var force client.GracePeriodSeconds = 0
if err := k8sClient.Delete(ctx, cr, force); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
var force client.GracePeriodSeconds = 0
if err := k8sClient.Delete(ctx, cr, force); err != nil {
if apierrors.IsNotFound(err) {
return nil
}

ConsistentlyWithOffset(1, func() error {
deleteErr := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr)
if apierrors.IsNotFound(deleteErr) {
// when trying to create far CR with invalid name
log.Info("Cleanup: Got error 404", "name", cr.Name)
return nil
}
return deleteErr
}, pollInterval, timeoutPostRemediation).Should(BeNil(), "CR should be deleted")
verifyEvent(corev1.EventTypeNormal, utils.EventReasonRemoveRemediationCR, utils.EventMessageRemoveRemediationCR)
clearEvents()
return nil
return err
}

ConsistentlyWithOffset(1, func() error {
deleteErr := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr)
if apierrors.IsNotFound(deleteErr) {
// when trying to create far CR with invalid name
log.Info("Cleanup: Got error 404", "name", cr.Name)
return nil
}
return deleteErr
}, pollInterval, timeoutPostRemediation).Should(BeNil(), "CR should be deleted")
return nil
}

0 comments on commit bfa13bf

Please sign in to comment.