Skip to content

Commit 013e525

Browse files
hossainemruztamalsaha
authored andcommitted
Suspend CronJob but keep instant backup working while backup is paused (#1024)
1 parent 64f75bb commit 013e525

File tree

8 files changed

+426
-56
lines changed

8 files changed

+426
-56
lines changed

apis/stash/v1beta1/backup_session_types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ const (
5757
BackupSessionRunning BackupSessionPhase = "Running"
5858
BackupSessionSucceeded BackupSessionPhase = "Succeeded"
5959
BackupSessionFailed BackupSessionPhase = "Failed"
60-
BackupSessionSkipped BackupSessionPhase = "Skipped"
6160
BackupSessionUnknown BackupSessionPhase = "Unknown"
6261
)
6362

pkg/backup/backupsession.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ func (c *BackupSessionController) processBackupSession(key string) error {
207207
}
208208

209209
func (c *BackupSessionController) startBackupProcess(backupSession *api_v1beta1.BackupSession, invoker apis.Invoker, targetInfo apis.TargetInfo) (*restic.BackupOutput, error) {
210-
// skip if backup invoker is paused
211-
if invoker.Paused {
212-
log.Infof("Skipping processing BackupSession %s/%s. Reason: Backup invoker is paused.", backupSession.Namespace, backupSession.Name)
213-
return nil, nil
214-
}
215210

216211
// if BackupSession already has been processed for this host then skip further processing
217212
if c.isBackupTakenForThisHost(backupSession, targetInfo.Target) {
@@ -509,8 +504,7 @@ func (c *BackupSessionController) isBackupTakenForThisHost(backupSession *api_v1
509504

510505
// if overall backupSession phase is "Succeeded" or "Failed" or "Skipped" then it has been processed already
511506
if backupSession.Status.Phase == api_v1beta1.BackupSessionSucceeded ||
512-
backupSession.Status.Phase == api_v1beta1.BackupSessionFailed ||
513-
backupSession.Status.Phase == api_v1beta1.BackupSessionSkipped {
507+
backupSession.Status.Phase == api_v1beta1.BackupSessionFailed {
514508
return true
515509
}
516510

pkg/cmds/create_backupsession.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,6 @@ func (opt *options) createBackupSession() error {
106106
OwnerReferences: []metav1.OwnerReference{},
107107
}
108108

109-
// skip if backup invoker paused
110-
if invoker.Paused {
111-
msg := fmt.Sprintf("Skipping creating BackupSession. Reason: Backup invoker %s/%s is paused.", opt.namespace, invoker.ObjectMeta.Name)
112-
log.Infoln(msg)
113-
114-
// write event to backup invoker denoting that backup session has been skipped
115-
return writeBackupSessionSkippedEvent(opt.k8sClient, invoker.ObjectRef, msg)
116-
}
117-
118109
wc := util.WorkloadClients{
119110
KubeClient: opt.k8sClient,
120111
StashClient: opt.stashClient,

pkg/controller/backup_configuration.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"stash.appscode.dev/stash/pkg/util"
3030

3131
"github.com/appscode/go/log"
32+
"github.com/appscode/go/types"
3233
"github.com/golang/glog"
3334
batch_v1beta1 "k8s.io/api/batch/v1beta1"
3435
core "k8s.io/api/core/v1"
@@ -111,12 +112,6 @@ func (c *StashController) applyBackupInvokerReconciliationLogic(invoker apis.Inv
111112
if err != nil {
112113
return err
113114
}
114-
// skip if BackupBatch paused
115-
if invoker.Paused {
116-
log.Infof("Skipping processing for invoker %s %s/%s. Reason: The invoker is paused.", invoker.ObjectRef.Kind, invoker.ObjectMeta.Namespace, invoker.ObjectMeta.Name)
117-
return nil
118-
}
119-
120115
for _, targetInfo := range invoker.TargetsInfo {
121116
if targetInfo.Target != nil && invoker.Driver != api_v1beta1.VolumeSnapshotter &&
122117
util.BackupModel(targetInfo.Target.Ref.Kind) == apis.ModelSidecar {
@@ -255,6 +250,7 @@ func (c *StashController) EnsureBackupTriggeringCronJob(invoker apis.Invoker) er
255250
core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)
256251

257252
in.Spec.Schedule = invoker.Schedule
253+
in.Spec.Suspend = types.BoolP(invoker.Paused) // this ensure that the CronJob is suspended when the backup invoker is paused.
258254
in.Spec.JobTemplate.Labels = invoker.Labels
259255
// ensure that job gets deleted on completion
260256
in.Spec.JobTemplate.Labels[apis.KeyDeleteJobOnCompletion] = apis.AllowDeletingJobOnCompletion

pkg/controller/backup_session.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,6 @@ func (c *StashController) applyBackupSessionReconciliationLogic(backupSession *a
152152
} else if phase == api_v1beta1.BackupSessionRunning {
153153
log.Infof("Skipping processing BackupSession %s/%s. Reason: phase is %q.", backupSession.Namespace, backupSession.Name, backupSession.Status.Phase)
154154
return nil
155-
} else if phase == api_v1beta1.BackupSessionSkipped {
156-
log.Infof("Skipping processing BackupSession %s/%s. Reason: phase is %q.", backupSession.Namespace, backupSession.Name, backupSession.Status.Phase)
157-
return nil
158-
}
159-
160-
// skip if backup invoker is paused
161-
if invoker.Paused {
162-
log.Infof("Skipping processing BackupSession %s/%s. Reason: %s is paused.", backupSession.Namespace, backupSession.Name, invoker.ObjectRef.Kind)
163-
return c.setBackupSessionSkipped(backupSession, fmt.Sprintf("backup invoker %s %s/%s is paused", invoker.ObjectRef.Kind, invoker.ObjectRef.Namespace, invoker.ObjectRef.Name))
164155
}
165156

166157
// if preBackup hook exist, then execute preBackupHook
@@ -427,29 +418,6 @@ func (c *StashController) setBackupSessionFailed(invoker apis.Invoker, backupSes
427418
return errors.NewAggregate([]error{backupErr, err})
428419
}
429420

430-
func (c *StashController) setBackupSessionSkipped(backupSession *api_v1beta1.BackupSession, reason string) error {
431-
// set BackupSession phase to "Skipped"
432-
_, err := stash_util.UpdateBackupSessionStatus(c.stashClient.StashV1beta1(), backupSession, func(in *api_v1beta1.BackupSessionStatus) *api_v1beta1.BackupSessionStatus {
433-
in.Phase = api_v1beta1.BackupSessionSkipped
434-
in.Targets = backupSession.Status.Targets
435-
return in
436-
})
437-
if err != nil {
438-
return err
439-
}
440-
441-
// write skip event
442-
_, err = eventer.CreateEvent(
443-
c.kubeClient,
444-
eventer.EventSourceBackupSessionController,
445-
backupSession,
446-
core.EventTypeWarning,
447-
eventer.EventReasonBackupSessionSkipped,
448-
reason,
449-
)
450-
return err
451-
}
452-
453421
func (c *StashController) setTargetPhaseRunning(target *api_v1beta1.BackupTarget, driver api_v1beta1.Snapshotter, backupSession *api_v1beta1.BackupSession) (*api_v1beta1.BackupSession, error) {
454422
// find out the total number of hosts in target that will be backed up in this backup session
455423
totalHosts, err := c.getTotalHosts(target, backupSession.Namespace, driver)

test/e2e/framework/backup_configuration.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,34 @@ func (f *Framework) EventuallyCronJobCreated(meta metav1.ObjectMeta) GomegaAsync
8787
)
8888
}
8989

90+
func (f *Framework) EventuallyCronJobSuspended(meta metav1.ObjectMeta) GomegaAsyncAssertion {
91+
return Eventually(
92+
func() bool {
93+
cronJob, err := f.KubeClient.BatchV1beta1().CronJobs(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
94+
if err != nil {
95+
return false
96+
}
97+
return *cronJob.Spec.Suspend
98+
},
99+
time.Minute*2,
100+
time.Second*5,
101+
)
102+
}
103+
104+
func (f *Framework) EventuallyCronJobResumed(meta metav1.ObjectMeta) GomegaAsyncAssertion {
105+
return Eventually(
106+
func() bool {
107+
cronJob, err := f.KubeClient.BatchV1beta1().CronJobs(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
108+
if err != nil {
109+
return false
110+
}
111+
return !*cronJob.Spec.Suspend
112+
},
113+
time.Minute*2,
114+
time.Second*5,
115+
)
116+
}
117+
90118
func (f *Framework) EventuallyBackupConfigurationCreated(meta metav1.ObjectMeta) GomegaAsyncAssertion {
91119
return Eventually(
92120
func() bool {

test/e2e/framework/backup_session.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package framework
1919
import (
2020
"fmt"
2121
"strings"
22+
"time"
2223

2324
"stash.appscode.dev/stash/apis"
2425
"stash.appscode.dev/stash/apis/stash/v1beta1"
@@ -47,7 +48,6 @@ func (f *Framework) EventuallyBackupProcessCompleted(meta metav1.ObjectMeta) Gom
4748
}
4849
if bs.Status.Phase == v1beta1.BackupSessionSucceeded ||
4950
bs.Status.Phase == v1beta1.BackupSessionFailed ||
50-
bs.Status.Phase == v1beta1.BackupSessionSkipped ||
5151
bs.Status.Phase == v1beta1.BackupSessionUnknown {
5252
return true
5353
}
@@ -103,3 +103,47 @@ func (fi *Invocation) TriggerInstantBackup(objMeta metav1.ObjectMeta, invokerRef
103103

104104
return fi.StashClient.StashV1beta1().BackupSessions(backupSession.Namespace).Create(backupSession)
105105
}
106+
107+
func (fi *Invocation) EventuallyBackupCount(invokerMeta metav1.ObjectMeta, invokerKind string) GomegaAsyncAssertion {
108+
return Eventually(func() int64 {
109+
count, err := fi.GetSuccessfulBackupSessionCount(invokerMeta, invokerKind)
110+
if err != nil {
111+
return 0
112+
}
113+
return count
114+
}, 3*time.Minute, 5*time.Second)
115+
}
116+
117+
func (fi *Invocation) GetSuccessfulBackupSessionCount(invokerMeta metav1.ObjectMeta, invokerKind string) (int64, error) {
118+
backupSessions, err := fi.StashClient.StashV1beta1().BackupSessions(fi.namespace).List(metav1.ListOptions{})
119+
if err != nil {
120+
return 0, err
121+
}
122+
123+
count := int64(0)
124+
for _, bs := range backupSessions.Items {
125+
if bs.Spec.Invoker.Kind == invokerKind &&
126+
bs.Spec.Invoker.Name == invokerMeta.Name &&
127+
bs.Status.Phase == v1beta1.BackupSessionSucceeded {
128+
count++
129+
}
130+
}
131+
return count, nil
132+
}
133+
134+
func (fi *Invocation) EventuallyRunningBackupCompleted(invokerMeta metav1.ObjectMeta, invokerKind string) GomegaAsyncAssertion {
135+
return Eventually(func() bool {
136+
backupSessions, err := fi.StashClient.StashV1beta1().BackupSessions(fi.namespace).List(metav1.ListOptions{})
137+
if err != nil {
138+
return false
139+
}
140+
for _, bs := range backupSessions.Items {
141+
if bs.Spec.Invoker.Kind == invokerKind &&
142+
bs.Spec.Invoker.Name == invokerMeta.Name &&
143+
bs.Status.Phase == v1beta1.BackupSessionRunning {
144+
return false
145+
}
146+
}
147+
return true
148+
}, 3*time.Minute, 5*time.Second)
149+
}

0 commit comments

Comments
 (0)