Dashboards: Don't run cleanup job when backed by legacy storage (#104001)

Don't run dashboard cleanup job when backed by legacy storage

Co-authored-by: Marco de Abreu <18629099+marcoabreu@users.noreply.github.com>
This commit is contained in:
Marco de Abreu
2025-04-15 22:43:05 +02:00
committed by GitHub
parent 163a8d5f0c
commit 2a61763703
2 changed files with 32 additions and 11 deletions
@@ -95,6 +95,7 @@ type DashboardServiceImpl struct {
publicDashboardService publicdashboards.ServiceWrapper
serverLockService *serverlock.ServerLockService
kvstore kvstore.KVStore
dual dualwrite.Service
dashboardPermissionsReady chan struct{}
}
@@ -149,6 +150,12 @@ func (dr *DashboardServiceImpl) cleanupK8sDashboardResources(ctx context.Context
return nil
}
readingFromLegacy := dualwrite.IsReadingLegacyDashboardsAndFolders(ctx, dr.dual)
if readingFromLegacy {
// Legacy does its own cleanup
return nil
}
// Create a timeout context to ensure we complete before the lock expires
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
@@ -393,6 +400,7 @@ func ProvideDashboardServiceImpl(
publicDashboardService: publicDashboardService,
serverLockService: serverLockService,
kvstore: kvstore,
dual: dual,
}
defaultLimits, err := readQuotaConfig(cfg)
@@ -42,6 +42,7 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/storage/legacysql/dualwrite"
"github.com/grafana/grafana/pkg/storage/unified/resource"
"golang.org/x/exp/slices"
)
@@ -2595,11 +2596,12 @@ func TestCleanUpDashboard(t *testing.T) {
func TestK8sDashboardCleanupJob(t *testing.T) {
tests := []struct {
name string
featureEnabled bool
batchSize int
setupFunc func(*DashboardServiceImpl, context.Context, *client.MockK8sHandler)
verifyFunc func(*testing.T, *DashboardServiceImpl, context.Context, *client.MockK8sHandler, *kvstore.FakeKVStore)
name string
featureEnabled bool
readFromUnified bool
batchSize int
setupFunc func(*DashboardServiceImpl, context.Context, *client.MockK8sHandler)
verifyFunc func(*testing.T, *DashboardServiceImpl, context.Context, *client.MockK8sHandler, *kvstore.FakeKVStore)
}{
{
name: "Should not run cleanup when feature flag is disabled",
@@ -2607,9 +2609,16 @@ func TestK8sDashboardCleanupJob(t *testing.T) {
batchSize: 10,
},
{
name: "Should process dashboard cleanup for all orgs",
featureEnabled: true,
batchSize: 10,
name: "Should not run cleanup when feature flag is enabled but we're reading from legacy",
featureEnabled: true,
readFromUnified: false,
batchSize: 10,
},
{
name: "Should process dashboard cleanup for all orgs",
featureEnabled: true,
readFromUnified: true,
batchSize: 10,
setupFunc: func(service *DashboardServiceImpl, ctx context.Context, k8sCliMock *client.MockK8sHandler) {
// Test organizations
fakeOrgService := service.orgService.(*orgtest.FakeOrgService)
@@ -2677,9 +2686,10 @@ func TestK8sDashboardCleanupJob(t *testing.T) {
},
},
{
name: "Should handle pagination and batching when processing large sets of dashboards",
featureEnabled: true,
batchSize: 3,
name: "Should handle pagination and batching when processing large sets of dashboards",
featureEnabled: true,
readFromUnified: true,
batchSize: 3,
setupFunc: func(service *DashboardServiceImpl, ctx context.Context, k8sCliMock *client.MockK8sHandler) {
// Test organization
fakeOrgService := service.orgService.(*orgtest.FakeOrgService)
@@ -2763,6 +2773,8 @@ func TestK8sDashboardCleanupJob(t *testing.T) {
sqlStore, _ := sqlstore.InitTestDB(t)
lockService := serverlock.ProvideService(sqlStore, tracing.InitializeTracerForTest())
kv := kvstore.NewFakeKVStore()
dual := dualwrite.NewMockService(t)
dual.On("ReadFromUnified", mock.Anything, mock.Anything).Return(tc.readFromUnified, nil)
fakeStore := dashboards.FakeDashboardStore{}
fakePublicDashboardService := publicdashboards.NewFakePublicDashboardServiceWrapper(t)
@@ -2782,6 +2794,7 @@ func TestK8sDashboardCleanupJob(t *testing.T) {
serverLockService: lockService,
kvstore: kv,
features: features,
dual: dual,
}
ctx, k8sCliMock := setupK8sDashboardTests(service)