From 4cb756c5d195257189e21c3f93683a405e03fdb8 Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Mon, 31 Mar 2025 07:34:54 -0600 Subject: [PATCH] K8s: Fix error conversion for provisioned dashboards (#103074) --- pkg/api/apierrors/dashboard.go | 5 +++++ pkg/api/dashboard.go | 7 +++++++ pkg/registry/apis/dashboard/register.go | 2 +- pkg/tests/api/dashboards/api_dashboards_test.go | 16 +++++++++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/api/apierrors/dashboard.go b/pkg/api/apierrors/dashboard.go index 84bd504e9d4..b3d11f2f0d8 100644 --- a/pkg/api/apierrors/dashboard.go +++ b/pkg/api/apierrors/dashboard.go @@ -46,5 +46,10 @@ func ToDashboardErrorResponse(ctx context.Context, pluginStore pluginstore.Store return response.Error(http.StatusRequestEntityTooLarge, fmt.Sprintf("Dashboard is too large, max is %d MB", apiserver.MaxRequestBodyBytes/1024/1024), err) } + var statusErr *apierrors.StatusError + if errors.As(err, &statusErr) { + return response.Error(int(statusErr.ErrStatus.Code), statusErr.ErrStatus.Message, err) + } + return response.Error(http.StatusInternalServerError, "Failed to save dashboard", err) } diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index ab1f43424be..f8f8df1132e 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -36,6 +36,7 @@ import ( "github.com/grafana/grafana/pkg/services/user" "github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/web" + k8serrors "k8s.io/apimachinery/pkg/api/errors" ) const ( @@ -504,6 +505,12 @@ func (hs *HTTPServer) deleteDashboard(c *contextmodel.ReqContext) response.Respo return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), err) } } + + var statusErr *k8serrors.StatusError + if errors.As(err, &statusErr) { + return response.Error(int(statusErr.ErrStatus.Code), statusErr.ErrStatus.Message, err) + } + return response.Error(http.StatusInternalServerError, "Failed to delete dashboard", err) } diff --git a/pkg/registry/apis/dashboard/register.go b/pkg/registry/apis/dashboard/register.go index e420f9b8e63..f1ccc26bf3d 100644 --- a/pkg/registry/apis/dashboard/register.go +++ b/pkg/registry/apis/dashboard/register.go @@ -173,7 +173,7 @@ func (b *DashboardsAPIBuilder) Validate(ctx context.Context, a admission.Attribu } if provisioningData != nil { - return dashboards.ErrDashboardCannotDeleteProvisionedDashboard + return apierrors.NewBadRequest(dashboards.ErrDashboardCannotDeleteProvisionedDashboard.Reason) } } } diff --git a/pkg/tests/api/dashboards/api_dashboards_test.go b/pkg/tests/api/dashboards/api_dashboards_test.go index cdb016e6dc3..f188bed455a 100644 --- a/pkg/tests/api/dashboards/api_dashboards_test.go +++ b/pkg/tests/api/dashboards/api_dashboards_test.go @@ -152,9 +152,23 @@ func TestIntegrationUpdatingProvisionionedDashboards(t *testing.T) { t.Skip("skipping integration test") } + testUpdatingProvisionionedDashboards(t, []string{}) +} + +func TestIntegrationUpdatingProvisionionedDashboardsK8s(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + + // will be the default in g12 + testUpdatingProvisionionedDashboards(t, []string{featuremgmt.FlagKubernetesClientDashboardsFolders}) +} + +func testUpdatingProvisionionedDashboards(t *testing.T, featureToggles []string) { // Setup Grafana and its Database dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{ - DisableAnonymous: true, + DisableAnonymous: true, + EnableFeatureToggles: featureToggles, }) provDashboardsDir := filepath.Join(dir, "conf", "provisioning", "dashboards")