Skip to content

Commit

Permalink
Merge pull request #5248 from allenxu404/gc-freq
Browse files Browse the repository at this point in the history
equip gc controller with configurable frequency
  • Loading branch information
blackpiglet authored Aug 30, 2022
2 parents c8818ec + a8ba487 commit 4bc3a3a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/5248-allenxu404
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
equip gc controller with configurable frequency
2 changes: 1 addition & 1 deletion pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,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)
}
Expand Down
25 changes: 16 additions & 9 deletions pkg/controller/gc_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/gc_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4bc3a3a

Please sign in to comment.