diff --git a/api/v1beta1/grafanadashboard_types.go b/api/v1beta1/grafanadashboard_types.go index 4fb66ce2d..21e14e78c 100644 --- a/api/v1beta1/grafanadashboard_types.go +++ b/api/v1beta1/grafanadashboard_types.go @@ -246,25 +246,10 @@ func (in *GrafanaDashboard) Unchanged(hash string) bool { } func (in *GrafanaDashboard) ResyncPeriodHasElapsed() bool { - deadline := in.Status.LastResync.Add(in.GetResyncPeriod()) + deadline := in.Status.LastResync.Add(in.Spec.ResyncPeriod.Duration) return time.Now().After(deadline) } -func (in *GrafanaDashboard) GetResyncPeriod() time.Duration { - if in.Spec.ResyncPeriod == "" { - in.Spec.ResyncPeriod = DefaultResyncPeriod - return in.GetResyncPeriod() - } - - duration, err := time.ParseDuration(in.Spec.ResyncPeriod) - if err != nil { - in.Spec.ResyncPeriod = DefaultResyncPeriod - return in.GetResyncPeriod() - } - - return duration -} - func (in *GrafanaDashboard) GetSourceTypes() []DashboardSourceType { var sourceTypes []DashboardSourceType diff --git a/api/v1beta1/grafanadatasource_types.go b/api/v1beta1/grafanadatasource_types.go index 0c36ece3a..ebc2b48e5 100644 --- a/api/v1beta1/grafanadatasource_types.go +++ b/api/v1beta1/grafanadatasource_types.go @@ -115,23 +115,8 @@ type GrafanaDatasourceList struct { Items []GrafanaDatasource `json:"items"` } -func (in *GrafanaDatasource) GetResyncPeriod() time.Duration { - if in.Spec.ResyncPeriod == "" { - in.Spec.ResyncPeriod = DefaultResyncPeriod - return in.GetResyncPeriod() - } - - duration, err := time.ParseDuration(in.Spec.ResyncPeriod) - if err != nil { - in.Spec.ResyncPeriod = DefaultResyncPeriod - return in.GetResyncPeriod() - } - - return duration -} - func (in *GrafanaDatasource) ResyncPeriodHasElapsed() bool { - deadline := in.Status.LastResync.Add(in.GetResyncPeriod()) + deadline := in.Status.LastResync.Add(in.Spec.ResyncPeriod.Duration) return time.Now().After(deadline) } diff --git a/api/v1beta1/grafanafolder_types.go b/api/v1beta1/grafanafolder_types.go index 0aacc75af..cbde5117f 100644 --- a/api/v1beta1/grafanafolder_types.go +++ b/api/v1beta1/grafanafolder_types.go @@ -166,22 +166,7 @@ func (in *GrafanaFolder) GetTitle() string { return in.Name } -func (in *GrafanaFolder) GetResyncPeriod() time.Duration { - if in.Spec.ResyncPeriod == "" { - in.Spec.ResyncPeriod = DefaultResyncPeriod - return in.GetResyncPeriod() - } - - duration, err := time.ParseDuration(in.Spec.ResyncPeriod) - if err != nil { - in.Spec.ResyncPeriod = DefaultResyncPeriod - return in.GetResyncPeriod() - } - - return duration -} - func (in *GrafanaFolder) ResyncPeriodHasElapsed() bool { - deadline := in.Status.LastResync.Add(in.GetResyncPeriod()) + deadline := in.Status.LastResync.Add(in.Spec.ResyncPeriod.Duration) return time.Now().After(deadline) } diff --git a/controllers/dashboard_controller.go b/controllers/dashboard_controller.go index 210380b79..fb85ef0d6 100644 --- a/controllers/dashboard_controller.go +++ b/controllers/dashboard_controller.go @@ -289,7 +289,7 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req } cr.Status.Hash = hash cr.Status.UID = uid - return ctrl.Result{RequeueAfter: cr.GetResyncPeriod()}, r.Client.Status().Update(ctx, cr) + return ctrl.Result{RequeueAfter: cr.Spec.ResyncPeriod.Duration}, r.Client.Status().Update(ctx, cr) } return ctrl.Result{RequeueAfter: RequeueDelay}, nil diff --git a/controllers/dashboard_controller_test.go b/controllers/dashboard_controller_test.go index f14f919d3..a93495a99 100644 --- a/controllers/dashboard_controller_test.go +++ b/controllers/dashboard_controller_test.go @@ -42,9 +42,11 @@ func TestGetDashboardsToDelete(t *testing.T) { Namespace: "grafana-operator-system", }, Spec: v1beta1.GrafanaDashboardSpec{ - InstanceSelector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - "dashboard": "external", + GrafanaCommonSpec: v1beta1.GrafanaCommonSpec{ + InstanceSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "dashboard": "external", + }, }, }, }, @@ -56,9 +58,11 @@ func TestGetDashboardsToDelete(t *testing.T) { Namespace: "grafana-operator-system", }, Spec: v1beta1.GrafanaDashboardSpec{ - InstanceSelector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - "dashboard": "internal", + GrafanaCommonSpec: v1beta1.GrafanaCommonSpec{ + InstanceSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + "dashboard": "external", + }, }, }, }, diff --git a/controllers/datasource_controller.go b/controllers/datasource_controller.go index c99a3cc66..951d27e5b 100644 --- a/controllers/datasource_controller.go +++ b/controllers/datasource_controller.go @@ -262,7 +262,7 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re cr.Status.LastResync = metav1.Time{Time: time.Now()} } cr.Status.UID = cr.CustomUIDOrUID() - return ctrl.Result{RequeueAfter: cr.GetResyncPeriod()}, r.Client.Status().Update(ctx, cr) + return ctrl.Result{RequeueAfter: cr.Spec.ResyncPeriod.Duration}, r.Client.Status().Update(ctx, cr) } else { // if there was an issue with the datasource, update the status return ctrl.Result{RequeueAfter: RequeueDelay}, r.Client.Status().Update(ctx, cr) diff --git a/controllers/grafanafolder_controller.go b/controllers/grafanafolder_controller.go index 139b44cde..0f3ddb034 100644 --- a/controllers/grafanafolder_controller.go +++ b/controllers/grafanafolder_controller.go @@ -238,7 +238,7 @@ func (r *GrafanaFolderReconciler) Reconcile(ctx context.Context, req ctrl.Reques if folder.ResyncPeriodHasElapsed() { folder.Status.LastResync = metav1.Time{Time: time.Now()} } - return ctrl.Result{RequeueAfter: folder.GetResyncPeriod()}, nil + return ctrl.Result{RequeueAfter: folder.Spec.ResyncPeriod.Duration}, nil } // SetupWithManager sets up the controller with the Manager.