Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix scheduled update #218

Merged
merged 1 commit into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions controllers/hazelcast/hot_backup_controller.go
Expand Up @@ -128,6 +128,7 @@ func (r *HotBackupReconciler) Reconcile(ctx context.Context, req reconcile.Reque
}
r.cron.Start()
} else {
r.removeSchedule(req.NamespacedName, logger)
err = r.triggerHotBackup(ctx, req, rest, logger)
if err != nil {
_ = r.Client.Get(ctx, req.NamespacedName, hb)
Expand Down Expand Up @@ -259,10 +260,7 @@ func (r *HotBackupReconciler) executeFinalizer(ctx context.Context, hb *hazelcas
Name: hb.Name,
Namespace: hb.Namespace,
}
if jobId, ok := r.scheduled.LoadAndDelete(key); ok {
logger.V(1).Info("Removing cron Job.", "EntryId", jobId)
r.cron.Remove(jobId.(cron.EntryID))
}
r.removeSchedule(key, logger)
if s, ok := r.statuses.LoadAndDelete(key); ok {
logger.V(1).Info("Stopping status ticker for HotBackup.", "CR", key)
s.(*StatusTicker).stop()
Expand All @@ -276,6 +274,13 @@ func (r *HotBackupReconciler) executeFinalizer(ctx context.Context, hb *hazelcas
return nil
}

func (r *HotBackupReconciler) removeSchedule(key types.NamespacedName, logger logr.Logger) {
if jobId, ok := r.scheduled.LoadAndDelete(key); ok {
logger.V(1).Info("Removing cron Job.", "EntryId", jobId)
r.cron.Remove(jobId.(cron.EntryID))
}
}

func (r *HotBackupReconciler) triggerHotBackup(ctx context.Context, req reconcile.Request, rest *RestClient, logger logr.Logger) error {
hb := &hazelcastv1alpha1.HotBackup{}
err := r.Get(ctx, req.NamespacedName, hb)
Expand Down
59 changes: 57 additions & 2 deletions controllers/hazelcast/hot_backup_controller_test.go
Expand Up @@ -49,7 +49,7 @@ func TestHotBackupReconciler_shouldScheduleHotBackupExecution(t *testing.T) {
r := hotBackupReconcilerWithCRs(h, hb)
_, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: n})
if err != nil {
t.Errorf("Error executing Reconcile: %e", err)
t.Errorf("Error executing Reconcile: %v", err)
}
load, _ := r.scheduled.Load(n)
Expect(load).ShouldNot(BeNil())
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestHotBackupReconciler_shouldRemoveScheduledBackup(t *testing.T) {
r := hotBackupReconcilerWithCRs(h, hb)
_, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: n})
if err != nil {
t.Errorf("Error executing Reconcile: %e", err)
t.Errorf("Error executing Reconcile: %v", err)
}

Expect(r.cron.Entries()).Should(BeEmpty())
Expand Down Expand Up @@ -211,6 +211,61 @@ func TestHotBackupReconciler_shouldNotTriggerHotBackupTwice(t *testing.T) {
Expect(hotBackupTriggers).Should(Equal(int32(1)))
}

func TestHotBackupReconciler_shouldUpdateWhenScheduledBackupChangedToInstantBackup(t *testing.T) {
RegisterFailHandler(fail(t))
n := types.NamespacedName{
Name: "hazelcast",
Namespace: "default",
}
h := &hazelcastv1alpha1.Hazelcast{
ObjectMeta: metav1.ObjectMeta{
Name: n.Name,
Namespace: n.Namespace,
},
Status: hazelcastv1alpha1.HazelcastStatus{Phase: hazelcastv1alpha1.Running},
}
hb := &hazelcastv1alpha1.HotBackup{
ObjectMeta: metav1.ObjectMeta{
Name: n.Name,
Namespace: n.Namespace,
},
Spec: hazelcastv1alpha1.HotBackupSpec{
HazelcastResourceName: "hazelcast",
Schedule: "0 23 31 2 *",
},
}
ts, err := fakeHttpServer(hazelcastUrl(h), func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(200)
_, _ = writer.Write([]byte("{\"status\":\"success\"}"))
})
if err != nil {
t.Errorf("Failed to start fake HTTP server: %v", err)
}
defer ts.Close()

r := hotBackupReconcilerWithCRs(h, hb)
_, err = r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: n})
if err != nil {
t.Errorf("Error executing Reconcile: %v", err)
}

Expect(r.cron.Entries()).Should(HaveLen(1))

Expect(r.Client.Get(context.TODO(), n, hb)).Should(Succeed())
hb.Spec.Schedule = ""
Expect(r.Client.Update(context.TODO(), hb)).Should(Succeed())

_, err = r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: n})
if err != nil {
t.Errorf("Error executing Reconcile: %v", err)
}
Expect(r.cron.Entries()).Should(BeEmpty())
r.scheduled.Range(func(key, value interface{}) bool {
t.Errorf("Scheduled map should be empty. But contains key: %v value: %v", key, value)
return false
})
}

func fail(t *testing.T) func(message string, callerSkip ...int) {
return func(message string, callerSkip ...int) {
t.Errorf(message)
Expand Down