From d57fa1291f10f1907a93c7f14fb7e115302d7acd Mon Sep 17 00:00:00 2001 From: Marco de Abreu Date: Tue, 15 Apr 2025 23:08:54 +0200 Subject: [PATCH] Dashboards: Disallow List with RV filter on legacy & align RV format (#104003) Throw errors when trying to call List in legacy with an RV specified Change ResourceVersion to microseconds Co-authored-by: Marco de Abreu <18629099+marcoabreu@users.noreply.github.com> --- pkg/registry/apis/dashboard/legacy/sql_dashboards.go | 4 ++-- pkg/registry/apis/dashboard/legacy/storage.go | 5 +++++ pkg/registry/apis/dashboard/libary_panel.go | 4 ++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/registry/apis/dashboard/legacy/sql_dashboards.go b/pkg/registry/apis/dashboard/legacy/sql_dashboards.go index e7aa63cac61..90012b46ff5 100644 --- a/pkg/registry/apis/dashboard/legacy/sql_dashboards.go +++ b/pkg/registry/apis/dashboard/legacy/sql_dashboards.go @@ -550,7 +550,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library ObjectMeta: metav1.ObjectMeta{ Name: p.UID, CreationTimestamp: metav1.NewTime(p.Created), - ResourceVersion: strconv.FormatInt(p.Updated.UnixMilli(), 10), + ResourceVersion: strconv.FormatInt(p.Updated.UnixMicro(), 10), }, Spec: dashboard.LibraryPanelSpec{}, } @@ -610,7 +610,7 @@ func (a *dashboardSqlAccess) GetLibraryPanels(ctx context.Context, query Library if query.UID == "" { rv, err := sqlx.GetResourceVersion(ctx, "library_element", "updated") if err == nil { - res.ResourceVersion = strconv.FormatInt(rv, 10) + res.ResourceVersion = strconv.FormatInt(rv*1000, 10) // convert to microseconds } } return res, err diff --git a/pkg/registry/apis/dashboard/legacy/storage.go b/pkg/registry/apis/dashboard/legacy/storage.go index a188b714d8a..a51241a6410 100644 --- a/pkg/registry/apis/dashboard/legacy/storage.go +++ b/pkg/registry/apis/dashboard/legacy/storage.go @@ -13,6 +13,7 @@ import ( "github.com/grafana/grafana/pkg/apimachinery/utils" "github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/storage/unified/resource" + apierrors "k8s.io/apimachinery/pkg/api/errors" ) func getDashboardFromEvent(event resource.WriteEvent) (*dashboard.Dashboard, error) { @@ -222,6 +223,9 @@ func (a *dashboardSqlAccess) ReadResource(ctx context.Context, req *resource.Rea // List implements AppendingStore. func (a *dashboardSqlAccess) ListIterator(ctx context.Context, req *resource.ListRequest, cb func(resource.ListIterator) error) (int64, error) { + if req.ResourceVersion != 0 { + return 0, apierrors.NewBadRequest("List with explicit resourceVersion is not supported with this storage backend") + } opts := req.Options info, err := claims.ParseNamespace(opts.Key.Namespace) if err == nil { @@ -265,6 +269,7 @@ func (a *dashboardSqlAccess) ListIterator(ctx context.Context, req *resource.Lis if err != nil { return 0, err } + listRV *= 1000 // Convert to microseconds rows, err := a.getRows(ctx, sql, query) if rows != nil { defer func() { diff --git a/pkg/registry/apis/dashboard/libary_panel.go b/pkg/registry/apis/dashboard/libary_panel.go index a540b28ecf2..9e560117c33 100644 --- a/pkg/registry/apis/dashboard/libary_panel.go +++ b/pkg/registry/apis/dashboard/libary_panel.go @@ -13,6 +13,7 @@ import ( "github.com/grafana/grafana/pkg/apimachinery/utils" "github.com/grafana/grafana/pkg/registry/apis/dashboard/legacy" "github.com/grafana/grafana/pkg/services/apiserver/endpoints/request" + apierrors "k8s.io/apimachinery/pkg/api/errors" ) var ( @@ -51,6 +52,9 @@ func (s *LibraryPanelStore) ConvertToTable(ctx context.Context, object runtime.O } func (s *LibraryPanelStore) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) { + if options.ResourceVersion != "" { + return nil, apierrors.NewBadRequest("List with explicit resourceVersion is not supported with this storage backend") + } ns, err := request.NamespaceInfoFrom(ctx, true) if err != nil { return nil, err