From 473c0b5e89b39fea282998060947684830bba1cf Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Fri, 7 Mar 2025 17:42:20 -0700 Subject: [PATCH] K8s: Fix plugin updater (#101843) --- pkg/registry/backgroundsvcs/background_services.go | 4 +++- .../plugindashboards/service/dashboard_updater.go | 14 ++++++++++---- .../service/dashboard_updater_test.go | 12 ++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pkg/registry/backgroundsvcs/background_services.go b/pkg/registry/backgroundsvcs/background_services.go index 4c6bb02f89d..b0f339ed29d 100644 --- a/pkg/registry/backgroundsvcs/background_services.go +++ b/pkg/registry/backgroundsvcs/background_services.go @@ -68,10 +68,11 @@ func ProvideBackgroundServiceRegistry( pluginInstaller *plugininstaller.Service, zanzanaReconciler *dualwrite.ZanzanaReconciler, appRegistry *appregistry.Service, + pluginDashboardUpdater *plugindashboardsservice.DashboardUpdater, // Need to make sure these are initialized, is there a better place to put them? _ dashboardsnapshots.Service, _ serviceaccounts.Service, _ *guardian.Provider, - _ *plugindashboardsservice.DashboardUpdater, _ *sanitizer.Provider, + _ *sanitizer.Provider, _ *grpcserver.HealthService, _ *grpcserver.ReflectionService, _ *ldapapi.Service, _ *apiregistry.Service, _ auth.IDService, _ *teamapi.TeamAPI, _ ssosettings.Service, _ cloudmigration.Service, _ authnimpl.Registration, @@ -113,6 +114,7 @@ func ProvideBackgroundServiceRegistry( pluginInstaller, zanzanaReconciler, appRegistry, + pluginDashboardUpdater, ) } diff --git a/pkg/services/plugindashboards/service/dashboard_updater.go b/pkg/services/plugindashboards/service/dashboard_updater.go index 7cf8bc8c46d..efcc49c0545 100644 --- a/pkg/services/plugindashboards/service/dashboard_updater.go +++ b/pkg/services/plugindashboards/service/dashboard_updater.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/services/accesscontrol" @@ -20,7 +21,6 @@ func ProvideDashboardUpdater(bus bus.Bus, pluginStore pluginstore.Store, pluginD dashboardPluginService dashboards.PluginService, dashboardService dashboards.DashboardService) *DashboardUpdater { du := newDashboardUpdater(bus, pluginStore, pluginDashboardService, dashboardImportService, pluginSettingsService, dashboardPluginService, dashboardService) - du.updateAppDashboards() return du } @@ -52,7 +52,12 @@ type DashboardUpdater struct { logger log.Logger } -func (du *DashboardUpdater) updateAppDashboards() { +func (du *DashboardUpdater) Run(ctx context.Context) error { + du.updateAppDashboards(ctx) + return nil +} + +func (du *DashboardUpdater) updateAppDashboards(ctx context.Context) { du.logger.Debug("Looking for app dashboard updates") pluginSettings, err := du.pluginSettingsService.GetPluginSettings(context.Background(), &pluginsettings.GetArgs{OrgID: 0}) @@ -67,9 +72,10 @@ func (du *DashboardUpdater) updateAppDashboards() { continue } - if pluginDef, exists := du.pluginStore.Plugin(context.Background(), pluginSetting.PluginID); exists { + serviceCtx, _ := identity.WithServiceIdentity(ctx, pluginSetting.OrgID) + if pluginDef, exists := du.pluginStore.Plugin(serviceCtx, pluginSetting.PluginID); exists { if pluginDef.Info.Version != pluginSetting.PluginVersion { - du.syncPluginDashboards(context.Background(), pluginDef, pluginSetting.OrgID) + du.syncPluginDashboards(serviceCtx, pluginDef, pluginSetting.OrgID) } } } diff --git a/pkg/services/plugindashboards/service/dashboard_updater_test.go b/pkg/services/plugindashboards/service/dashboard_updater_test.go index e92fa23ed11..8f65d0f7f49 100644 --- a/pkg/services/plugindashboards/service/dashboard_updater_test.go +++ b/pkg/services/plugindashboards/service/dashboard_updater_test.go @@ -23,7 +23,7 @@ func TestDashboardUpdater(t *testing.T) { t.Run("updateAppDashboards", func(t *testing.T) { scenario(t, "Without any stored plugin settings shouldn't delete/import any dashboards", scenarioInput{}, func(ctx *scenarioContext) { - ctx.dashboardUpdater.updateAppDashboards() + ctx.dashboardUpdater.updateAppDashboards(context.Background()) require.Len(t, ctx.pluginSettingsService.getPluginSettingsArgs, 1) require.Equal(t, int64(0), ctx.pluginSettingsService.getPluginSettingsArgs[0]) @@ -46,7 +46,7 @@ func TestDashboardUpdater(t *testing.T) { }, }, }, func(ctx *scenarioContext) { - ctx.dashboardUpdater.updateAppDashboards() + ctx.dashboardUpdater.updateAppDashboards(context.Background()) require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs) require.Empty(t, ctx.dashboardService.deleteDashboardArgs) @@ -68,7 +68,7 @@ func TestDashboardUpdater(t *testing.T) { }, }, }, func(ctx *scenarioContext) { - ctx.dashboardUpdater.updateAppDashboards() + ctx.dashboardUpdater.updateAppDashboards(context.Background()) require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs) require.Empty(t, ctx.dashboardService.deleteDashboardArgs) @@ -100,7 +100,7 @@ func TestDashboardUpdater(t *testing.T) { }, }, }, func(ctx *scenarioContext) { - ctx.dashboardUpdater.updateAppDashboards() + ctx.dashboardUpdater.updateAppDashboards(context.Background()) require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs) require.Empty(t, ctx.dashboardService.deleteDashboardArgs) @@ -135,7 +135,7 @@ func TestDashboardUpdater(t *testing.T) { }, }, }, func(ctx *scenarioContext) { - ctx.dashboardUpdater.updateAppDashboards() + ctx.dashboardUpdater.updateAppDashboards(context.Background()) require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs) require.Empty(t, ctx.dashboardService.deleteDashboardArgs) @@ -183,7 +183,7 @@ func TestDashboardUpdater(t *testing.T) { }, }, }, func(ctx *scenarioContext) { - ctx.dashboardUpdater.updateAppDashboards() + ctx.dashboardUpdater.updateAppDashboards(context.Background()) require.NotEmpty(t, ctx.pluginSettingsService.getPluginSettingsArgs) require.Len(t, ctx.dashboardService.deleteDashboardArgs, 1)