Skip to content

Commit 133ecd7

Browse files
committed
chore: Cleanup implementation
1 parent b076c76 commit 133ecd7

File tree

2 files changed

+36
-61
lines changed

2 files changed

+36
-61
lines changed

api/v1beta1/namespaced_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ type NamespacedResource string
99

1010
type NamespacedResourceList []NamespacedResource
1111

12+
// +kubebuilder:object:generate=false
13+
type NamespacedResourceImpl[T interface{}] interface {
14+
Find(string, string) *T
15+
}
16+
1217
func (in NamespacedResource) Split() (string, string, string) {
1318
parts := strings.Split(string(in), "/")
1419
return parts[0], parts[1], parts[2]

controllers/grafana_controller.go

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,22 @@ func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
134134
return ctrl.Result{}, nil
135135
}
136136

137+
func removeMissingCRs[T interface{}](statusList *grafanav1beta1.NamespacedResourceList, crs grafanav1beta1.NamespacedResourceImpl[T], updateStatus *bool) {
138+
for _, namespacedCR := range *statusList {
139+
namespace, name, _ := namespacedCR.Split()
140+
if crs.Find(namespace, name) == nil {
141+
*statusList = statusList.Remove(namespace, name)
142+
*updateStatus = true
143+
}
144+
}
145+
}
146+
137147
func (r *GrafanaReconciler) syncStatuses(ctx context.Context) error {
138148
log := logf.FromContext(ctx)
139149

140150
// get all grafana instances
141151
grafanas := &grafanav1beta1.GrafanaList{}
142-
var opts []client.ListOption
143-
err := r.List(ctx, grafanas, opts...)
152+
err := r.List(ctx, grafanas)
144153
if err != nil {
145154
return err
146155
}
@@ -151,100 +160,61 @@ func (r *GrafanaReconciler) syncStatuses(ctx context.Context) error {
151160

152161
// folders
153162
folders := &grafanav1beta1.GrafanaFolderList{}
154-
err = r.List(ctx, folders, opts...)
163+
err = r.List(ctx, folders)
155164
if err != nil {
156165
return err
157166
}
158167

159168
// dashboards
160169
dashboards := &grafanav1beta1.GrafanaDashboardList{}
161-
err = r.List(ctx, dashboards, opts...)
170+
err = r.List(ctx, dashboards)
162171
if err != nil {
163172
return err
164173
}
165174

166-
// library panels
167-
panels := &grafanav1beta1.GrafanaLibraryPanelList{}
168-
err = r.List(ctx, panels, opts...)
175+
// library libraryPanels
176+
libraryPanels := &grafanav1beta1.GrafanaLibraryPanelList{}
177+
err = r.List(ctx, libraryPanels)
169178
if err != nil {
170179
return err
171180
}
172181

173182
// datasources
174-
datasource := &grafanav1beta1.GrafanaDatasourceList{}
175-
err = r.List(ctx, datasource, opts...)
183+
datasources := &grafanav1beta1.GrafanaDatasourceList{}
184+
err = r.List(ctx, datasources)
176185
if err != nil {
177186
return err
178187
}
179188

180189
// contact points
181-
allContactPoints := &grafanav1beta1.GrafanaContactPointList{}
182-
err = r.List(ctx, allContactPoints, opts...)
190+
contactPoints := &grafanav1beta1.GrafanaContactPointList{}
191+
err = r.List(ctx, contactPoints)
183192
if err != nil {
184193
return err
185194
}
186195

187196
// delete resources from grafana statuses that no longer have a CR
188-
statusesSynced := 0
197+
statusUpdates := 0
189198
for _, grafana := range grafanas.Items {
190-
statusUpdated := false
191-
192-
// folders
193-
for _, folder := range grafana.Status.Folders {
194-
namespace, name, _ := folder.Split()
195-
if folders.Find(namespace, name) == nil {
196-
grafana.Status.Folders = grafana.Status.Folders.Remove(namespace, name)
197-
statusUpdated = true
198-
}
199-
}
200-
201-
// dashboards
202-
for _, dashboard := range grafana.Status.Dashboards {
203-
namespace, name, _ := dashboard.Split()
204-
if dashboards.Find(namespace, name) == nil {
205-
grafana.Status.Dashboards = grafana.Status.Dashboards.Remove(namespace, name)
206-
statusUpdated = true
207-
}
208-
}
199+
updateStatus := false
209200

210-
// library panels
211-
for _, panel := range grafana.Status.LibraryPanels {
212-
namespace, name, _ := panel.Split()
213-
if panels.Find(namespace, name) == nil {
214-
grafana.Status.LibraryPanels = grafana.Status.LibraryPanels.Remove(namespace, name)
215-
statusUpdated = true
216-
}
217-
}
218-
219-
// datasource
220-
for _, ds := range grafana.Status.Datasources {
221-
namespace, name, _ := ds.Split()
222-
if datasource.Find(namespace, name) == nil {
223-
grafana.Status.Datasources = grafana.Status.Datasources.Remove(namespace, name)
224-
statusUpdated = true
225-
}
226-
}
227-
228-
// contact points
229-
for _, contactpoint := range grafana.Status.ContactPoints {
230-
namespace, name, _ := contactpoint.Split()
231-
if allContactPoints.Find(namespace, name) == nil {
232-
grafana.Status.ContactPoints = grafana.Status.ContactPoints.Remove(namespace, name)
233-
statusUpdated = true
234-
}
235-
}
201+
removeMissingCRs(&grafana.Status.Folders, folders, &updateStatus)
202+
removeMissingCRs(&grafana.Status.Dashboards, dashboards, &updateStatus)
203+
removeMissingCRs(&grafana.Status.LibraryPanels, libraryPanels, &updateStatus)
204+
removeMissingCRs(&grafana.Status.Datasources, datasources, &updateStatus)
205+
removeMissingCRs(&grafana.Status.ContactPoints, contactPoints, &updateStatus)
236206

237-
if statusUpdated {
238-
statusesSynced += 1
207+
if updateStatus {
208+
statusUpdates += 1
239209
err = r.Client.Status().Update(ctx, &grafana)
240210
if err != nil {
241211
return err
242212
}
243213
}
244214
}
245215

246-
if statusesSynced > 0 {
247-
log.Info("successfully synced grafana statuses", "count", statusesSynced)
216+
if statusUpdates > 0 {
217+
log.Info("successfully synced grafana statuses", "update count", statusUpdates)
248218
}
249219
return nil
250220
}

0 commit comments

Comments
 (0)