From 745eda28480d6c83ee7ff15890e0db9818d39d76 Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Sun, 25 May 2025 05:18:49 -0500 Subject: [PATCH] Dashboard Provisioning: Fix re-provisioning on each run (#105979) Dashboard Provisioning: Fix reprovisioning on startup --- .../provisioning/dashboards/file_reader.go | 11 ++++++++-- .../dashboards/file_reader_test.go | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/pkg/services/provisioning/dashboards/file_reader.go b/pkg/services/provisioning/dashboards/file_reader.go index 8db4fbc3c80..593438a0f7e 100644 --- a/pkg/services/provisioning/dashboards/file_reader.go +++ b/pkg/services/provisioning/dashboards/file_reader.go @@ -101,7 +101,7 @@ func (fr *FileReader) walkDisk(ctx context.Context) error { return err } - provisionedDashboardRefs, err := getProvisionedDashboardsByPath(ctx, fr.dashboardProvisioningService, fr.Cfg.Name) + provisionedDashboardRefs, err := fr.getProvisionedDashboardsByPath(ctx, fr.dashboardProvisioningService, fr.Cfg.Name) if err != nil { return err } @@ -326,7 +326,7 @@ func (fr *FileReader) saveDashboard(ctx context.Context, path string, folderID i return provisioningMetadata, nil } -func getProvisionedDashboardsByPath(ctx context.Context, service dashboards.DashboardProvisioningService, name string) ( +func (fr *FileReader) getProvisionedDashboardsByPath(ctx context.Context, service dashboards.DashboardProvisioningService, name string) ( map[string]*dashboards.DashboardProvisioning, error) { arr, err := service.GetProvisionedDashboardData(ctx, name) if err != nil { @@ -335,6 +335,13 @@ func getProvisionedDashboardsByPath(ctx context.Context, service dashboards.Dash byPath := map[string]*dashboards.DashboardProvisioning{} for _, pd := range arr { + // as a part of the migration of dashboards to unified storage, the dashboard provisiong data will be stored as + // an annotation on the dashboard. in modes 0-2, that will only return the relative path. however, we will be comparing + // that to the data stored in the dashboard_provisioning table, so we need to change it into the resolved path + if !strings.HasPrefix(pd.ExternalID, fr.resolvedPath()) { + pd.ExternalID = fr.resolvedPath() + "/" + pd.ExternalID + } + byPath[pd.ExternalID] = pd } diff --git a/pkg/services/provisioning/dashboards/file_reader_test.go b/pkg/services/provisioning/dashboards/file_reader_test.go index 22e822a138c..0209fa7ef67 100644 --- a/pkg/services/provisioning/dashboards/file_reader_test.go +++ b/pkg/services/provisioning/dashboards/file_reader_test.go @@ -456,6 +456,27 @@ func TestDashboardFileReader(t *testing.T) { }) }) + t.Run("Should resolve relative ExternalID paths to absolute paths", func(t *testing.T) { + setup() + cfg.Options["path"] = defaultDashboards + provisionedDashboard := []*dashboards.DashboardProvisioning{ + { + Name: configName, + ExternalID: "dashboard1.json", + }, + } + + fakeService.On("GetProvisionedDashboardData", mock.Anything, configName).Return(provisionedDashboard, nil).Once() + reader, err := NewDashboardFileReader(cfg, logger, nil, fakeStore, folderSvc) + reader.dashboardProvisioningService = fakeService + require.NoError(t, err) + resolvedPath := reader.resolvedPath() + dashboards, err := reader.getProvisionedDashboardsByPath(context.Background(), fakeService, configName) + require.NoError(t, err) + expectedPath := filepath.Join(resolvedPath, "dashboard1.json") + require.Equal(t, expectedPath, dashboards[expectedPath].ExternalID) + }) + t.Run("Given missing dashboard file", func(t *testing.T) { absPath1, err := filepath.Abs(unprovision + "/dashboard1.json") require.NoError(t, err)