Maintenance job should not be launched if the repo already has a running one

Signed-off-by: Ming Qiu <ming.qiu@broadcom.com>
pull/7752/head
Ming Qiu 2024-04-29 03:21:08 +00:00
parent a798182d61
commit e91d9b906c
3 changed files with 23 additions and 7 deletions

View File

@ -238,10 +238,15 @@ func getMaintenanceResultFromJob(cli client.Client, job *batchv1.Job) (string, e
return podList.Items[0].Status.ContainerStatuses[0].State.Terminated.Message, nil
}
func GetLatestMaintenanceJob(cli client.Client, repo string) (*batchv1.Job, error) {
func getLatestMaintenanceJob(cli client.Client, ns string) (*batchv1.Job, error) {
// Get the maintenance job list by label
jobList := &batchv1.JobList{}
err := cli.List(context.TODO(), jobList, client.MatchingLabels(map[string]string{RepositoryNameLabel: repo}))
err := cli.List(context.TODO(), jobList, &client.ListOptions{
Namespace: ns,
},
&client.HasLabels{RepositoryNameLabel},
)
if err != nil {
return nil, err
}
@ -254,5 +259,6 @@ func GetLatestMaintenanceJob(cli client.Client, repo string) (*batchv1.Job, erro
sort.Slice(jobList.Items, func(i, j int) bool {
return jobList.Items[i].CreationTimestamp.Time.After(jobList.Items[j].CreationTimestamp.Time)
})
return &jobList.Items[0], nil
}

View File

@ -257,7 +257,7 @@ func TestGetLatestMaintenanceJob(t *testing.T) {
cli := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
// Call the function
job, err := GetLatestMaintenanceJob(cli, repo)
job, err := getLatestMaintenanceJob(cli, "default")
assert.NoError(t, err)
// We expect the returned job to be the newer job

View File

@ -191,10 +191,6 @@ func (m *manager) PruneRepo(repo *velerov1api.BackupRepository) error {
return errors.WithStack(err)
}
if err := prd.BoostRepoConnect(context.Background(), param); err != nil {
return errors.WithStack(err)
}
log := m.log.WithFields(logrus.Fields{
"BSL name": param.BackupLocation.Name,
"repo type": param.BackupRepo.Spec.RepositoryType,
@ -202,6 +198,20 @@ func (m *manager) PruneRepo(repo *velerov1api.BackupRepository) error {
"repo UID": param.BackupRepo.UID,
})
job, err := getLatestMaintenanceJob(m.client, m.namespace)
if err != nil {
return errors.WithStack(err)
}
if job != nil && job.Status.Succeeded == 0 && job.Status.Failed == 0 {
log.Debugf("There already has a unfinished maintenance job %s/%s for repository %s, please wait for it to complete", job.Namespace, job.Name, param.BackupRepo.Name)
return nil
}
if err := prd.BoostRepoConnect(context.Background(), param); err != nil {
return errors.WithStack(err)
}
log.Info("Start to maintence repo")
maintenanceJob, err := buildMaintenanceJob(m.maintenanceCfg, param, m.client, m.namespace)