From 5cdc1ceb20eb86d061c14e4ae850e92f05a8ae87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wenkai=20Yin=28=E5=B0=B9=E6=96=87=E5=BC=80=29?= Date: Tue, 26 Oct 2021 17:28:32 +0800 Subject: [PATCH] Don't create a backup immediately after creating a schedule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't create a backup immediately after creating a schedule Fixes #1980 Signed-off-by: Wenkai Yin(尹文开) --- changelogs/unreleased/4281-ywk253100 | 1 + pkg/controller/schedule_controller.go | 4 ++-- pkg/controller/schedule_controller_test.go | 9 ++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 changelogs/unreleased/4281-ywk253100 diff --git a/changelogs/unreleased/4281-ywk253100 b/changelogs/unreleased/4281-ywk253100 new file mode 100644 index 0000000000..29ff20281a --- /dev/null +++ b/changelogs/unreleased/4281-ywk253100 @@ -0,0 +1 @@ +Don't create a backup immediately after creating a schedule \ No newline at end of file diff --git a/pkg/controller/schedule_controller.go b/pkg/controller/schedule_controller.go index 1d114494dc..e5f48814f3 100644 --- a/pkg/controller/schedule_controller.go +++ b/pkg/controller/schedule_controller.go @@ -276,11 +276,11 @@ func (c *scheduleController) submitBackupIfDue(item *api.Schedule, cronSchedule } func getNextRunTime(schedule *api.Schedule, cronSchedule cron.Schedule, asOf time.Time) (bool, time.Time) { - // get the latest run time (if the schedule hasn't run yet, this will be the zero value which will trigger - // an immediate backup) var lastBackupTime time.Time if schedule.Status.LastBackup != nil { lastBackupTime = schedule.Status.LastBackup.Time + } else { + lastBackupTime = schedule.CreationTimestamp.Time } nextRunTime := cronSchedule.Next(lastBackupTime) diff --git a/pkg/controller/schedule_controller_test.go b/pkg/controller/schedule_controller_test.go index 2dfff7c064..5900e673e9 100644 --- a/pkg/controller/schedule_controller_test.go +++ b/pkg/controller/schedule_controller_test.go @@ -274,7 +274,7 @@ func TestGetNextRunTime(t *testing.T) { { name: "first run", schedule: defaultSchedule(), - expectedDue: true, + expectedDue: false, expectedNextRunTimeOffset: "5m", }, { @@ -319,6 +319,9 @@ func TestGetNextRunTime(t *testing.T) { require.NoError(t, err, "unable to parse test.lastRanOffset: %v", err) test.schedule.Status.LastBackup = &metav1.Time{Time: testClock.Now().Add(-offsetDuration)} + test.schedule.CreationTimestamp = *test.schedule.Status.LastBackup + } else { + test.schedule.CreationTimestamp = metav1.Time{Time: testClock.Now()} } nextRunTimeOffset, err := time.ParseDuration(test.expectedNextRunTimeOffset) @@ -326,11 +329,11 @@ func TestGetNextRunTime(t *testing.T) { panic(err) } - // calculate expected next run time (if the schedule hasn't run yet, this - // will be the zero value which will trigger an immediate backup) var baseTime time.Time if test.lastRanOffset != "" { baseTime = test.schedule.Status.LastBackup.Time + } else { + baseTime = test.schedule.CreationTimestamp.Time } expectedNextRunTime := baseTime.Add(nextRunTimeOffset)