From 5c4c66bee9fdc62a032ae49e1c7ea8a4d917701a Mon Sep 17 00:00:00 2001 From: Tiger Kaovilai Date: Mon, 9 Sep 2024 11:37:29 -0400 Subject: [PATCH 1/2] Add controller name to periodical_enqueue_source The code changes are related to the `NewPeriodicalEnqueueSource` function in the `kube/periodical_enqueue_source.go` file. This function is used to create a new instance of the `PeriodicalEnqueueSource` struct, which is responsible for periodically enqueueing objects into a work queue. The changes involve adding two new parameters to this function: `controllerName string` and modifying the existing `logger` parameter to include additional fields. Here's what changed: 1. A new `controllerName` parameter was added to the `NewPeriodicalEnqueueSource` function. These changes are to adding more context or metadata to the logging output, possibly for debugging or monitoring purposes. The other files (`restore_operations_controller.go`, `schedule_controller.go`, and their respective test files) were modified to use this updated `NewPeriodicalEnqueueSource` function with the new `controllerName` parameter. Signed-off-by: Tiger Kaovilai --- changelogs/unreleased/8198-kaovilai | 1 + pkg/controller/backup_deletion_controller.go | 2 +- pkg/controller/backup_operations_controller.go | 2 +- pkg/controller/backup_repository_controller.go | 2 +- pkg/controller/backup_storage_location_controller.go | 1 + pkg/controller/backup_sync_controller.go | 1 + pkg/controller/data_download_controller.go | 2 +- pkg/controller/data_upload_controller.go | 2 +- pkg/controller/download_request_controller.go | 2 +- pkg/controller/gc_controller.go | 2 +- pkg/controller/restore_operations_controller.go | 2 +- pkg/controller/schedule_controller.go | 2 +- pkg/util/kube/periodical_enqueue_source.go | 5 ++++- pkg/util/kube/periodical_enqueue_source_test.go | 4 +++- 14 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 changelogs/unreleased/8198-kaovilai diff --git a/changelogs/unreleased/8198-kaovilai b/changelogs/unreleased/8198-kaovilai new file mode 100644 index 0000000000..4a1320bc21 --- /dev/null +++ b/changelogs/unreleased/8198-kaovilai @@ -0,0 +1 @@ +Add controller name to periodical_enqueue_source. The logger parameter now includes an additional field with the value of reflect.TypeOf(objList).String() and another field with the value of controllerName. \ No newline at end of file diff --git a/pkg/controller/backup_deletion_controller.go b/pkg/controller/backup_deletion_controller.go index 87662c506a..cde32aceda 100644 --- a/pkg/controller/backup_deletion_controller.go +++ b/pkg/controller/backup_deletion_controller.go @@ -102,7 +102,7 @@ func NewBackupDeletionReconciler( func (r *backupDeletionReconciler) SetupWithManager(mgr ctrl.Manager) error { // Make sure the expired requests can be deleted eventually - s := kube.NewPeriodicalEnqueueSource(r.logger, mgr.GetClient(), &velerov1api.DeleteBackupRequestList{}, time.Hour, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("backupDeletion", r.logger, mgr.GetClient(), &velerov1api.DeleteBackupRequestList{}, time.Hour, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.DeleteBackupRequest{}). WatchesRawSource(s, nil). diff --git a/pkg/controller/backup_operations_controller.go b/pkg/controller/backup_operations_controller.go index 73836a0d33..581d1941bc 100644 --- a/pkg/controller/backup_operations_controller.go +++ b/pkg/controller/backup_operations_controller.go @@ -84,7 +84,7 @@ func NewBackupOperationsReconciler( } func (c *backupOperationsReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(c.logger, mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("backupOperations", c.logger, mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { backup := object.(*velerov1api.Backup) return (backup.Status.Phase == velerov1api.BackupPhaseWaitingForPluginOperations || diff --git a/pkg/controller/backup_repository_controller.go b/pkg/controller/backup_repository_controller.go index 0bc457a172..7e2e1eca1b 100644 --- a/pkg/controller/backup_repository_controller.go +++ b/pkg/controller/backup_repository_controller.go @@ -75,7 +75,7 @@ func NewBackupRepoReconciler(namespace string, logger logrus.FieldLogger, client } func (r *BackupRepoReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(r.logger, mgr.GetClient(), &velerov1api.BackupRepositoryList{}, repoSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("backupRepo", r.logger, mgr.GetClient(), &velerov1api.BackupRepositoryList{}, repoSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.BackupRepository{}, builder.WithPredicates(kube.SpecChangePredicate{})). diff --git a/pkg/controller/backup_storage_location_controller.go b/pkg/controller/backup_storage_location_controller.go index b10126eacb..66eb747b70 100644 --- a/pkg/controller/backup_storage_location_controller.go +++ b/pkg/controller/backup_storage_location_controller.go @@ -191,6 +191,7 @@ func (r *backupStorageLocationReconciler) logReconciledPhase(defaultFound bool, func (r *backupStorageLocationReconciler) SetupWithManager(mgr ctrl.Manager) error { g := kube.NewPeriodicalEnqueueSource( + "backupStorageLocation", r.log, mgr.GetClient(), &velerov1api.BackupStorageLocationList{}, diff --git a/pkg/controller/backup_sync_controller.go b/pkg/controller/backup_sync_controller.go index 5a9b445e6d..ac05a15433 100644 --- a/pkg/controller/backup_sync_controller.go +++ b/pkg/controller/backup_sync_controller.go @@ -348,6 +348,7 @@ func (b *backupSyncReconciler) filterBackupOwnerReferences(ctx context.Context, // SetupWithManager is used to setup controller and its watching sources. func (b *backupSyncReconciler) SetupWithManager(mgr ctrl.Manager) error { backupSyncSource := kube.NewPeriodicalEnqueueSource( + "backupSync", b.logger, mgr.GetClient(), &velerov1api.BackupStorageLocationList{}, diff --git a/pkg/controller/data_download_controller.go b/pkg/controller/data_download_controller.go index 7d6f6e0c6d..5921697d56 100644 --- a/pkg/controller/data_download_controller.go +++ b/pkg/controller/data_download_controller.go @@ -497,7 +497,7 @@ func (r *DataDownloadReconciler) OnDataDownloadProgress(ctx context.Context, nam // re-enqueue the previous related request once the related pod is in running status to keep going on the rest logic. and below logic will avoid handling the unwanted // pod status and also avoid block others CR handling func (r *DataDownloadReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(r.logger, r.client, &velerov2alpha1api.DataDownloadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("dataDownload", r.logger, r.client, &velerov2alpha1api.DataDownloadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { dd := object.(*velerov2alpha1api.DataDownload) return (dd.Status.Phase == velerov2alpha1api.DataDownloadPhaseAccepted) diff --git a/pkg/controller/data_upload_controller.go b/pkg/controller/data_upload_controller.go index 5b3169621f..39e9c73da0 100644 --- a/pkg/controller/data_upload_controller.go +++ b/pkg/controller/data_upload_controller.go @@ -534,7 +534,7 @@ func (r *DataUploadReconciler) OnDataUploadProgress(ctx context.Context, namespa // re-enqueue the previous related request once the related pod is in running status to keep going on the rest logic. and below logic will avoid handling the unwanted // pod status and also avoid block others CR handling func (r *DataUploadReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(r.logger, r.client, &velerov2alpha1api.DataUploadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("dataUpload", r.logger, r.client, &velerov2alpha1api.DataUploadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { du := object.(*velerov2alpha1api.DataUpload) return (du.Status.Phase == velerov2alpha1api.DataUploadPhaseAccepted) diff --git a/pkg/controller/download_request_controller.go b/pkg/controller/download_request_controller.go index 22e9d87075..3a2a721327 100644 --- a/pkg/controller/download_request_controller.go +++ b/pkg/controller/download_request_controller.go @@ -218,7 +218,7 @@ func (r *downloadRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ } func (r *downloadRequestReconciler) SetupWithManager(mgr ctrl.Manager) error { - downloadRequestSource := kube.NewPeriodicalEnqueueSource(r.log, mgr.GetClient(), + downloadRequestSource := kube.NewPeriodicalEnqueueSource("downloadRequest", r.log, mgr.GetClient(), &velerov1api.DownloadRequestList{}, defaultDownloadRequestSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) downloadRequestPredicates := kube.NewGenericEventPredicate(func(object kbclient.Object) bool { downloadRequest := object.(*velerov1api.DownloadRequest) diff --git a/pkg/controller/gc_controller.go b/pkg/controller/gc_controller.go index 7f464523c1..a5d5f2bdf4 100644 --- a/pkg/controller/gc_controller.go +++ b/pkg/controller/gc_controller.go @@ -75,7 +75,7 @@ func NewGCReconciler( // 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{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("gc", c.logger, mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.Backup{}, builder.WithPredicates(predicate.Funcs{ UpdateFunc: func(ue event.UpdateEvent) bool { diff --git a/pkg/controller/restore_operations_controller.go b/pkg/controller/restore_operations_controller.go index 24d4698a56..c70de6be05 100644 --- a/pkg/controller/restore_operations_controller.go +++ b/pkg/controller/restore_operations_controller.go @@ -82,7 +82,7 @@ func NewRestoreOperationsReconciler( } func (r *restoreOperationsReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(r.logger, mgr.GetClient(), &velerov1api.RestoreList{}, r.frequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("restoreOperations", r.logger, mgr.GetClient(), &velerov1api.RestoreList{}, r.frequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { restore := object.(*velerov1api.Restore) return (restore.Status.Phase == velerov1api.RestorePhaseWaitingForPluginOperations || diff --git a/pkg/controller/schedule_controller.go b/pkg/controller/schedule_controller.go index da15cdabf8..d1de7e5f09 100644 --- a/pkg/controller/schedule_controller.go +++ b/pkg/controller/schedule_controller.go @@ -69,7 +69,7 @@ func NewScheduleReconciler( } func (c *scheduleReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource(c.logger, mgr.GetClient(), &velerov1.ScheduleList{}, scheduleSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource("schedule", c.logger, mgr.GetClient(), &velerov1.ScheduleList{}, scheduleSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). // global predicate, works for both For and Watch WithEventFilter(kube.NewAllEventPredicate(func(obj client.Object) bool { diff --git a/pkg/util/kube/periodical_enqueue_source.go b/pkg/util/kube/periodical_enqueue_source.go index b4c4f27b8e..19ab2884b2 100644 --- a/pkg/util/kube/periodical_enqueue_source.go +++ b/pkg/util/kube/periodical_enqueue_source.go @@ -36,13 +36,16 @@ import ( ) func NewPeriodicalEnqueueSource( + controllerName string, logger logrus.FieldLogger, client client.Client, objList client.ObjectList, period time.Duration, option PeriodicalEnqueueSourceOption) *PeriodicalEnqueueSource { return &PeriodicalEnqueueSource{ - logger: logger.WithField("resource", reflect.TypeOf(objList).String()), + logger: logger. + WithField("resource", reflect.TypeOf(objList).String()). + WithField("controller", controllerName), Client: client, objList: objList, period: period, diff --git a/pkg/util/kube/periodical_enqueue_source_test.go b/pkg/util/kube/periodical_enqueue_source_test.go index a8b18c2291..b9028cc2cb 100644 --- a/pkg/util/kube/periodical_enqueue_source_test.go +++ b/pkg/util/kube/periodical_enqueue_source_test.go @@ -42,7 +42,7 @@ func TestStart(t *testing.T) { ctx, cancelFunc := context.WithCancel(context.TODO()) client := (&fake.ClientBuilder{}).Build() queue := workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()) - source := NewPeriodicalEnqueueSource(logrus.WithContext(ctx), client, &velerov1.ScheduleList{}, 1*time.Second, PeriodicalEnqueueSourceOption{}) + source := NewPeriodicalEnqueueSource("PES_TEST", logrus.WithContext(ctx), client, &velerov1.ScheduleList{}, 1*time.Second, PeriodicalEnqueueSourceOption{}) require.NoError(t, source.Start(ctx, nil, queue)) @@ -75,6 +75,7 @@ func TestPredicate(t *testing.T) { client := (&fake.ClientBuilder{}).Build() queue := workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()) source := NewPeriodicalEnqueueSource( + "PES_TEST", logrus.WithContext(ctx), client, &velerov1.BackupStorageLocationList{}, @@ -115,6 +116,7 @@ func TestOrder(t *testing.T) { client := (&fake.ClientBuilder{}).Build() queue := workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()) source := NewPeriodicalEnqueueSource( + "PES_TEST", logrus.WithContext(ctx), client, &velerov1.BackupStorageLocationList{}, From c8aa37d852a3e8526ceb77355e6b280fee030f27 Mon Sep 17 00:00:00 2001 From: Tiger Kaovilai Date: Wed, 11 Sep 2024 17:13:37 -0400 Subject: [PATCH 2/2] Remove additional param, use pkg/constant Signed-off-by: Tiger Kaovilai --- pkg/constant/constant.go | 2 ++ pkg/controller/backup_deletion_controller.go | 2 +- pkg/controller/backup_operations_controller.go | 3 ++- pkg/controller/backup_repository_controller.go | 3 ++- pkg/controller/backup_storage_location_controller.go | 3 +-- pkg/controller/backup_sync_controller.go | 3 +-- pkg/controller/data_download_controller.go | 3 ++- pkg/controller/data_upload_controller.go | 3 ++- pkg/controller/download_request_controller.go | 3 ++- pkg/controller/gc_controller.go | 3 ++- pkg/controller/restore_operations_controller.go | 3 ++- pkg/controller/schedule_controller.go | 3 ++- pkg/util/kube/periodical_enqueue_source.go | 5 +---- pkg/util/kube/periodical_enqueue_source_test.go | 8 +++----- 14 files changed, 25 insertions(+), 22 deletions(-) diff --git a/pkg/constant/constant.go b/pkg/constant/constant.go index ed4c530de5..f8d5b191f6 100644 --- a/pkg/constant/constant.go +++ b/pkg/constant/constant.go @@ -8,6 +8,8 @@ const ( ControllerBackupRepo = "backup-repo" ControllerBackupStorageLocation = "backup-storage-location" ControllerBackupSync = "backup-sync" + ControllerDataDownload = "data-download" + ControllerDataUpload = "data-upload" ControllerDownloadRequest = "download-request" ControllerGarbageCollection = "gc" ControllerPodVolumeBackup = "pod-volume-backup" diff --git a/pkg/controller/backup_deletion_controller.go b/pkg/controller/backup_deletion_controller.go index cde32aceda..cace160dc9 100644 --- a/pkg/controller/backup_deletion_controller.go +++ b/pkg/controller/backup_deletion_controller.go @@ -102,7 +102,7 @@ func NewBackupDeletionReconciler( func (r *backupDeletionReconciler) SetupWithManager(mgr ctrl.Manager) error { // Make sure the expired requests can be deleted eventually - s := kube.NewPeriodicalEnqueueSource("backupDeletion", r.logger, mgr.GetClient(), &velerov1api.DeleteBackupRequestList{}, time.Hour, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(r.logger.WithField("controller", constant.ControllerBackupDeletion), mgr.GetClient(), &velerov1api.DeleteBackupRequestList{}, time.Hour, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.DeleteBackupRequest{}). WatchesRawSource(s, nil). diff --git a/pkg/controller/backup_operations_controller.go b/pkg/controller/backup_operations_controller.go index 581d1941bc..597a81d900 100644 --- a/pkg/controller/backup_operations_controller.go +++ b/pkg/controller/backup_operations_controller.go @@ -34,6 +34,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/itemoperation" "github.com/vmware-tanzu/velero/pkg/itemoperationmap" "github.com/vmware-tanzu/velero/pkg/metrics" @@ -84,7 +85,7 @@ func NewBackupOperationsReconciler( } func (c *backupOperationsReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource("backupOperations", c.logger, mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(c.logger.WithField("controller", constant.ControllerBackupOperations), mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { backup := object.(*velerov1api.Backup) return (backup.Status.Phase == velerov1api.BackupPhaseWaitingForPluginOperations || diff --git a/pkg/controller/backup_repository_controller.go b/pkg/controller/backup_repository_controller.go index 7e2e1eca1b..b6e32d7eb8 100644 --- a/pkg/controller/backup_repository_controller.go +++ b/pkg/controller/backup_repository_controller.go @@ -36,6 +36,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/label" "github.com/vmware-tanzu/velero/pkg/repository" repoconfig "github.com/vmware-tanzu/velero/pkg/repository/config" @@ -75,7 +76,7 @@ func NewBackupRepoReconciler(namespace string, logger logrus.FieldLogger, client } func (r *BackupRepoReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource("backupRepo", r.logger, mgr.GetClient(), &velerov1api.BackupRepositoryList{}, repoSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(r.logger.WithField("controller", constant.ControllerBackupRepo), mgr.GetClient(), &velerov1api.BackupRepositoryList{}, repoSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.BackupRepository{}, builder.WithPredicates(kube.SpecChangePredicate{})). diff --git a/pkg/controller/backup_storage_location_controller.go b/pkg/controller/backup_storage_location_controller.go index 66eb747b70..5f9f7caadb 100644 --- a/pkg/controller/backup_storage_location_controller.go +++ b/pkg/controller/backup_storage_location_controller.go @@ -191,8 +191,7 @@ func (r *backupStorageLocationReconciler) logReconciledPhase(defaultFound bool, func (r *backupStorageLocationReconciler) SetupWithManager(mgr ctrl.Manager) error { g := kube.NewPeriodicalEnqueueSource( - "backupStorageLocation", - r.log, + r.log.WithField("controller", constant.ControllerBackupStorageLocation), mgr.GetClient(), &velerov1api.BackupStorageLocationList{}, bslValidationEnqueuePeriod, diff --git a/pkg/controller/backup_sync_controller.go b/pkg/controller/backup_sync_controller.go index ac05a15433..0cbf392407 100644 --- a/pkg/controller/backup_sync_controller.go +++ b/pkg/controller/backup_sync_controller.go @@ -348,8 +348,7 @@ func (b *backupSyncReconciler) filterBackupOwnerReferences(ctx context.Context, // SetupWithManager is used to setup controller and its watching sources. func (b *backupSyncReconciler) SetupWithManager(mgr ctrl.Manager) error { backupSyncSource := kube.NewPeriodicalEnqueueSource( - "backupSync", - b.logger, + b.logger.WithField("controller", constant.ControllerBackupSync), mgr.GetClient(), &velerov1api.BackupStorageLocationList{}, backupSyncReconcilePeriod, diff --git a/pkg/controller/data_download_controller.go b/pkg/controller/data_download_controller.go index 5921697d56..701a033ee7 100644 --- a/pkg/controller/data_download_controller.go +++ b/pkg/controller/data_download_controller.go @@ -42,6 +42,7 @@ import ( "github.com/vmware-tanzu/velero/pkg/apis/velero/shared" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1" + "github.com/vmware-tanzu/velero/pkg/constant" datamover "github.com/vmware-tanzu/velero/pkg/datamover" "github.com/vmware-tanzu/velero/pkg/datapath" "github.com/vmware-tanzu/velero/pkg/exposer" @@ -497,7 +498,7 @@ func (r *DataDownloadReconciler) OnDataDownloadProgress(ctx context.Context, nam // re-enqueue the previous related request once the related pod is in running status to keep going on the rest logic. and below logic will avoid handling the unwanted // pod status and also avoid block others CR handling func (r *DataDownloadReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource("dataDownload", r.logger, r.client, &velerov2alpha1api.DataDownloadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(r.logger.WithField("controller", constant.ControllerDataDownload), r.client, &velerov2alpha1api.DataDownloadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { dd := object.(*velerov2alpha1api.DataDownload) return (dd.Status.Phase == velerov2alpha1api.DataDownloadPhaseAccepted) diff --git a/pkg/controller/data_upload_controller.go b/pkg/controller/data_upload_controller.go index 39e9c73da0..72a2d8b39d 100644 --- a/pkg/controller/data_upload_controller.go +++ b/pkg/controller/data_upload_controller.go @@ -44,6 +44,7 @@ import ( "github.com/vmware-tanzu/velero/pkg/apis/velero/shared" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/datamover" "github.com/vmware-tanzu/velero/pkg/datapath" "github.com/vmware-tanzu/velero/pkg/exposer" @@ -534,7 +535,7 @@ func (r *DataUploadReconciler) OnDataUploadProgress(ctx context.Context, namespa // re-enqueue the previous related request once the related pod is in running status to keep going on the rest logic. and below logic will avoid handling the unwanted // pod status and also avoid block others CR handling func (r *DataUploadReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource("dataUpload", r.logger, r.client, &velerov2alpha1api.DataUploadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(r.logger.WithField("controller", constant.ControllerDataUpload), r.client, &velerov2alpha1api.DataUploadList{}, preparingMonitorFrequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { du := object.(*velerov2alpha1api.DataUpload) return (du.Status.Phase == velerov2alpha1api.DataUploadPhaseAccepted) diff --git a/pkg/controller/download_request_controller.go b/pkg/controller/download_request_controller.go index 3a2a721327..687763a532 100644 --- a/pkg/controller/download_request_controller.go +++ b/pkg/controller/download_request_controller.go @@ -30,6 +30,7 @@ import ( kbclient "sigs.k8s.io/controller-runtime/pkg/client" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/itemoperationmap" "github.com/vmware-tanzu/velero/pkg/persistence" "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt" @@ -218,7 +219,7 @@ func (r *downloadRequestReconciler) Reconcile(ctx context.Context, req ctrl.Requ } func (r *downloadRequestReconciler) SetupWithManager(mgr ctrl.Manager) error { - downloadRequestSource := kube.NewPeriodicalEnqueueSource("downloadRequest", r.log, mgr.GetClient(), + downloadRequestSource := kube.NewPeriodicalEnqueueSource(r.log.WithField("controller", constant.ControllerDownloadRequest), mgr.GetClient(), &velerov1api.DownloadRequestList{}, defaultDownloadRequestSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) downloadRequestPredicates := kube.NewGenericEventPredicate(func(object kbclient.Object) bool { downloadRequest := object.(*velerov1api.DownloadRequest) diff --git a/pkg/controller/gc_controller.go b/pkg/controller/gc_controller.go index a5d5f2bdf4..0f00951eed 100644 --- a/pkg/controller/gc_controller.go +++ b/pkg/controller/gc_controller.go @@ -33,6 +33,7 @@ import ( velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" pkgbackup "github.com/vmware-tanzu/velero/pkg/backup" veleroclient "github.com/vmware-tanzu/velero/pkg/client" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/label" "github.com/vmware-tanzu/velero/pkg/util/kube" ) @@ -75,7 +76,7 @@ func NewGCReconciler( // 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("gc", c.logger, mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(c.logger.WithField("controller", constant.ControllerGarbageCollection), mgr.GetClient(), &velerov1api.BackupList{}, c.frequency, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). For(&velerov1api.Backup{}, builder.WithPredicates(predicate.Funcs{ UpdateFunc: func(ue event.UpdateEvent) bool { diff --git a/pkg/controller/restore_operations_controller.go b/pkg/controller/restore_operations_controller.go index c70de6be05..f8f20c5d64 100644 --- a/pkg/controller/restore_operations_controller.go +++ b/pkg/controller/restore_operations_controller.go @@ -30,6 +30,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/itemoperation" "github.com/vmware-tanzu/velero/pkg/itemoperationmap" "github.com/vmware-tanzu/velero/pkg/metrics" @@ -82,7 +83,7 @@ func NewRestoreOperationsReconciler( } func (r *restoreOperationsReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource("restoreOperations", r.logger, mgr.GetClient(), &velerov1api.RestoreList{}, r.frequency, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(r.logger.WithField("controller", constant.ControllerRestoreOperations), mgr.GetClient(), &velerov1api.RestoreList{}, r.frequency, kube.PeriodicalEnqueueSourceOption{}) gp := kube.NewGenericEventPredicate(func(object client.Object) bool { restore := object.(*velerov1api.Restore) return (restore.Status.Phase == velerov1api.RestorePhaseWaitingForPluginOperations || diff --git a/pkg/controller/schedule_controller.go b/pkg/controller/schedule_controller.go index d1de7e5f09..2b77ea6460 100644 --- a/pkg/controller/schedule_controller.go +++ b/pkg/controller/schedule_controller.go @@ -34,6 +34,7 @@ import ( velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" "github.com/vmware-tanzu/velero/pkg/builder" + "github.com/vmware-tanzu/velero/pkg/constant" "github.com/vmware-tanzu/velero/pkg/metrics" "github.com/vmware-tanzu/velero/pkg/util/kube" ) @@ -69,7 +70,7 @@ func NewScheduleReconciler( } func (c *scheduleReconciler) SetupWithManager(mgr ctrl.Manager) error { - s := kube.NewPeriodicalEnqueueSource("schedule", c.logger, mgr.GetClient(), &velerov1.ScheduleList{}, scheduleSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) + s := kube.NewPeriodicalEnqueueSource(c.logger.WithField("controller", constant.ControllerSchedule), mgr.GetClient(), &velerov1.ScheduleList{}, scheduleSyncPeriod, kube.PeriodicalEnqueueSourceOption{}) return ctrl.NewControllerManagedBy(mgr). // global predicate, works for both For and Watch WithEventFilter(kube.NewAllEventPredicate(func(obj client.Object) bool { diff --git a/pkg/util/kube/periodical_enqueue_source.go b/pkg/util/kube/periodical_enqueue_source.go index 19ab2884b2..b4c4f27b8e 100644 --- a/pkg/util/kube/periodical_enqueue_source.go +++ b/pkg/util/kube/periodical_enqueue_source.go @@ -36,16 +36,13 @@ import ( ) func NewPeriodicalEnqueueSource( - controllerName string, logger logrus.FieldLogger, client client.Client, objList client.ObjectList, period time.Duration, option PeriodicalEnqueueSourceOption) *PeriodicalEnqueueSource { return &PeriodicalEnqueueSource{ - logger: logger. - WithField("resource", reflect.TypeOf(objList).String()). - WithField("controller", controllerName), + logger: logger.WithField("resource", reflect.TypeOf(objList).String()), Client: client, objList: objList, period: period, diff --git a/pkg/util/kube/periodical_enqueue_source_test.go b/pkg/util/kube/periodical_enqueue_source_test.go index b9028cc2cb..6e8973d60a 100644 --- a/pkg/util/kube/periodical_enqueue_source_test.go +++ b/pkg/util/kube/periodical_enqueue_source_test.go @@ -42,7 +42,7 @@ func TestStart(t *testing.T) { ctx, cancelFunc := context.WithCancel(context.TODO()) client := (&fake.ClientBuilder{}).Build() queue := workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()) - source := NewPeriodicalEnqueueSource("PES_TEST", logrus.WithContext(ctx), client, &velerov1.ScheduleList{}, 1*time.Second, PeriodicalEnqueueSourceOption{}) + source := NewPeriodicalEnqueueSource(logrus.WithContext(ctx).WithField("controller", "PES_TEST"), client, &velerov1.ScheduleList{}, 1*time.Second, PeriodicalEnqueueSourceOption{}) require.NoError(t, source.Start(ctx, nil, queue)) @@ -75,8 +75,7 @@ func TestPredicate(t *testing.T) { client := (&fake.ClientBuilder{}).Build() queue := workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()) source := NewPeriodicalEnqueueSource( - "PES_TEST", - logrus.WithContext(ctx), + logrus.WithContext(ctx).WithField("controller", "PES_TEST"), client, &velerov1.BackupStorageLocationList{}, 1*time.Second, @@ -116,8 +115,7 @@ func TestOrder(t *testing.T) { client := (&fake.ClientBuilder{}).Build() queue := workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()) source := NewPeriodicalEnqueueSource( - "PES_TEST", - logrus.WithContext(ctx), + logrus.WithContext(ctx).WithField("controller", "PES_TEST"), client, &velerov1.BackupStorageLocationList{}, 1*time.Second,