Skip to content

Commit 0b499db

Browse files
authored
feat: All datasources are synced on restart (#956)
1 parent fa70a76 commit 0b499db

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

internal/knowledge/datasources/plugins/openstack/controller.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99
"net/http"
10+
"sync"
1011
"time"
1112

1213
"github.com/cobaltcore-dev/cortex/api/v1alpha1"
@@ -63,6 +64,10 @@ type OpenStackDatasourceReconciler struct {
6364

6465
// Config for the reconciler.
6566
conf config
67+
// Tracks datasources that have completed at least one reconcile this process lifetime.
68+
// On first reconcile the timestamp skip is bypassed, so a DB wipe + operator restart
69+
// forces an immediate re-sync of all datasources.
70+
reconciledOnce sync.Map
6671
}
6772

6873
// Reconcile is part of the main kubernetes reconciliation loop which aims to
@@ -91,8 +96,11 @@ func (r *OpenStackDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.
9196
return ctrl.Result{}, nil
9297
}
9398
if datasource.Status.NextSyncTime.After(time.Now()) && datasource.Status.NumberOfObjects != 0 {
94-
log.Info("skipping datasource sync, not yet time", "name", datasource.Name)
95-
return ctrl.Result{RequeueAfter: time.Until(datasource.Status.NextSyncTime.Time)}, nil
99+
if _, seen := r.reconciledOnce.Load(req.NamespacedName); seen {
100+
log.Info("skipping datasource sync, not yet time", "name", datasource.Name)
101+
return ctrl.Result{RequeueAfter: time.Until(datasource.Status.NextSyncTime.Time)}, nil
102+
}
103+
log.Info("first reconcile this process lifetime, forcing sync despite timestamp", "name", datasource.Name)
96104
}
97105

98106
// Authenticate with the database based on the secret provided in the datasource.
@@ -263,6 +271,7 @@ func (r *OpenStackDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.
263271

264272
// Calculate the next sync time based on the configured sync interval.
265273
log.Info("Finished reconcile", "next", nextTime)
274+
r.reconciledOnce.Store(req.NamespacedName, struct{}{})
266275
return ctrl.Result{RequeueAfter: datasource.Spec.OpenStack.SyncInterval.Duration}, nil
267276
}
268277

internal/knowledge/datasources/plugins/prometheus/controller.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package prometheus
66
import (
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

Comments
 (0)