From 401aabd391c59d9e713f128a572d9d21c1458ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Mon, 20 Mar 2023 14:11:26 +0100 Subject: [PATCH] Fix the converter to use the proper annotation for folders --- .../controllers/grafanadashboard_controller.go | 4 ++-- internal/pkg/converter/json.go | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/pkg/controllers/grafanadashboard_controller.go b/internal/pkg/controllers/grafanadashboard_controller.go index 388fe665..9a9464fa 100644 --- a/internal/pkg/controllers/grafanadashboard_controller.go +++ b/internal/pkg/controllers/grafanadashboard_controller.go @@ -17,7 +17,7 @@ import ( ) const grafanaDashboardFinalizerName = "grafanadashboards.k8s.kevingomez.fr/finalizer" -const folderAnnotation = "dark/folder" +const DashboardFolderAnnotation = "dark/folder" type dashboardManager interface { FromRawSpec(ctx context.Context, folderName string, uid string, rawJSON []byte) error @@ -103,7 +103,7 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req return ctrl.Result{}, nil } - folder := dashboard.Annotations[folderAnnotation] + folder := dashboard.Annotations[DashboardFolderAnnotation] if dashboard.Folder != "" { folder = dashboard.Folder } diff --git a/internal/pkg/converter/json.go b/internal/pkg/converter/json.go index d9905ca2..1e466076 100644 --- a/internal/pkg/converter/json.go +++ b/internal/pkg/converter/json.go @@ -5,19 +5,18 @@ import ( "fmt" "io" + v1 "github.com/K-Phoen/dark/api/v1" + "github.com/K-Phoen/dark/internal/pkg/controllers" grabana "github.com/K-Phoen/grabana/decoder" "github.com/K-Phoen/sdk" "go.uber.org/zap" "gopkg.in/yaml.v3" - - v1 "github.com/K-Phoen/dark/api/v1" ) type k8sDashboard struct { APIVersion string `yaml:"apiVersion"` Kind string - Metadata map[string]string - Folder string + Metadata map[string]interface{} Spec *grabana.DashboardModel } @@ -81,8 +80,7 @@ func (converter *JSON) ToK8SManifest(input io.Reader, output io.Writer, options manifest := k8sDashboard{ APIVersion: v1.GroupVersion.String(), Kind: "GrafanaDashboard", - Metadata: map[string]string{"name": options.Name}, - Folder: options.Folder, + Metadata: map[string]interface{}{"name": options.Name}, Spec: dashboard, } @@ -90,6 +88,12 @@ func (converter *JSON) ToK8SManifest(input io.Reader, output io.Writer, options manifest.Metadata["namespace"] = options.Namespace } + if options.Folder != "" { + manifest.Metadata["annotations"] = map[string]string{ + controllers.DashboardFolderAnnotation: options.Folder, + } + } + converted, err := yaml.Marshal(manifest) if err != nil { converter.logger.Error("could marshall dashboard to yaml", zap.Error(err))