From e45f0eb9237c73e671d6b28c2f68ff842aeca0b0 Mon Sep 17 00:00:00 2001 From: ste Date: Fri, 3 Jan 2025 17:37:41 +0100 Subject: [PATCH 1/2] chore: Refactor const usage --- controllers/controller_shared.go | 10 ++++++++-- controllers/dashboard_controller.go | 9 +-------- controllers/datasource_controller.go | 7 +------ controllers/grafana_controller.go | 5 ----- controllers/grafanafolder_controller.go | 7 +------ 5 files changed, 11 insertions(+), 27 deletions(-) diff --git a/controllers/controller_shared.go b/controllers/controller_shared.go index 1300e18f0..2df09e8ac 100644 --- a/controllers/controller_shared.go +++ b/controllers/controller_shared.go @@ -26,9 +26,12 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" ) -const grafanaFinalizer = "operator.grafana.com/finalizer" - const ( + // Synchronization size and timeout values + syncBatchSize = 100 + initialSyncDelay = 10 * time.Second + RequeueDelay = 10 * time.Second + // condition types conditionNoMatchingInstance = "NoMatchingInstance" conditionNoMatchingFolder = "NoMatchingFolder" @@ -37,6 +40,9 @@ const ( // condition reasons conditionApplySuccessful = "ApplySuccessful" conditionApplyFailed = "ApplyFailed" + + // Finalizer + grafanaFinalizer = "operator.grafana.com/finalizer" ) //+kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;update;patch;delete diff --git a/controllers/dashboard_controller.go b/controllers/dashboard_controller.go index fb85ef0d6..87f9b560f 100644 --- a/controllers/dashboard_controller.go +++ b/controllers/dashboard_controller.go @@ -57,8 +57,6 @@ import ( ) const ( - initialSyncDelay = "10s" - syncBatchSize = 100 conditionDashboardSynchronized = "DashboardSynchronized" ) @@ -756,17 +754,12 @@ func (r *GrafanaDashboardReconciler) SetupWithManager(mgr ctrl.Manager, ctx cont Complete(r) if err == nil { - d, err := time.ParseDuration(initialSyncDelay) - if err != nil { - return err - } - go func() { for { select { case <-ctx.Done(): return - case <-time.After(d): + case <-time.After(initialSyncDelay): result, err := r.Reconcile(ctx, ctrl.Request{}) if err != nil { r.Log.Error(err, "error synchronizing dashboards") diff --git a/controllers/datasource_controller.go b/controllers/datasource_controller.go index 951d27e5b..33b7ddfa4 100644 --- a/controllers/datasource_controller.go +++ b/controllers/datasource_controller.go @@ -390,17 +390,12 @@ func (r *GrafanaDatasourceReconciler) SetupWithManager(mgr ctrl.Manager, ctx con Complete(r) if err == nil { - d, err := time.ParseDuration(initialSyncDelay) - if err != nil { - return err - } - go func() { for { select { case <-ctx.Done(): return - case <-time.After(d): + case <-time.After(initialSyncDelay): result, err := r.Reconcile(ctx, ctrl.Request{}) if err != nil { r.Log.Error(err, "error synchronizing datasources") diff --git a/controllers/grafana_controller.go b/controllers/grafana_controller.go index 6c5ff25b3..f0f8e61cf 100644 --- a/controllers/grafana_controller.go +++ b/controllers/grafana_controller.go @@ -24,7 +24,6 @@ import ( "os" "reflect" "strings" - "time" "github.com/go-logr/logr" "github.com/grafana/grafana-operator/v5/controllers/config" @@ -45,10 +44,6 @@ import ( client2 "github.com/grafana/grafana-operator/v5/controllers/client" ) -const ( - RequeueDelay = 10 * time.Second -) - // GrafanaReconciler reconciles a Grafana object type GrafanaReconciler struct { client.Client diff --git a/controllers/grafanafolder_controller.go b/controllers/grafanafolder_controller.go index 643b23e4b..74255de9a 100644 --- a/controllers/grafanafolder_controller.go +++ b/controllers/grafanafolder_controller.go @@ -256,17 +256,12 @@ func (r *GrafanaFolderReconciler) SetupWithManager(mgr ctrl.Manager, ctx context Complete(r) if err == nil { - d, err := time.ParseDuration(initialSyncDelay) - if err != nil { - return err - } - go func() { for { select { case <-ctx.Done(): return - case <-time.After(d): + case <-time.After(initialSyncDelay): result, err := r.Reconcile(ctx, ctrl.Request{}) if err != nil { r.Log.Error(err, "error synchronizing folders") From d979cf5227b85bd4cca915e58aaa8477f2c1c323 Mon Sep 17 00:00:00 2001 From: ste Date: Sat, 4 Jan 2025 14:18:55 +0100 Subject: [PATCH 2/2] chore: Improve logs when instances are unready --- controllers/controller_shared.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/controller_shared.go b/controllers/controller_shared.go index 2df09e8ac..aa58c3cc1 100644 --- a/controllers/controller_shared.go +++ b/controllers/controller_shared.go @@ -120,7 +120,7 @@ func GetScopedMatchingInstances(log logr.Logger, ctx context.Context, k8sClient selectedList = append(selectedList, instance) } if len(unready_instances) > 0 { - log.Info("Grafana instances not ready", "instances", unready_instances) + log.Info("Grafana instances not ready, excluded from matching", "instances", unready_instances) } return selectedList, nil