diff --git a/backup/backup.go b/backup/backup.go index 2645a8ac..15838eb7 100644 --- a/backup/backup.go +++ b/backup/backup.go @@ -47,7 +47,7 @@ func (b *Backup) Unwrap() *v1alhpa1.Backup { // GetNameForJob returns the name of the job func (b *Backup) GetNameForJob() string { - return fmt.Sprintf("%s-backup", b.Name) + return fmt.Sprintf("%s-bak", b.Name) } // Create the backup Domain Name or leader DNS. diff --git a/backup/syncer/job.go b/backup/syncer/job.go index b9250bc3..a4624834 100644 --- a/backup/syncer/job.go +++ b/backup/syncer/job.go @@ -73,6 +73,9 @@ func (s *jobSyncer) SyncFn() error { s.job.Labels = map[string]string{ "Host": s.backup.Spec.HostName, "Type": utils.BackupJobTypeName, + + // Cluster used as selector. + "Cluster": s.backup.Spec.ClusterName, } var backoff int32 = 3 s.job.Spec.Template.Spec = s.ensurePodSpec(s.job.Spec.Template.Spec) diff --git a/controllers/backup_controller.go b/controllers/backup_controller.go index 45685a88..2bc22bdf 100644 --- a/controllers/backup_controller.go +++ b/controllers/backup_controller.go @@ -99,17 +99,17 @@ func (r *BackupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr } // Clear the backup, Just keep historyLimit len - if err = r.clearHistoryJob(ctx, req, *backup.Spec.HistoryLimit); err != nil { + if err = r.clearHistoryJob(ctx, req, *backup.Spec.HistoryLimit, backup.Spec.ClusterName); err != nil { return reconcile.Result{}, err } return ctrl.Result{}, nil } // Clear the History finished Jobs over HistoryLimit. -func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request, historyLimit int32) error { +func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request, historyLimit int32, clusterName string) error { log := log.Log.WithName("controllers").WithName("Backup") backupJobs := batchv1.JobList{} - labelSet := labels.Set{"Type": utils.BackupJobTypeName} + labelSet := labels.Set{"Type": utils.BackupJobTypeName, "Cluster": clusterName} if err := r.List(context.TODO(), &backupJobs, &client.ListOptions{ Namespace: req.Namespace, LabelSelector: labelSet.AsSelector(), }); err != nil { @@ -137,7 +137,7 @@ func (r *BackupReconciler) clearHistoryJob(ctx context.Context, req ctrl.Request // at first check backup status completed. backup := backup.New(&apiv1alpha1.Backup{}) namespacedName := types.NamespacedName{ - Name: strings.TrimSuffix(job.Name, "-backup"), + Name: strings.TrimSuffix(job.Name, "-bak"), Namespace: job.Namespace, } if err := r.Get(context.TODO(), namespacedName, backup.Unwrap()); err != nil {