From a8ba4875f09ec1ccd279d42886d53790a3d90fdc Mon Sep 17 00:00:00 2001 From: allenxu404 Date: Wed, 24 Aug 2022 19:06:17 +0800 Subject: [PATCH] equip gc controller with configurable frequency Signed-off-by: allenxu404 --- changelogs/unreleased/5248-allenxu404 | 1 + pkg/cmd/server/server.go | 2 +- pkg/controller/gc_controller.go | 25 ++++++++++++++++--------- pkg/controller/gc_controller_test.go | 5 +++-- 4 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 changelogs/unreleased/5248-allenxu404 diff --git a/changelogs/unreleased/5248-allenxu404 b/changelogs/unreleased/5248-allenxu404 new file mode 100644 index 0000000000..8551bc8cfa --- /dev/null +++ b/changelogs/unreleased/5248-allenxu404 @@ -0,0 +1 @@ +equip gc controller with configurable frequency \ No newline at end of file diff --git a/pkg/cmd/server/server.go b/pkg/cmd/server/server.go index c05dc75ef5..c0d86f399e 100644 --- a/pkg/cmd/server/server.go +++ b/pkg/cmd/server/server.go @@ -836,7 +836,7 @@ func (s *server) runControllers(defaultVolumeSnapshotLocations map[string]string } if _, ok := enabledRuntimeControllers[controller.GarbageCollection]; ok { - r := controller.NewGCReconciler(s.logger, s.mgr.GetClient()) + r := controller.NewGCReconciler(s.logger, s.mgr.GetClient(), s.config.garbageCollectionFrequency) if err := r.SetupWithManager(s.mgr); err != nil { s.logger.Fatal(err, "unable to create controller", "controller", controller.GarbageCollection) } diff --git a/pkg/controller/gc_controller.go b/pkg/controller/gc_controller.go index c12ba45d9c..60bfb40669 100644 --- a/pkg/controller/gc_controller.go +++ b/pkg/controller/gc_controller.go @@ -46,27 +46,34 @@ const ( // gcReconciler creates DeleteBackupRequests for expired backups. type gcReconciler struct { client.Client - logger logrus.FieldLogger - clock clock.Clock + logger logrus.FieldLogger + clock clock.Clock + frequency time.Duration } // NewGCReconciler constructs a new gcReconciler. func NewGCReconciler( logger logrus.FieldLogger, client client.Client, + frequency time.Duration, ) *gcReconciler { - return &gcReconciler{ - Client: client, - logger: logger, - clock: clock.RealClock{}, + gcr := &gcReconciler{ + Client: client, + logger: logger, + clock: clock.RealClock{}, + frequency: frequency, } + if gcr.frequency <= 0 { + gcr.frequency = defaultGCFrequency + } + return gcr } // GCController only watches on CreateEvent for ensuring every new backup will be taken care of. // Other Events will be filtered to decrease the number of reconcile call. Especially UpdateEvent must be filtered since we removed // the backup status as the sub-resource of backup in v1.9, every change on it will be treated as UpdateEvent and trigger reconcile call. func (c *gcReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(c.logger, mgr.GetClient(), &velerov1api.BackupList{}, defaultGCFrequency) + s := kube.NewPeriodicalEnqueueSource(c.logger, mgr.GetClient(), &velerov1api.BackupList{}, c.frequency) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.Backup{}). WithEventFilter(predicate.Funcs{ @@ -101,7 +108,7 @@ func (c *gcReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re } return ctrl.Result{}, errors.Wrapf(err, "error getting backup %s", req.String()) } - log.Debugf("backup: %v", backup) + log.Debugf("backup: %s", backup.Name) log = c.logger.WithFields( logrus.Fields{ @@ -116,7 +123,7 @@ func (c *gcReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Re return ctrl.Result{}, nil } - log.Info("Backup has expired") + log.Infof("Backup:%s has expired", backup.Name) if backup.Labels == nil { backup.Labels = make(map[string]string) diff --git a/pkg/controller/gc_controller_test.go b/pkg/controller/gc_controller_test.go index 8985f3be18..772d123c0a 100644 --- a/pkg/controller/gc_controller_test.go +++ b/pkg/controller/gc_controller_test.go @@ -34,10 +34,11 @@ import ( velerotest "github.com/vmware-tanzu/velero/pkg/test" ) -func mockGCReconciler(fakeClient kbclient.Client, fakeClock *clock.FakeClock) *gcReconciler { +func mockGCReconciler(fakeClient kbclient.Client, fakeClock *clock.FakeClock, freq time.Duration) *gcReconciler { gcr := NewGCReconciler( velerotest.NewLogger(), fakeClient, + freq, ) gcr.clock = fakeClock return gcr @@ -137,7 +138,7 @@ func TestGCReconcile(t *testing.T) { } fakeClient := velerotest.NewFakeControllerRuntimeClient(t, initObjs...) - reconciler := mockGCReconciler(fakeClient, fakeClock) + reconciler := mockGCReconciler(fakeClient, fakeClock, defaultGCFrequency) _, err := reconciler.Reconcile(context.TODO(), ctrl.Request{NamespacedName: types.NamespacedName{Namespace: test.backup.Namespace, Name: test.backup.Name}}) gotErr := err != nil assert.Equal(t, test.expectError, gotErr)