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

fix: more than one source types found for dashboard #991

Merged
merged 4 commits into from
Apr 16, 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
8 changes: 4 additions & 4 deletions api/v1beta1/grafanadashboard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ type GrafanaDashboardList struct {
Items []GrafanaDashboard `json:"items"`
}

func (in *GrafanaDashboard) Hash() string {
func (in *GrafanaDashboard) Hash(dashboardJson []byte) string {
hash := sha256.New()
hash.Write([]byte(in.Spec.Json))
hash.Write(dashboardJson)
return fmt.Sprintf("%x", hash.Sum(nil))
}

func (in *GrafanaDashboard) Unchanged() bool {
return in.Hash() == in.Status.Hash
func (in *GrafanaDashboard) Unchanged(hash string) bool {
return in.Status.Hash == hash
}

func (in *GrafanaDashboard) GetResyncPeriod() time.Duration {
Expand Down
19 changes: 8 additions & 11 deletions controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra
return err
}

// NOTE: previously, we had hash calculated directly from CR, though, with growing number of fetchers,
// it's easier to do it here to make sure it's always the right set of data that's used
hash := cr.Hash(dashboardJson)

dashboardJson, err = r.resolveDatasources(cr, dashboardJson)
if err != nil {
return err
Expand All @@ -302,10 +306,6 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra
return fmt.Errorf("external grafana instances don't support plugins, please remove spec.plugins from your dashboard cr")
}

// Dashboards come from different sources, whereas Spec.Json is used to calculate hash
// So, we should keep the field updated to make sure changes in dashboards get noticed
cr.Spec.Json = string(dashboardJson)

grafanaClient, err := client2.NewGrafanaClient(ctx, r.Client, grafana)
if err != nil {
return err
Expand All @@ -316,7 +316,7 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra
if err != nil {
return err
}
if exists && cr.Unchanged() {
if exists && cr.Unchanged(hash) {
return nil
}

Expand Down Expand Up @@ -357,7 +357,9 @@ func (r *GrafanaDashboardReconciler) onDashboardCreated(ctx context.Context, gra
return err
}

return r.UpdateStatus(ctx, cr)
cr.Status.Hash = hash

return r.Client.Status().Update(ctx, cr)
}

// map data sources that are required in the dashboard to data sources that exist in the instance
Expand Down Expand Up @@ -407,11 +409,6 @@ func (r *GrafanaDashboardReconciler) fetchDashboardJson(dashboard *v1beta1.Grafa
}
}

func (r *GrafanaDashboardReconciler) UpdateStatus(ctx context.Context, cr *v1beta1.GrafanaDashboard) error {
cr.Status.Hash = cr.Hash()
return r.Client.Status().Update(ctx, cr)
}

func (r *GrafanaDashboardReconciler) Exists(client *grapi.Client, cr *v1beta1.GrafanaDashboard) (bool, error) {
dashboards, err := client.Dashboards()
if err != nil {
Expand Down