@@ -6,6 +6,7 @@ package prometheus
66import (
77 "context"
88 "net/http"
9+ "sync"
910 "time"
1011
1112 "github.com/cobaltcore-dev/cortex/api/v1alpha1"
@@ -50,6 +51,10 @@ type PrometheusDatasourceReconciler struct {
5051 conf config
5152 // Monitor for tracking the datasource syncs.
5253 Monitor datasources.Monitor
54+ // Tracks datasources that have completed at least one reconcile this process lifetime.
55+ // On first reconcile the timestamp skip is bypassed, so a DB wipe + operator restart
56+ // forces an immediate re-sync of all datasources.
57+ reconciledOnce sync.Map
5358}
5459
5560// Reconcile is part of the main kubernetes reconciliation loop which aims to
@@ -67,8 +72,11 @@ func (r *PrometheusDatasourceReconciler) Reconcile(ctx context.Context, req ctrl
6772 return ctrl.Result {}, nil
6873 }
6974 if datasource .Status .NextSyncTime .After (time .Now ()) && datasource .Status .NumberOfObjects != 0 {
70- log .Info ("skipping datasource sync, not yet time" , "name" , datasource .Name )
71- return ctrl.Result {RequeueAfter : time .Until (datasource .Status .NextSyncTime .Time )}, nil
75+ if _ , seen := r .reconciledOnce .Load (req .NamespacedName ); seen {
76+ log .Info ("skipping datasource sync, not yet time" , "name" , datasource .Name )
77+ return ctrl.Result {RequeueAfter : time .Until (datasource .Status .NextSyncTime .Time )}, nil
78+ }
79+ log .Info ("first reconcile this process lifetime, forcing sync despite timestamp" , "name" , datasource .Name )
7280 }
7381
7482 newSyncerFunc , ok := supportedMetricSyncers [datasource .Spec .Prometheus .Type ]
@@ -201,6 +209,7 @@ func (r *PrometheusDatasourceReconciler) Reconcile(ctx context.Context, req ctrl
201209 }
202210
203211 // Calculate the next sync time based on the configured sync interval.
212+ r .reconciledOnce .Store (req .NamespacedName , struct {}{})
204213 return ctrl.Result {RequeueAfter : time .Until (nextSync )}, nil
205214}
206215
0 commit comments