Skip to content

Commit

Permalink
feat: Add folderUID and folderRef in GrafanaDashboard CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
aboulay-numspot committed Jul 4, 2024
1 parent fc7a4d3 commit 203ff7c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/v1beta1/grafanadashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 34 additions & 2 deletions controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,20 @@ GrafanaDashboardSpec defines the desired state of GrafanaDashboard
folder assignment for dashboard<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>folderRef</b></td>
<td>string</td>
<td>
find parent folder using GrafanaFolder CR reference<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>folderUID</b></td>
<td>string</td>
<td>
find parent folder for dashboard<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#grafanadashboardspecgrafanacom">grafanaCom</a></b></td>
<td>object</td>
Expand Down

0 comments on commit 203ff7c

Please sign in to comment.