@@ -134,13 +134,22 @@ func (r *GrafanaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
134
134
return ctrl.Result {}, nil
135
135
}
136
136
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
+
137
147
func (r * GrafanaReconciler ) syncStatuses (ctx context.Context ) error {
138
148
log := logf .FromContext (ctx )
139
149
140
150
// get all grafana instances
141
151
grafanas := & grafanav1beta1.GrafanaList {}
142
- var opts []client.ListOption
143
- err := r .List (ctx , grafanas , opts ... )
152
+ err := r .List (ctx , grafanas )
144
153
if err != nil {
145
154
return err
146
155
}
@@ -151,100 +160,61 @@ func (r *GrafanaReconciler) syncStatuses(ctx context.Context) error {
151
160
152
161
// folders
153
162
folders := & grafanav1beta1.GrafanaFolderList {}
154
- err = r .List (ctx , folders , opts ... )
163
+ err = r .List (ctx , folders )
155
164
if err != nil {
156
165
return err
157
166
}
158
167
159
168
// dashboards
160
169
dashboards := & grafanav1beta1.GrafanaDashboardList {}
161
- err = r .List (ctx , dashboards , opts ... )
170
+ err = r .List (ctx , dashboards )
162
171
if err != nil {
163
172
return err
164
173
}
165
174
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 )
169
178
if err != nil {
170
179
return err
171
180
}
172
181
173
182
// datasources
174
- datasource := & grafanav1beta1.GrafanaDatasourceList {}
175
- err = r .List (ctx , datasource , opts ... )
183
+ datasources := & grafanav1beta1.GrafanaDatasourceList {}
184
+ err = r .List (ctx , datasources )
176
185
if err != nil {
177
186
return err
178
187
}
179
188
180
189
// contact points
181
- allContactPoints := & grafanav1beta1.GrafanaContactPointList {}
182
- err = r .List (ctx , allContactPoints , opts ... )
190
+ contactPoints := & grafanav1beta1.GrafanaContactPointList {}
191
+ err = r .List (ctx , contactPoints )
183
192
if err != nil {
184
193
return err
185
194
}
186
195
187
196
// delete resources from grafana statuses that no longer have a CR
188
- statusesSynced := 0
197
+ statusUpdates := 0
189
198
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
209
200
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 )
236
206
237
- if statusUpdated {
238
- statusesSynced += 1
207
+ if updateStatus {
208
+ statusUpdates += 1
239
209
err = r .Client .Status ().Update (ctx , & grafana )
240
210
if err != nil {
241
211
return err
242
212
}
243
213
}
244
214
}
245
215
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 )
248
218
}
249
219
return nil
250
220
}
0 commit comments