From d72e048bfe8348c0a0b5d8ee3550cd46736ac718 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Wed, 29 Oct 2025 00:54:10 +0300 Subject: [PATCH] Chore: Use Kind().GroupVersionResource() (#113133) --- pkg/api/playlist.go | 7 +---- pkg/api/short_url.go | 7 +---- pkg/registry/apps/playlist/legacy_storage.go | 6 +--- pkg/registry/apps/playlist/register.go | 6 +--- pkg/registry/apps/plugins/register.go | 6 +--- pkg/registry/apps/shorturl/legacy_storage.go | 11 ++------ pkg/registry/apps/shorturl/register.go | 6 +--- .../builder/runner/admission_test.go | 19 ++++--------- pkg/services/cleanup/cleanup.go | 7 +---- .../alerting/notifications/common/testing.go | 28 ++++--------------- .../apis/alerting/rules/common/testing.go | 26 +++++------------ 11 files changed, 27 insertions(+), 102 deletions(-) diff --git a/pkg/api/playlist.go b/pkg/api/playlist.go index 4108bba14f9..632646c7c61 100644 --- a/pkg/api/playlist.go +++ b/pkg/api/playlist.go @@ -144,13 +144,8 @@ type playlistK8sHandler struct { //----------------------------------------------------------------------------------------- func newPlaylistK8sHandler(hs *HTTPServer) *playlistK8sHandler { - gvr := schema.GroupVersionResource{ - Group: v0alpha1.PlaylistKind().Group(), - Version: v0alpha1.PlaylistKind().Version(), - Resource: v0alpha1.PlaylistKind().Plural(), - } return &playlistK8sHandler{ - gvr: gvr, + gvr: v0alpha1.PlaylistKind().GroupVersionResource(), namespacer: request.GetNamespaceMapper(hs.Cfg), clientConfigProvider: hs.clientConfigProvider, } diff --git a/pkg/api/short_url.go b/pkg/api/short_url.go index 763c58395ad..b07e1488e05 100644 --- a/pkg/api/short_url.go +++ b/pkg/api/short_url.go @@ -119,13 +119,8 @@ type shortURLK8sHandler struct { } func newShortURLK8sHandler(hs *HTTPServer) *shortURLK8sHandler { - gvr := schema.GroupVersionResource{ - Group: v1alpha1.ShortURLKind().Group(), - Version: v1alpha1.ShortURLKind().Version(), - Resource: v1alpha1.ShortURLKind().Plural(), - } return &shortURLK8sHandler{ - gvr: gvr, + gvr: v1alpha1.ShortURLKind().GroupVersionResource(), namespacer: request.GetNamespaceMapper(hs.Cfg), clientConfigProvider: hs.clientConfigProvider, cfg: hs.Cfg, diff --git a/pkg/registry/apps/playlist/legacy_storage.go b/pkg/registry/apps/playlist/legacy_storage.go index 4f6affce640..8425035e093 100644 --- a/pkg/registry/apps/playlist/legacy_storage.go +++ b/pkg/registry/apps/playlist/legacy_storage.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/internalversion" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/registry/rest" playlist "github.com/grafana/grafana/apps/playlist/pkg/apis/playlist/v0alpha1" @@ -87,10 +86,7 @@ func (s *legacyStorage) Get(ctx context.Context, name string, options *metav1.Ge }) if err != nil || dto == nil { if errors.Is(err, playlistsvc.ErrPlaylistNotFound) || err == nil { - err = k8serrors.NewNotFound(schema.GroupResource{ - Group: playlist.PlaylistKind().Group(), - Resource: playlist.PlaylistKind().Plural(), - }, name) + err = k8serrors.NewNotFound(playlist.PlaylistKind().GroupVersionResource().GroupResource(), name) } return nil, err } diff --git a/pkg/registry/apps/playlist/register.go b/pkg/registry/apps/playlist/register.go index 67d6476401f..52bbc0210f9 100644 --- a/pkg/registry/apps/playlist/register.go +++ b/pkg/registry/apps/playlist/register.go @@ -65,11 +65,7 @@ func RegisterAppInstaller( // GetLegacyStorage returns the legacy storage for the playlist app. func (p *PlaylistAppInstaller) GetLegacyStorage(requested schema.GroupVersionResource) grafanarest.Storage { - gvr := schema.GroupVersionResource{ - Group: playlistv0alpha1.PlaylistKind().Group(), - Version: playlistv0alpha1.PlaylistKind().Version(), - Resource: playlistv0alpha1.PlaylistKind().Plural(), - } + gvr := playlistv0alpha1.PlaylistKind().GroupVersionResource() if requested.String() != gvr.String() { return nil } diff --git a/pkg/registry/apps/plugins/register.go b/pkg/registry/apps/plugins/register.go index dc05a47f965..2929efda78f 100644 --- a/pkg/registry/apps/plugins/register.go +++ b/pkg/registry/apps/plugins/register.go @@ -55,11 +55,7 @@ func (p *PluginsAppInstaller) InstallAPIs( server appsdkapiserver.GenericAPIServer, restOptsGetter generic.RESTOptionsGetter, ) error { - pluginMetaGVR := schema.GroupVersionResource{ - Group: pluginsv0alpha1.GroupVersion.Group, - Version: pluginsv0alpha1.GroupVersion.Version, - Resource: pluginsv0alpha1.PluginMetaKind().Plural(), - } + pluginMetaGVR := pluginsv0alpha1.PluginMetaKind().GroupVersionResource() replacedStorage := map[schema.GroupVersionResource]rest.Storage{ pluginMetaGVR: pluginsapp.NewPluginMetaStorage(request.GetNamespaceMapper(p.cfg)), } diff --git a/pkg/registry/apps/shorturl/legacy_storage.go b/pkg/registry/apps/shorturl/legacy_storage.go index 8ee7a59e899..c9462359236 100644 --- a/pkg/registry/apps/shorturl/legacy_storage.go +++ b/pkg/registry/apps/shorturl/legacy_storage.go @@ -10,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/internalversion" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/registry/rest" shorturl "github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1" @@ -97,10 +96,7 @@ func (s *legacyStorage) Get(ctx context.Context, name string, options *metav1.Ge dto, err := s.service.GetShortURLByUID(ctx, signedInUser, name) if err != nil || dto == nil { if errors.Is(err, shorturls.ErrShortURLNotFound) || err == nil { - err = k8serrors.NewNotFound(schema.GroupResource{ - Group: shorturl.ShortURLKind().Group(), - Resource: shorturl.ShortURLKind().Plural(), - }, name) + err = k8serrors.NewNotFound(shorturl.ShortURLKind().GroupVersionResource().GroupResource(), name) } return nil, err } @@ -167,10 +163,7 @@ func (s *legacyStorage) Update(ctx context.Context, shortURL, err := s.service.GetShortURLByUID(ctx, signedInUser, name) if err != nil || shortURL == nil { if errors.Is(err, shorturls.ErrShortURLNotFound) || err == nil { - err = k8serrors.NewNotFound(schema.GroupResource{ - Group: shorturl.ShortURLKind().Group(), - Resource: shorturl.ShortURLKind().Plural(), - }, name) + err = k8serrors.NewNotFound(shorturl.ShortURLKind().GroupVersionResource().GroupResource(), name) } return nil, false, err } diff --git a/pkg/registry/apps/shorturl/register.go b/pkg/registry/apps/shorturl/register.go index ecf56769e55..a44593ceabe 100644 --- a/pkg/registry/apps/shorturl/register.go +++ b/pkg/registry/apps/shorturl/register.go @@ -55,11 +55,7 @@ func RegisterAppInstaller( } func (s *ShortURLAppInstaller) GetLegacyStorage(requested schema.GroupVersionResource) grafanarest.Storage { - gvr := schema.GroupVersionResource{ - Group: shorturl.ShortURLKind().Group(), - Version: shorturl.ShortURLKind().Version(), - Resource: shorturl.ShortURLKind().Plural(), - } + gvr := shorturl.ShortURLKind().GroupVersionResource() if requested.String() != gvr.String() { return nil } diff --git a/pkg/services/apiserver/builder/runner/admission_test.go b/pkg/services/apiserver/builder/runner/admission_test.go index f9b6fec3244..11275afdea8 100644 --- a/pkg/services/apiserver/builder/runner/admission_test.go +++ b/pkg/services/apiserver/builder/runner/admission_test.go @@ -5,12 +5,11 @@ import ( "errors" "testing" - "github.com/grafana/grafana-app-sdk/app" "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/admission" "k8s.io/apiserver/pkg/authentication/user" + "github.com/grafana/grafana-app-sdk/app" examplev1 "github.com/grafana/grafana/pkg/services/apiserver/builder/runner/testdata/app/pkg/apis/example/v1" ) @@ -20,12 +19,8 @@ func TestBuilderAdmission_Validate(t *testing.T) { A: "test", }, } - gvk := schema.GroupVersionKind{ - Group: examplev1.ExampleKind().Group(), - Version: examplev1.ExampleKind().Version(), - Kind: examplev1.ExampleKind().Kind(), - } - gvr := gvk.GroupVersion().WithResource(examplev1.ExampleKind().Plural()) + gvk := examplev1.ExampleKind().GroupVersionKind() + gvr := examplev1.ExampleKind().GroupVersionResource() defaultAttributes := admission.NewAttributesRecord(exampleObj, nil, gvk, "default", "foo", gvr, "", admission.Create, nil, false, &user.DefaultInfo{}) tests := []struct { @@ -73,12 +68,8 @@ func TestBuilderAdmission_Validate(t *testing.T) { } func TestBuilderAdmission_Mutate(t *testing.T) { - gvk := schema.GroupVersionKind{ - Group: examplev1.ExampleKind().Group(), - Version: examplev1.ExampleKind().Version(), - Kind: examplev1.ExampleKind().Kind(), - } - gvr := gvk.GroupVersion().WithResource(examplev1.ExampleKind().Plural()) + gvk := examplev1.ExampleKind().GroupVersionKind() + gvr := examplev1.ExampleKind().GroupVersionResource() getAttributes := func() admission.Attributes { exampleObj := &examplev1.Example{ Spec: examplev1.ExampleSpec{ diff --git a/pkg/services/cleanup/cleanup.go b/pkg/services/cleanup/cleanup.go index cc73faa7b12..0e4fe0d319f 100644 --- a/pkg/services/cleanup/cleanup.go +++ b/pkg/services/cleanup/cleanup.go @@ -14,7 +14,6 @@ import ( k8serrors "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" "github.com/grafana/grafana/apps/shorturl/pkg/apis/shorturl/v1alpha1" @@ -326,11 +325,7 @@ func (srv *CleanUpService) deleteStaleKubernetesShortURLs(ctx context.Context) { } // Set up the GroupVersionResource for shortURLs - gvr := schema.GroupVersionResource{ - Group: v1alpha1.ShortURLKind().Group(), - Version: v1alpha1.ShortURLKind().Version(), - Resource: v1alpha1.ShortURLKind().Plural(), - } + gvr := v1alpha1.ShortURLKind().GroupVersionResource() // Calculate the expiration time expirationTime := time.Now().Add(-time.Duration(srv.Cfg.ShortLinkExpiration*24) * time.Hour) diff --git a/pkg/tests/apis/alerting/notifications/common/testing.go b/pkg/tests/apis/alerting/notifications/common/testing.go index bd9891402c9..2376b1368aa 100644 --- a/pkg/tests/apis/alerting/notifications/common/testing.go +++ b/pkg/tests/apis/alerting/notifications/common/testing.go @@ -3,12 +3,11 @@ package common import ( "testing" - "github.com/grafana/grafana/pkg/tests/apis" "github.com/stretchr/testify/require" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" "github.com/grafana/grafana/apps/alerting/notifications/pkg/apis/alertingnotifications/v0alpha1" + "github.com/grafana/grafana/pkg/tests/apis" ) func NewReceiverClient(t *testing.T, user apis.User) *apis.TypedClient[v0alpha1.Receiver, v0alpha1.ReceiverList] { @@ -19,11 +18,8 @@ func NewReceiverClient(t *testing.T, user apis.User) *apis.TypedClient[v0alpha1. return &apis.TypedClient[v0alpha1.Receiver, v0alpha1.ReceiverList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: v0alpha1.ReceiverKind().Group(), - Version: v0alpha1.ReceiverKind().Version(), - Resource: v0alpha1.ReceiverKind().Plural(), - }).Namespace("default"), + v0alpha1.ReceiverKind().GroupVersionResource()). + Namespace("default"), } } @@ -35,11 +31,7 @@ func NewRoutingTreeClient(t *testing.T, user apis.User) *apis.TypedClient[v0alph return &apis.TypedClient[v0alpha1.RoutingTree, v0alpha1.RoutingTreeList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: v0alpha1.RoutingTreeKind().Group(), - Version: v0alpha1.RoutingTreeKind().Version(), - Resource: v0alpha1.RoutingTreeKind().Plural(), - }).Namespace("default"), + v0alpha1.RoutingTreeKind().GroupVersionResource()).Namespace("default"), } } @@ -51,11 +43,7 @@ func NewTemplateGroupClient(t *testing.T, user apis.User) *apis.TypedClient[v0al return &apis.TypedClient[v0alpha1.TemplateGroup, v0alpha1.TemplateGroupList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: v0alpha1.TemplateGroupKind().Group(), - Version: v0alpha1.TemplateGroupKind().Version(), - Resource: v0alpha1.TemplateGroupKind().Plural(), - }).Namespace("default"), + v0alpha1.TemplateGroupKind().GroupVersionResource()).Namespace("default"), } } @@ -67,10 +55,6 @@ func NewTimeIntervalClient(t *testing.T, user apis.User) *apis.TypedClient[v0alp return &apis.TypedClient[v0alpha1.TimeInterval, v0alpha1.TimeIntervalList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: v0alpha1.TimeIntervalKind().Group(), - Version: v0alpha1.TimeIntervalKind().Version(), - Resource: v0alpha1.TimeIntervalKind().Plural(), - }).Namespace("default"), + v0alpha1.TimeIntervalKind().GroupVersionResource()).Namespace("default"), } } diff --git a/pkg/tests/apis/alerting/rules/common/testing.go b/pkg/tests/apis/alerting/rules/common/testing.go index da9dd55a694..be43be5da26 100644 --- a/pkg/tests/apis/alerting/rules/common/testing.go +++ b/pkg/tests/apis/alerting/rules/common/testing.go @@ -4,14 +4,14 @@ import ( "context" "testing" + "github.com/stretchr/testify/require" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/dynamic" + "github.com/grafana/grafana/apps/alerting/rules/pkg/apis/alerting/v0alpha1" folders "github.com/grafana/grafana/apps/folder/pkg/apis/folder/v1beta1" "github.com/grafana/grafana/pkg/tests/apis" "github.com/grafana/grafana/pkg/tests/testinfra" - "github.com/stretchr/testify/require" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/dynamic" ) func NewAlertRuleClient(t *testing.T, user apis.User) *apis.TypedClient[v0alpha1.AlertRule, v0alpha1.AlertRuleList] { @@ -22,11 +22,7 @@ func NewAlertRuleClient(t *testing.T, user apis.User) *apis.TypedClient[v0alpha1 return &apis.TypedClient[v0alpha1.AlertRule, v0alpha1.AlertRuleList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: v0alpha1.AlertRuleKind().Group(), - Version: v0alpha1.AlertRuleKind().Version(), - Resource: v0alpha1.AlertRuleKind().Plural(), - }).Namespace("default"), + v0alpha1.AlertRuleKind().GroupVersionResource()).Namespace("default"), } } @@ -38,11 +34,7 @@ func NewRecordingRuleClient(t *testing.T, user apis.User) *apis.TypedClient[v0al return &apis.TypedClient[v0alpha1.RecordingRule, v0alpha1.RecordingRuleList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: v0alpha1.RecordingRuleKind().Group(), - Version: v0alpha1.RecordingRuleKind().Version(), - Resource: v0alpha1.RecordingRuleKind().Plural(), - }).Namespace("default"), + v0alpha1.RecordingRuleKind().GroupVersionResource()).Namespace("default"), } } @@ -54,11 +46,7 @@ func NewFolderClient(t *testing.T, user apis.User) *apis.TypedClient[folders.Fol return &apis.TypedClient[folders.Folder, folders.FolderList]{ Client: client.Resource( - schema.GroupVersionResource{ - Group: folders.FolderKind().Group(), - Version: folders.FolderKind().Version(), - Resource: folders.FolderKind().Plural(), - }).Namespace("default"), + folders.FolderKind().GroupVersionResource()).Namespace("default"), } }