Skip to content

Commit

Permalink
refactor: Use of ResyncPeriod in all reconcilers
Browse files Browse the repository at this point in the history
  • Loading branch information
Baarsgaard committed Nov 18, 2024
1 parent de69b0a commit 8d94cc2
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 57 deletions.
17 changes: 1 addition & 16 deletions api/v1beta1/grafanadashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 1 addition & 16 deletions api/v1beta1/grafanadatasource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 1 addition & 16 deletions api/v1beta1/grafanafolder_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions controllers/dashboard_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
},
Expand All @@ -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",
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion controllers/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8d94cc2

Please sign in to comment.