Skip to content
Next Next commit
Add DD_DASHBOARD_FORCE_SYNC_PERIOD environment variable Fixes #2179
Signed-off-by: puretension <[email protected]>
  • Loading branch information
puretension committed Sep 18, 2025
commit 760353fcbd54480552beb752d62867f4a6d27e20
25 changes: 20 additions & 5 deletions internal/controller/datadogdashboard/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package datadogdashboard

import (
"context"
"os"
"strconv"
"strings"
"time"

Expand All @@ -28,10 +30,11 @@ import (
)

const (
defaultRequeuePeriod = 60 * time.Second
defaultErrRequeuePeriod = 5 * time.Second
defaultForceSyncPeriod = 60 * time.Minute
datadogDashboardKind = "DatadogDashboard"
defaultRequeuePeriod = 60 * time.Second
defaultErrRequeuePeriod = 5 * time.Second
defaultForceSyncPeriod = 60 * time.Minute
datadogDashboardKind = "DatadogDashboard"
DDDashboardForceSyncPeriodEnvVar = "DD_DASHBOARD_FORCE_SYNC_PERIOD"
)

type Reconciler struct {
Expand Down Expand Up @@ -63,6 +66,18 @@ func (r *Reconciler) internalReconcile(ctx context.Context, req reconcile.Reques
logger.Info("Reconciling Datadog Dashboard")
now := metav1.NewTime(time.Now())

forceSyncPeriod := defaultForceSyncPeriod

if userForceSyncPeriod, ok := os.LookupEnv(DDDashboardForceSyncPeriodEnvVar); ok {
forceSyncPeriodInt, err := strconv.Atoi(userForceSyncPeriod)
if err != nil {
logger.Error(err, "Invalid value for dashboard force sync period. Defaulting to 60 minutes.")
} else {
logger.V(1).Info("Setting dashboard force sync period", "minutes", forceSyncPeriodInt)
forceSyncPeriod = time.Duration(forceSyncPeriodInt) * time.Minute
}
}

instance := &v1alpha1.DatadogDashboard{}
var result ctrl.Result
var err error
Expand Down Expand Up @@ -106,7 +121,7 @@ func (r *Reconciler) internalReconcile(ctx context.Context, req reconcile.Reques
if instanceSpecHash != statusSpecHash {
logger.Info("DatadogDashboard manifest has changed")
shouldUpdate = true
} else if instance.Status.LastForceSyncTime == nil || ((defaultForceSyncPeriod - now.Sub(instance.Status.LastForceSyncTime.Time)) <= 0) {
} else if instance.Status.LastForceSyncTime == nil || ((forceSyncPeriod - now.Sub(instance.Status.LastForceSyncTime.Time)) <= 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puretension Thanks for putting this together! It looks good I think but it seems like this doesn't follow the same pattern as the monitors do exactly. I haven't dived too deeply into the patterns used in other crd controllers here but the Monitor controller seems to check the Monitor specific MonitorLastForceSyncTime rather than the generic LastForceSyncTime:

} else if instance.Status.MonitorLastForceSyncTime == nil || (forceSyncPeriod-now.Sub(instance.Status.MonitorLastForceSyncTime.Time)) <= 0 {
// Periodically force a sync with the API monitor to ensure parity
// Get monitor to make sure it exists before trying any updates. If it doesn't, set shouldCreate
m, err = r.get(instance, newStatus)
if err != nil {
logger.Error(err, "error getting monitor", "Monitor ID", instance.Status.ID)
if strings.Contains(err.Error(), ctrutils.NotFoundString) {
shouldCreate = true
}
} else {
shouldUpdate = true
}
} else if instance.Status.MonitorStateLastUpdateTime == nil || (defaultRequeuePeriod-now.Sub(instance.Status.MonitorStateLastUpdateTime.Time)) <= 0 {
// If other conditions aren't met, and we have passed the defaultRequeuePeriod, then update monitor state
// Get monitor to make sure it exists before trying any updates. If it doesn't, set shouldCreate
m, err = r.get(instance, newStatus)
if err != nil {
logger.Error(err, "error getting monitor", "Monitor ID", instance.Status.ID)
if strings.Contains(err.Error(), ctrutils.NotFoundString) {
shouldCreate = true
}
}
updateMonitorState(m, now, newStatus)
}
}

The MonitorLastForceSyncTime seems to be set whenever a monitor is synced. I'm not sure if having a Monitor specific variable there was intentional or not or if the maintainers care about consistency in this case but thought it'd be worthwhile to bring to your attention.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orkhanM Great catch! You're absolutely right about the consistency issue.

Looking at the Monitor controller pattern you referenced,
I can see it uses MonitorLastForceSyncTime for the force sync check.
I'll update the Dashboard implementation to use DashboardLastForceSyncTime
to match this pattern for consistency across controllers.

Thanks for pointing this out!

// Periodically force a sync with the API to ensure parity
// Get Dashboard to make sure it exists before trying any updates. If it doesn't, set shouldCreate
_, err = r.get(instance)
Expand Down
Loading