Skip to content

Commit b05453a

Browse files
committed
chore: call getFolderUID once before looping instances
1 parent c4214ac commit b05453a

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed

controllers/alertrulegroup_controller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ func (r *GrafanaAlertRuleGroupReconciler) Reconcile(ctx context.Context, req ctr
114114
log.Info("found matching Grafana instances for group", "count", len(instances))
115115

116116
folderUID, err := getFolderUID(ctx, r.Client, group)
117-
if err != nil || folderUID == "" {
118-
return ctrl.Result{}, fmt.Errorf("folder uid not found: %w", err)
117+
if err != nil {
118+
return ctrl.Result{}, fmt.Errorf(ErrFetchingFolder, err)
119+
}
120+
121+
if folderUID == "" {
122+
return ctrl.Result{}, fmt.Errorf("folder uid not found, alert rule must reference a folder")
119123
}
120124

121125
editable := "true" //nolint:goconst

controllers/controller_shared.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const (
5151
)
5252

5353
var ErrNoMatchingInstances = fmt.Errorf("no matching instances")
54+
var ErrFetchingFolder = "fetching folder to resolve uid: %w"
5455

5556
//+kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;update;patch;delete
5657

@@ -125,7 +126,7 @@ func GetScopedMatchingInstances(ctx context.Context, k8sClient client.Client, cr
125126
return selectedList, nil
126127
}
127128

128-
// getFolderUID fetches the folderUID from an existing GrafanaFolder CR declared in the specified namespace
129+
// getFolderUID returns the folderUID from an existing GrafanaFolder CR within the same namespace
129130
func getFolderUID(ctx context.Context, k8sClient client.Client, ref operatorapi.FolderReferencer) (string, error) {
130131
if ref.FolderUID() != "" {
131132
return ref.FolderUID(), nil

controllers/dashboard_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
157157
return ctrl.Result{Requeue: true}, nil
158158
}
159159

160+
folderUID, err := getFolderUID(ctx, r.Client, cr)
161+
if err != nil {
162+
return ctrl.Result{}, fmt.Errorf(ErrFetchingFolder, err)
163+
}
164+
160165
applyHomeErrors := make(map[string]string)
161166
pluginErrors := make(map[string]string)
162167
applyErrors := make(map[string]string)
@@ -174,7 +179,7 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
174179
}
175180

176181
// then import the dashboard into the matching grafana instances
177-
err = r.onDashboardCreated(ctx, &grafana, cr, dashboardModel, hash)
182+
err = r.onDashboardCreated(ctx, &grafana, cr, dashboardModel, hash, folderUID)
178183
if err != nil {
179184
applyErrors[fmt.Sprintf("%s/%s", grafana.Namespace, grafana.Name)] = err.Error()
180185
}
@@ -292,7 +297,7 @@ func (r *GrafanaDashboardReconciler) finalize(ctx context.Context, cr *v1beta1.G
292297
return nil
293298
}
294299

295-
func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, grafana *v1beta1.Grafana, cr *v1beta1.GrafanaDashboard, dashboardModel map[string]any, hash string) error {
300+
func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, grafana *v1beta1.Grafana, cr *v1beta1.GrafanaDashboard, dashboardModel map[string]any, hash, folderUID string) error {
296301
log := logf.FromContext(ctx)
297302

298303
if grafana.IsExternal() && cr.Spec.Plugins != nil {
@@ -304,11 +309,6 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra
304309
return fmt.Errorf("creating grafana http client: %w", err)
305310
}
306311

307-
folderUID, err := getFolderUID(ctx, r.Client, cr)
308-
if err != nil {
309-
return err
310-
}
311-
312312
if folderUID == "" {
313313
folderUID, err = r.GetOrCreateFolder(grafanaClient, cr)
314314
if err != nil {

controllers/folder_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,17 @@ func (r *GrafanaFolderReconciler) Reconcile(ctx context.Context, req ctrl.Reques
122122
removeNoMatchingInstance(&folder.Status.Conditions)
123123
folder.Status.NoMatchingInstances = false
124124

125+
parentFolderUID, err := getFolderUID(ctx, r.Client, folder)
126+
if err != nil {
127+
return ctrl.Result{}, fmt.Errorf(ErrFetchingFolder, err)
128+
}
129+
125130
log.Info("found matching Grafana instances for folder", "count", len(instances))
126131

127132
applyErrors := make(map[string]string)
128133

129134
for _, grafana := range instances {
130-
err = r.onFolderCreated(ctx, &grafana, folder)
135+
err = r.onFolderCreated(ctx, &grafana, folder, parentFolderUID)
131136
if err != nil {
132137
applyErrors[fmt.Sprintf("%s/%s", grafana.Namespace, grafana.Name)] = err.Error()
133138
}
@@ -183,7 +188,7 @@ func (r *GrafanaFolderReconciler) finalize(ctx context.Context, folder *grafanav
183188
return nil
184189
}
185190

186-
func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *grafanav1beta1.Grafana, cr *grafanav1beta1.GrafanaFolder) error {
191+
func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *grafanav1beta1.Grafana, cr *grafanav1beta1.GrafanaFolder, parentFolderUID string) error {
187192
log := logf.FromContext(ctx)
188193

189194
title := cr.GetTitle()
@@ -194,11 +199,6 @@ func (r *GrafanaFolderReconciler) onFolderCreated(ctx context.Context, grafana *
194199
return err
195200
}
196201

197-
parentFolderUID, err := getFolderUID(ctx, r.Client, cr)
198-
if err != nil {
199-
return err
200-
}
201-
202202
exists, remoteUID, remoteParent, err := r.Exists(grafanaClient, cr)
203203
if err != nil {
204204
return err

controllers/librarypanel_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,15 @@ func (r *GrafanaLibraryPanelReconciler) Reconcile(ctx context.Context, req ctrl.
151151
removeNoMatchingInstance(&libraryPanel.Status.Conditions)
152152
log.Info("found matching Grafana instances for library panel", "count", len(instances))
153153

154+
folderUID, err := getFolderUID(ctx, r.Client, libraryPanel)
155+
if err != nil {
156+
return ctrl.Result{}, fmt.Errorf(ErrFetchingFolder, err)
157+
}
158+
154159
applyErrors := make(map[string]string)
155160

156161
for _, grafana := range instances {
157-
err := r.reconcileWithInstance(ctx, &grafana, libraryPanel, contentModel, hash)
162+
err := r.reconcileWithInstance(ctx, &grafana, libraryPanel, contentModel, hash, folderUID)
158163
if err != nil {
159164
applyErrors[fmt.Sprintf("%s/%s", grafana.Namespace, grafana.Name)] = err.Error()
160165
}
@@ -170,7 +175,7 @@ func (r *GrafanaLibraryPanelReconciler) Reconcile(ctx context.Context, req ctrl.
170175
return ctrl.Result{RequeueAfter: libraryPanel.Spec.ResyncPeriod.Duration}, nil
171176
}
172177

173-
func (r *GrafanaLibraryPanelReconciler) reconcileWithInstance(ctx context.Context, instance *v1beta1.Grafana, cr *v1beta1.GrafanaLibraryPanel, model map[string]any, hash string) error {
178+
func (r *GrafanaLibraryPanelReconciler) reconcileWithInstance(ctx context.Context, instance *v1beta1.Grafana, cr *v1beta1.GrafanaLibraryPanel, model map[string]any, hash, folderUID string) error {
174179
if instance.IsInternal() {
175180
err := ReconcilePlugins(ctx, r.Client, r.Scheme, instance, cr.Spec.Plugins, fmt.Sprintf("%v-librarypanel", cr.Name))
176181
if err != nil {
@@ -185,11 +190,6 @@ func (r *GrafanaLibraryPanelReconciler) reconcileWithInstance(ctx context.Contex
185190
return err
186191
}
187192

188-
folderUID, err := getFolderUID(ctx, r.Client, cr)
189-
if err != nil {
190-
return err
191-
}
192-
193193
uid := content.CustomUIDOrUID(cr, fmt.Sprintf("%s", model["uid"]))
194194
name := fmt.Sprintf("%s", model["name"])
195195

0 commit comments

Comments
 (0)