Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set organization home dashboard #1301

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/v1beta1/grafana_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type GrafanaSpec struct {
Jsonnet *JsonnetConfig `json:"jsonnet,omitempty"`
// External enables you to configure external grafana instances that is not managed by the operator.
External *External `json:"external,omitempty"`
// Preferences holds the Grafana Preferences settings
Preferences *GrafanaPreferences `json:"preferences,omitempty"`
}

type External struct {
Expand All @@ -100,6 +102,11 @@ type GrafanaClient struct {
PreferIngress *bool `json:"preferIngress,omitempty"`
}

// GrafanaPreferences holds Grafana preferences API settings
type GrafanaPreferences struct {
HomeDashboardUID string `json:"homeDashboardUid,omitempty"`
}

// GrafanaStatus defines the observed state of Grafana
type GrafanaStatus struct {
Stage OperatorStageName `json:"stage,omitempty"`
Expand Down
20 changes: 20 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions bundle/manifests/grafana.integreatly.org_grafanas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3629,6 +3629,11 @@ spec:
type: string
type: object
type: object
preferences:
properties:
homeDashboardUid:
type: string
type: object
route:
properties:
metadata:
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/grafana.integreatly.org_grafanas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3629,6 +3629,11 @@ spec:
type: string
type: object
type: object
preferences:
properties:
homeDashboardUid:
type: string
type: object
route:
properties:
metadata:
Expand Down
6 changes: 6 additions & 0 deletions config/grafana.integreatly.org_grafanas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9028,6 +9028,12 @@ spec:
type: string
type: object
type: object
preferences:
description: Preferences holds the Grafana Preferences settings
properties:
homeDashboardUid:
type: string
type: object
route:
description: Route sets how the ingress object should look like with
your grafana instance, this only works in Openshift.
Expand Down
30 changes: 30 additions & 0 deletions controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
controllerLog.Error(err, "error reconciling dashboard", "dashboard", cr.Name, "grafana", grafana.Name)
success = false
}

if grafana.Spec.Preferences != nil && uid == grafana.Spec.Preferences.HomeDashboardUID {
err = r.UpdateHomeDashboard(ctx, grafana, uid, cr)
if err != nil {
return ctrl.Result{RequeueAfter: RequeueDelay}, err
}
}
}

// if the dashboard was successfully synced in all instances, wait for its re-sync period
Expand Down Expand Up @@ -736,3 +743,26 @@ func (r *GrafanaDashboardReconciler) GetMatchingDashboardInstances(ctx context.C

return instances, err
}

func (r *GrafanaDashboardReconciler) UpdateHomeDashboard(ctx context.Context, grafana v1beta1.Grafana, uid string, dashboard *v1beta1.GrafanaDashboard) error {
grafanaClient, err := client2.NewGrafanaClient(ctx, r.Client, &grafana)
if err != nil {
return err
}

p, err := grafanaClient.OrgPreferences()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a small bug in the grafana-api-go-client that makes us unable to use the PATCH endpoint, having to GET first and then PUT. I solved it in this PR, so I'm waiting for a new release to change this.

if err != nil {
r.Log.Error(err, "unable to fetch org preferences", "namespace", dashboard.Namespace, "name", dashboard.Name)
return err
}

p.HomeDashboardUID = uid
_, err = grafanaClient.UpdateAllOrgPreferences(p)
if err != nil {
r.Log.Error(err, "unable to update the home dashboard", "namespace", dashboard.Namespace, "name", dashboard.Name)
return err
}

r.Log.Info("home dashboard configured", "namespace", dashboard.Namespace, "name", dashboard.Name)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -3629,6 +3629,11 @@ spec:
type: string
type: object
type: object
preferences:
properties:
homeDashboardUid:
type: string
type: object
route:
properties:
metadata:
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 @@ -9703,6 +9703,12 @@ spec:
type: string
type: object
type: object
preferences:
description: Preferences holds the Grafana Preferences settings
properties:
homeDashboardUid:
type: string
type: object
route:
description: Route sets how the ingress object should look like with
your grafana instance, this only works in Openshift.
Expand Down
34 changes: 34 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,13 @@ GrafanaSpec defines the desired state of Grafana
PersistentVolumeClaim creates a PVC if you need to attach one to your grafana instance.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#grafanaspecpreferences">preferences</a></b></td>
<td>object</td>
<td>
Preferences holds the Grafana Preferences settings<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#grafanaspecroute">route</a></b></td>
<td>object</td>
Expand Down Expand Up @@ -15521,6 +15528,33 @@ A label selector requirement is a selector that contains values, a key, and an o
</table>


### Grafana.spec.preferences
<sup><sup>[↩ Parent](#grafanaspec)</sup></sup>



Preferences holds the Grafana Preferences settings

<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b>homeDashboardUid</b></td>
<td>string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>


### Grafana.spec.route
<sup><sup>[↩ Parent](#grafanaspec)</sup></sup>

Expand Down