From 203ff7ca630eb4f15000a5a7b9b3f75522f41c58 Mon Sep 17 00:00:00 2001 From: Adrien Boulay Date: Tue, 2 Jul 2024 17:16:47 +0200 Subject: [PATCH] feat: Add folderUID and folderRef in GrafanaDashboard CRD --- api/v1beta1/grafanadashboard_types.go | 8 +++++ ...ana.integreatly.org_grafanadashboards.yaml | 6 ++++ controllers/dashboard_controller.go | 36 +++++++++++++++++-- ...ana.integreatly.org_grafanadashboards.yaml | 6 ++++ deploy/kustomize/base/crds.yaml | 6 ++++ docs/docs/api.md | 14 ++++++++ 6 files changed, 74 insertions(+), 2 deletions(-) diff --git a/api/v1beta1/grafanadashboard_types.go b/api/v1beta1/grafanadashboard_types.go index cdd658876..74392407a 100644 --- a/api/v1beta1/grafanadashboard_types.go +++ b/api/v1beta1/grafanadashboard_types.go @@ -85,6 +85,14 @@ type GrafanaDashboardSpec struct { // +optional FolderTitle string `json:"folder,omitempty"` + // find parent folder for dashboard + // +optional + FolderUID string `json:"folderUID" validate:"omitempty,uuid4"` + + // find parent folder using GrafanaFolder CR reference + // +optional + FolderRef string `json:"folderRef,omitempty"` + // plugins // +optional Plugins PluginList `json:"plugins,omitempty"` diff --git a/config/crd/bases/grafana.integreatly.org_grafanadashboards.yaml b/config/crd/bases/grafana.integreatly.org_grafanadashboards.yaml index 7dd9669a3..eebd2ae17 100644 --- a/config/crd/bases/grafana.integreatly.org_grafanadashboards.yaml +++ b/config/crd/bases/grafana.integreatly.org_grafanadashboards.yaml @@ -224,6 +224,12 @@ spec: folder: description: folder assignment for dashboard type: string + folderRef: + description: find parent folder using GrafanaFolder CR reference + type: string + folderUID: + description: find parent folder for dashboard + type: string grafanaCom: description: grafana.com/dashboards properties: diff --git a/controllers/dashboard_controller.go b/controllers/dashboard_controller.go index 1bd4b4669..33bc7d2e8 100644 --- a/controllers/dashboard_controller.go +++ b/controllers/dashboard_controller.go @@ -370,9 +370,9 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra return err } - folderUID, err := r.GetOrCreateFolder(grafanaClient, cr) + folderUID, err := r.retrieveFolderUID(ctx, grafanaClient, cr) if err != nil { - return kuberr.NewInternalError(err) + return err } uid := fmt.Sprintf("%s", dashboardModel["uid"]) @@ -429,6 +429,38 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra return r.Client.Status().Update(ctx, grafana) } +func (r *GrafanaDashboardReconciler) retrieveFolderUID(ctx context.Context, grafanaClient *genapi.GrafanaHTTPAPI, cr *v1beta1.GrafanaDashboard) (string, error) { + if cr.Spec.FolderRef != "" && cr.Spec.FolderUID != "" { + return "", fmt.Errorf("error folderRef and folderUID cannot be declared at the same time in the CR %s (%s)", cr.Name, cr.Namespace) + } + + if cr.Spec.FolderRef != "" { + if cr.Spec.FolderTitle != "" { + r.Log.Info(fmt.Sprintf("warning folder and folderRef cannot be set at the same time. Ignoring folder field in %s (%s)", cr.Name, cr.Namespace)) + } + + folder := &v1beta1.GrafanaFolder{} + + err := r.Client.Get(ctx, client.ObjectKey{ + Namespace: cr.Namespace, + Name: cr.Spec.FolderRef, + }, folder) + if err != nil { + return "", err + } + + return string(folder.ObjectMeta.UID), nil + } + if cr.Spec.FolderUID != "" { + if cr.Spec.FolderTitle != "" { + r.Log.Info(fmt.Sprintf("warning folder and folderUID cannot be set at the same time. Ignoring folder field in %s (%s)", cr.Name, cr.Namespace)) + } + return cr.Spec.FolderUID, nil + } + + return r.GetOrCreateFolder(grafanaClient, cr) +} + // map data sources that are required in the dashboard to data sources that exist in the instance func (r *GrafanaDashboardReconciler) resolveDatasources(dashboard *v1beta1.GrafanaDashboard, dashboardJson []byte) ([]byte, error) { if len(dashboard.Spec.Datasources) == 0 { diff --git a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanadashboards.yaml b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanadashboards.yaml index 7dd9669a3..eebd2ae17 100644 --- a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanadashboards.yaml +++ b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanadashboards.yaml @@ -224,6 +224,12 @@ spec: folder: description: folder assignment for dashboard type: string + folderRef: + description: find parent folder using GrafanaFolder CR reference + type: string + folderUID: + description: find parent folder for dashboard + type: string grafanaCom: description: grafana.com/dashboards properties: diff --git a/deploy/kustomize/base/crds.yaml b/deploy/kustomize/base/crds.yaml index af12391d1..539d97691 100644 --- a/deploy/kustomize/base/crds.yaml +++ b/deploy/kustomize/base/crds.yaml @@ -749,6 +749,12 @@ spec: folder: description: folder assignment for dashboard type: string + folderRef: + description: find parent folder using GrafanaFolder CR reference + type: string + folderUID: + description: find parent folder for dashboard + type: string grafanaCom: description: grafana.com/dashboards properties: diff --git a/docs/docs/api.md b/docs/docs/api.md index 9372ac6e4..c65923ab9 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -1087,6 +1087,20 @@ GrafanaDashboardSpec defines the desired state of GrafanaDashboard folder assignment for dashboard
false + + folderRef + string + + find parent folder using GrafanaFolder CR reference
+ + false + + folderUID + string + + find parent folder for dashboard
+ + false grafanaCom object