From 6da8cda6d1970c841822b7708a2e51435167417c Mon Sep 17 00:00:00 2001 From: Peter Wilcsinszky Date: Thu, 9 Jan 2025 13:16:17 +0100 Subject: [PATCH] feat: add sync-period to control resync if needed, add debug log to see when resync happened (#1902) Signed-off-by: Peter Wilcsinszky --- controllers/logging/logging_controller.go | 2 ++ main.go | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/controllers/logging/logging_controller.go b/controllers/logging/logging_controller.go index 6ba501a7f..842ec6d97 100644 --- a/controllers/logging/logging_controller.go +++ b/controllers/logging/logging_controller.go @@ -106,6 +106,8 @@ type LoggingReconciler struct { func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("logging", req.Name) + log.V(1).Info("reconciling") + var logging loggingv1beta1.Logging if err := r.Client.Get(ctx, req.NamespacedName, &logging); err != nil { // If object is not found, return without error. diff --git a/main.go b/main.go index 02faa495e..2a8b98cea 100644 --- a/main.go +++ b/main.go @@ -87,6 +87,7 @@ func main() { var finalizerCleanup bool var enableTelemetryControllerRoute bool var klogLevel int + var syncPeriod string flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, @@ -99,6 +100,7 @@ func main() { flag.StringVar(&loggingRef, "watch-logging-name", "", "Logging resource name to optionally filter the list of watched objects based on which logging they belong to by checking the app.kubernetes.io/managed-by label") flag.BoolVar(&finalizerCleanup, "finalizer-cleanup", false, "Remove finalizers from Logging resources during operator shutdown, useful for Helm uninstallation") flag.BoolVar(&enableTelemetryControllerRoute, "enable-telemetry-controller-route", false, "Enable the Telemetry Controller route for Logging resources") + flag.StringVar(&syncPeriod, "sync-period", "", "SyncPeriod determines the minimum frequency at which watched resources are reconciled. Defaults to 10 hours. Parsed using time.ParseDuration.") flag.Parse() ctx := context.Background() @@ -150,7 +152,7 @@ func main() { mgrOptions.WebhookServer = webhookServer } - customMgrOptions, err := setupCustomCache(&mgrOptions, namespace, loggingRef) + customMgrOptions, err := setupCustomCache(&mgrOptions, syncPeriod, namespace, loggingRef) if err != nil { setupLog.Error(err, "unable to set up custom cache settings") os.Exit(1) @@ -310,7 +312,15 @@ func detectContainerRuntime(ctx context.Context, c client.Reader) error { return nil } -func setupCustomCache(mgrOptions *ctrl.Options, namespace string, loggingRef string) (*ctrl.Options, error) { +func setupCustomCache(mgrOptions *ctrl.Options, syncPeriod string, namespace string, loggingRef string) (*ctrl.Options, error) { + if syncPeriod != "" { + duration, err := time.ParseDuration(syncPeriod) + if err != nil { + return mgrOptions, err + } + mgrOptions.Cache.SyncPeriod = &duration + } + if namespace == "" && loggingRef == "" { return mgrOptions, nil }