Skip to content

Commit

Permalink
address theo's remarks, round 3: better dashboards unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
hervenicol committed Dec 20, 2024
1 parent 618382d commit 32848bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/giantswarm/apiextensions-application v0.6.2
github.com/go-logr/logr v1.4.2
github.com/go-openapi/strfmt v0.23.0
github.com/grafana/grafana-openapi-client-go v0.0.0-20241126111151-59d2d35e24eb
github.com/onsi/ginkgo/v2 v2.22.1
github.com/onsi/gomega v1.36.1
Expand Down Expand Up @@ -99,6 +98,7 @@ require (
github.com/go-openapi/loads v0.22.0 // indirect
github.com/go-openapi/runtime v0.28.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/strfmt v0.23.0 // indirect
github.com/go-openapi/validate v0.24.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
47 changes: 21 additions & 26 deletions internal/controller/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/go-openapi/strfmt"
grafanaAPI "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/pkg/errors"
Expand Down Expand Up @@ -187,15 +185,9 @@ func (r DashboardReconciler) reconcileCreate(ctx context.Context, dashboard *v1.
return ctrl.Result{}, nil
}

func getDashboardUID(jsonDashboard string) (string, error) {
func getDashboardUID(dashboard map[string]interface{}) (string, error) {

var Dashboard map[string]interface{}
err := json.Unmarshal([]byte(jsonDashboard), &Dashboard)
if err != nil {
return "", err
}

UID, ok := Dashboard["uid"].(string)
UID, ok := dashboard["uid"].(string)
if !ok {
return "", errors.New("dashboard UID not found in configmap")
}
Expand Down Expand Up @@ -236,7 +228,15 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM
}
}()

for _, dashboard := range dashboardCM.Data {
for _, dashboardString := range dashboardCM.Data {

var dashboard map[string]any
err = json.Unmarshal([]byte(dashboardString), &dashboard)
if err != nil {
logger.Info("Failed converting dashboard to json")
return errors.WithStack(err)
}

dashboardUID, err := getDashboardUID(dashboard)
if err != nil {
logger.Info("Skipping dashboard, no UID found")
Expand All @@ -254,23 +254,11 @@ func (r DashboardReconciler) configureDashboard(ctx context.Context, dashboardCM
return errors.WithStack(err)
}

var dashboardjson interface{}
err = json.Unmarshal([]byte(dashboard), &dashboardjson)
if err != nil {
logger.Info("Failed converting dashboard to json")
return errors.WithStack(err)
}

// Create or update dashboard
_, err = r.GrafanaAPI.Dashboards.PostDashboard(&models.SaveDashboardCommand{
UpdatedAt: strfmt.DateTime(time.Now()),
Dashboard: dashboardjson,
FolderID: 0,
FolderUID: "",
IsFolder: false,
Dashboard: any(dashboard),
Message: "Added by observability-operator",
Overwrite: true,
UserID: 0,
Overwrite: true, // allows dashboard to be updated by the same UID
})
if err != nil {
logger.Info("Failed updating dashboard")
Expand Down Expand Up @@ -298,7 +286,14 @@ func (r DashboardReconciler) reconcileDelete(ctx context.Context, dashboardCM *v
return nil
}

for _, dashboard := range dashboardCM.Data {
for _, dashboardString := range dashboardCM.Data {
var dashboard map[string]interface{}
err = json.Unmarshal([]byte(dashboardString), &dashboard)
if err != nil {
logger.Info("Failed converting dashboard to json")
return errors.WithStack(err)
}

dashboardUID, err := getDashboardUID(dashboard)
if err != nil {
logger.Info("Skipping dashboard, no UID found")
Expand Down

0 comments on commit 32848bd

Please sign in to comment.