Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/datadoghq/v1alpha1/datadogdashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ type DatadogDashboardStatus struct {
// CurrentHash tracks the hash of the current DatadogDashboardSpec to know
// if the Spec has changed and needs an update.
CurrentHash string `json:"currentHash,omitempty"`
// LastForceSyncTime is the last time the API dashboard was last force synced with the DatadogDashboard resource
LastForceSyncTime *metav1.Time `json:"lastForceSyncTime,omitempty"`
// DashboardLastForceSyncTime is the last time the API dashboard was last force synced with the DatadogDashboard resource
DashboardLastForceSyncTime *metav1.Time `json:"dashboardLastForceSyncTime,omitempty"`
}

type DatadogDashboardSyncStatus string
Expand Down
4 changes: 2 additions & 2 deletions api/datadoghq/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 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.DashboardLastForceSyncTime == nil || ((forceSyncPeriod - now.Sub(instance.Status.DashboardLastForceSyncTime.Time)) <= 0) {
// 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 All @@ -119,7 +134,6 @@ func (r *Reconciler) internalReconcile(ctx context.Context, req reconcile.Reques
} else {
shouldUpdate = true
}
status.LastForceSyncTime = &now
}
}

Expand Down Expand Up @@ -171,7 +185,7 @@ func (r *Reconciler) update(logger logr.Logger, instance *v1alpha1.DatadogDashbo
condition.UpdateStatusConditions(&status.Conditions, now, condition.DatadogConditionTypeUpdated, metav1.ConditionTrue, "UpdatingDashboard", "DatadogDashboard Update")
status.SyncStatus = v1alpha1.DatadogDashboardSyncStatusOK
status.CurrentHash = hash
status.LastForceSyncTime = &now
status.DashboardLastForceSyncTime = &now

logger.Info("Updated DatadogDashboard", "Dashboard ID", instance.Status.ID)
return nil
Expand All @@ -196,7 +210,7 @@ func (r *Reconciler) create(logger logr.Logger, instance *v1alpha1.DatadogDashbo
status.Creator = createdDashboard.GetAuthorHandle()
status.Created = &createdTime
status.SyncStatus = v1alpha1.DatadogDashboardSyncStatusOK
status.LastForceSyncTime = &createdTime
status.DashboardLastForceSyncTime = &createdTime
status.CurrentHash = hash

// Set condition and status
Expand Down