Skip to content

Commit

Permalink
Merge pull request #7752 from qiuming-best/maintenance-job-start-fix
Browse files Browse the repository at this point in the history
Maintenance job should not be launched if the repo already has a runn…
  • Loading branch information
qiuming-best authored May 7, 2024
2 parents dda6c1f + e91d9b9 commit 3cbf2eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 8 additions & 2 deletions pkg/repository/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/repository/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions pkg/repository/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,27 @@ 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,
"repo name": param.BackupRepo.Name,
"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)
Expand Down

0 comments on commit 3cbf2eb

Please sign in to comment.