Skip to content

Commit 0a4b2ab

Browse files
authored
Merge pull request #239 from acekingke/fix_bug_backupCopies
controller: fix the 8 backupJob but create 11 copies #237
2 parents 8f02da4 + a0dfa1e commit 0a4b2ab

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

controllers/backup_controller.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"reflect"
2323
"sort"
24+
"strings"
2425

2526
"github.com/presslabs/controller-util/syncer"
2627
batchv1 "k8s.io/api/batch/v1"
@@ -29,6 +30,7 @@ import (
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031
"k8s.io/apimachinery/pkg/labels"
3132
"k8s.io/apimachinery/pkg/runtime"
33+
"k8s.io/apimachinery/pkg/types"
3234
"k8s.io/client-go/tools/record"
3335
ctrl "sigs.k8s.io/controller-runtime"
3436
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -100,16 +102,16 @@ func (r *BackupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
100102
// Clear the History finished Jobs over HistoryLimit.
101103
func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request, historyLimit int32) error {
102104
log := log.Log.WithName("controllers").WithName("Backup")
103-
backups := batchv1.JobList{}
105+
backupJobs := batchv1.JobList{}
104106
labelSet := labels.Set{"Type": utils.BackupJobTypeName}
105-
if err := r.List(context.TODO(), &backups, &client.ListOptions{
107+
if err := r.List(context.TODO(), &backupJobs, &client.ListOptions{
106108
Namespace: req.Namespace, LabelSelector: labelSet.AsSelector(),
107109
}); err != nil {
108110
return err
109111
}
110112

111113
var finishedBackups []*batchv1.Job
112-
for _, job := range backups.Items {
114+
for _, job := range backupJobs.Items {
113115
if IsJobFinished(&job) {
114116
finishedBackups = append(finishedBackups, &job)
115117
}
@@ -126,11 +128,24 @@ func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request
126128
if int32(i) >= int32(len(finishedBackups))-historyLimit {
127129
break
128130
}
129-
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {
130-
log.Error(err, "unable to delete old completed job", "job", job)
131-
} else {
132-
log.V(0).Info("deleted old completed job", "job", job)
131+
// at first check backup status completed.
132+
backup := backup.New(&apiv1alpha1.Backup{})
133+
namespacedName := types.NamespacedName{
134+
Name: strings.TrimSuffix(job.Name, "-backup"),
135+
Namespace: job.Namespace,
133136
}
137+
if err := r.Get(context.TODO(), namespacedName, backup.Unwrap()); err != nil {
138+
log.Error(err, "can not find the backup", "jobName", job.Name)
139+
break
140+
}
141+
if backup.Status.Completed {
142+
if err := r.Delete(ctx, job, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {
143+
log.Error(err, "unable to delete old completed job", "job", job)
144+
} else {
145+
log.V(0).Info("deleted old completed job", "job", job)
146+
}
147+
}
148+
134149
}
135150
return nil
136151
}

0 commit comments

Comments
 (0)