From 19a52ba35eaaef0fac6e4458d8f08bab137e3186 Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Mon, 31 Mar 2025 07:34:39 -0600 Subject: [PATCH] K8s: Dashboards: Fix multi-org quota (#103073) --- .../dashboards/service/dashboard_service.go | 10 ++++++---- .../service/dashboard_service_test.go | 4 ++-- pkg/tests/api/dashboards/api_dashboards_test.go | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/pkg/services/dashboards/service/dashboard_service.go b/pkg/services/dashboards/service/dashboard_service.go index adcbd8014be..17177c0f8b9 100644 --- a/pkg/services/dashboards/service/dashboard_service.go +++ b/pkg/services/dashboards/service/dashboard_service.go @@ -447,11 +447,13 @@ func (dr *DashboardServiceImpl) Count(ctx context.Context, scopeParams *quota.Sc } total += orgDashboards - tag, err := quota.NewTag(dashboards.QuotaTargetSrv, dashboards.QuotaTarget, quota.OrgScope) - if err != nil { - return nil, err + if scopeParams != nil && scopeParams.OrgID == org.ID { + tag, err := quota.NewTag(dashboards.QuotaTargetSrv, dashboards.QuotaTarget, quota.OrgScope) + if err != nil { + return nil, err + } + u.Set(tag, orgDashboards) } - u.Set(tag, orgDashboards) } tag, err := quota.NewTag(dashboards.QuotaTargetSrv, dashboards.QuotaTarget, quota.GlobalScope) diff --git a/pkg/services/dashboards/service/dashboard_service_test.go b/pkg/services/dashboards/service/dashboard_service_test.go index 8930c2304cf..cab7f23ff66 100644 --- a/pkg/services/dashboards/service/dashboard_service_test.go +++ b/pkg/services/dashboards/service/dashboard_service_test.go @@ -2244,8 +2244,8 @@ func TestQuotaCount(t *testing.T) { ctx, k8sCliMock := setupK8sDashboardTests(service) orgSvc := orgtest.FakeOrgService{ExpectedOrgs: orgs} service.orgService = &orgSvc - k8sCliMock.On("GetStats", mock.Anything, mock.Anything).Return(&countOrg2, nil).Once() - k8sCliMock.On("GetStats", mock.Anything, mock.Anything).Return(&countOrg1, nil).Once() + k8sCliMock.On("GetStats", mock.Anything, int64(2)).Return(&countOrg2, nil).Once() + k8sCliMock.On("GetStats", mock.Anything, int64(1)).Return(&countOrg1, nil).Once() result, err := service.Count(ctx, query) require.NoError(t, err) diff --git a/pkg/tests/api/dashboards/api_dashboards_test.go b/pkg/tests/api/dashboards/api_dashboards_test.go index c982ab625ba..cdb016e6dc3 100644 --- a/pkg/tests/api/dashboards/api_dashboards_test.go +++ b/pkg/tests/api/dashboards/api_dashboards_test.go @@ -47,14 +47,25 @@ func TestIntegrationDashboardQuota(t *testing.T) { if testing.Short() { t.Skip("skipping integration test") } + testDashboardQuota(t, []string{}) +} +func TestIntegrationDashboardQuotaK8s(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + testDashboardQuota(t, []string{featuremgmt.FlagKubernetesClientDashboardsFolders}) +} + +func testDashboardQuota(t *testing.T, featureToggles []string) { // enable quota and set low dashboard quota // Setup Grafana and its Database dashboardQuota := int64(1) dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ - DisableAnonymous: true, - EnableQuota: true, - DashboardOrgQuota: &dashboardQuota, + DisableAnonymous: true, + EnableQuota: true, + DashboardOrgQuota: &dashboardQuota, + EnableFeatureToggles: featureToggles, }) grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)