From d49a1ada030356e11eed95e59cbee1e55618f3e8 Mon Sep 17 00:00:00 2001 From: acekingke Date: Wed, 29 Jun 2022 17:12:30 +0800 Subject: [PATCH 1/2] job,controllers: clear history job must do in cluster, not in namespace #559 --- backup/syncer/job.go | 3 +++ controllers/backup_controller.go | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) 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..75839ea0 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 { From abd4180606e8747b38f88d6d426c1a653cf2c7a6 Mon Sep 17 00:00:00 2001 From: acekingke Date: Thu, 30 Jun 2022 15:03:28 +0800 Subject: [PATCH 2/2] backup,controllers: shorten the backup name --- backup/backup.go | 2 +- controllers/backup_controller.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/controllers/backup_controller.go b/controllers/backup_controller.go index 75839ea0..2bc22bdf 100644 --- a/controllers/backup_controller.go +++ b/controllers/backup_controller.go @@ -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 {