Skip to content

Commit

Permalink
Merge pull request radondb#239 from acekingke/fix_bug_backupCopies
Browse files Browse the repository at this point in the history
controller: fix the 8 backupJob but create 11 copies radondb#237
  • Loading branch information
andyli029 authored Sep 28, 2021
2 parents 2553ff0 + c34ae43 commit a7b5195
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions controllers/backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"reflect"
"sort"
"strings"

"github.com/presslabs/controller-util/syncer"
batchv1 "k8s.io/api/batch/v1"
Expand All @@ -29,6 +30,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -100,16 +102,16 @@ func (r *BackupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
// Clear the History finished Jobs over HistoryLimit.
func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request, historyLimit int32) error {
log := log.Log.WithName("controllers").WithName("Backup")
backups := batchv1.JobList{}
backupJobs := batchv1.JobList{}
labelSet := labels.Set{"Type": utils.BackupJobTypeName}
if err := r.List(context.TODO(), &backups, &client.ListOptions{
if err := r.List(context.TODO(), &backupJobs, &client.ListOptions{
Namespace: req.Namespace, LabelSelector: labelSet.AsSelector(),
}); err != nil {
return err
}

var finishedBackups []*batchv1.Job
for _, job := range backups.Items {
for _, job := range backupJobs.Items {
if IsJobFinished(&job) {
finishedBackups = append(finishedBackups, &job)
}
Expand All @@ -126,11 +128,24 @@ func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request
if int32(i) >= int32(len(finishedBackups))-historyLimit {
break
}
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {
log.Error(err, "unable to delete old completed job", "job", job)
} else {
log.V(0).Info("deleted old completed job", "job", job)
// at first check backup status completed.
backup := backup.New(&apiv1alpha1.Backup{})
namespacedName := types.NamespacedName{
Name: strings.TrimSuffix(job.Name, "-backup"),
Namespace: job.Namespace,
}
if err := r.Get(context.TODO(), namespacedName, backup.Unwrap()); err != nil {
log.Error(err, "can not find the backup", "jobName", job.Name)
break
}
if backup.Status.Completed {
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {
log.Error(err, "unable to delete old completed job", "job", job)
} else {
log.V(0).Info("deleted old completed job", "job", job)
}
}

}
return nil
}
Expand Down

0 comments on commit a7b5195

Please sign in to comment.