diff --git a/pkg/apis/example/v0alpha1/register.go b/pkg/apis/example/v0alpha1/register.go index 88122ef0ccd..7dfca278ae6 100644 --- a/pkg/apis/example/v0alpha1/register.go +++ b/pkg/apis/example/v0alpha1/register.go @@ -22,7 +22,7 @@ import ( ) // GroupName is the group name for this API. -const GroupName = "example.grafana.com" +const GroupName = "example.grafana.app" const VersionID = "v0alpha1" // const APIVersion = GroupName + "/" + VersionID diff --git a/pkg/apis/playlist/v0alpha1/conversions.go b/pkg/apis/playlist/v0alpha1/conversions.go index c3cb0b95222..0994cd55ea6 100644 --- a/pkg/apis/playlist/v0alpha1/conversions.go +++ b/pkg/apis/playlist/v0alpha1/conversions.go @@ -39,10 +39,6 @@ func convertToK8sResource(v *playlist.PlaylistDTO, namespacer namespaceMapper) * }) } return &Playlist{ - TypeMeta: metav1.TypeMeta{ - Kind: "Playlist", - APIVersion: APIVersion, - }, ObjectMeta: metav1.ObjectMeta{ Name: v.Uid, UID: types.UID(v.Uid), diff --git a/pkg/apis/playlist/v0alpha1/conversions_test.go b/pkg/apis/playlist/v0alpha1/conversions_test.go index f8809ca050c..17c458b6cad 100644 --- a/pkg/apis/playlist/v0alpha1/conversions_test.go +++ b/pkg/apis/playlist/v0alpha1/conversions_test.go @@ -34,8 +34,6 @@ func TestPlaylistConversion(t *testing.T) { require.NoError(t, err) //fmt.Printf("%s", string(out)) require.JSONEq(t, `{ - "kind": "Playlist", - "apiVersion": "playlist.x.grafana.com/v0alpha1", "metadata": { "name": "abc", "namespace": "org-3", diff --git a/pkg/apis/playlist/v0alpha1/legacy_storage.go b/pkg/apis/playlist/v0alpha1/legacy_storage.go index 11af7806457..debf40e386b 100644 --- a/pkg/apis/playlist/v0alpha1/legacy_storage.go +++ b/pkg/apis/playlist/v0alpha1/legacy_storage.go @@ -68,12 +68,7 @@ func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListO return nil, err } - list := &PlaylistList{ - TypeMeta: metav1.TypeMeta{ - Kind: "PlaylistList", - APIVersion: APIVersion, - }, - } + list := &PlaylistList{} for _, v := range res { p, err := s.service.Get(ctx, &playlist.GetPlaylistByUidQuery{ UID: v.UID, diff --git a/pkg/apis/playlist/v0alpha1/register.go b/pkg/apis/playlist/v0alpha1/register.go index f77d0148ea8..f0d67d18a30 100644 --- a/pkg/apis/playlist/v0alpha1/register.go +++ b/pkg/apis/playlist/v0alpha1/register.go @@ -16,6 +16,10 @@ import ( "github.com/grafana/grafana/pkg/setting" ) +// GroupName is the group name for this API. +const GroupName = "playlist.grafana.app" +const VersionID = "v0alpha1" + var _ grafanaapiserver.APIGroupBuilder = (*PlaylistAPIBuilder)(nil) // This is used just so wire has something unique to return @@ -41,10 +45,11 @@ func (b *PlaylistAPIBuilder) GetGroupVersion() schema.GroupVersion { } func (b *PlaylistAPIBuilder) InstallSchema(scheme *runtime.Scheme) error { - err := AddToScheme(scheme) - if err != nil { - return err - } + scheme.AddKnownTypes(SchemeGroupVersion, + &Playlist{}, + &PlaylistList{}, + ) + metav1.AddToGroupVersion(scheme, SchemeGroupVersion) return scheme.SetVersionPriority(SchemeGroupVersion) } @@ -90,21 +95,3 @@ var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: VersionI func Resource(resource string) schema.GroupResource { return SchemeGroupVersion.WithResource(resource).GroupResource() } - -var ( - // SchemeBuilder points to a list of functions added to Scheme. - SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) - localSchemeBuilder = &SchemeBuilder - // AddToScheme is a common registration function for mapping packaged scoped group & version keys to a scheme. - AddToScheme = localSchemeBuilder.AddToScheme -) - -// Adds the list of known types to the given scheme. -func addKnownTypes(scheme *runtime.Scheme) error { - scheme.AddKnownTypes(SchemeGroupVersion, - &Playlist{}, - &PlaylistList{}, - ) - metav1.AddToGroupVersion(scheme, SchemeGroupVersion) - return nil -} diff --git a/pkg/apis/playlist/v0alpha1/types.go b/pkg/apis/playlist/v0alpha1/types.go index 7a8a147fb2f..fb5b9e6faa6 100644 --- a/pkg/apis/playlist/v0alpha1/types.go +++ b/pkg/apis/playlist/v0alpha1/types.go @@ -4,11 +4,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// GroupName is the group name for this API. -const GroupName = "playlist.x.grafana.com" -const VersionID = "v0alpha1" // -const APIVersion = GroupName + "/" + VersionID - // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type Playlist struct { metav1.TypeMeta `json:",inline"` diff --git a/pkg/apis/playlist/v0alpha1/types_test.go b/pkg/apis/playlist/v0alpha1/types_test.go index 83033359872..8ae306ddf2e 100644 --- a/pkg/apis/playlist/v0alpha1/types_test.go +++ b/pkg/apis/playlist/v0alpha1/types_test.go @@ -11,10 +11,6 @@ import ( func TestPlaylistClone(t *testing.T) { src := Playlist{ - TypeMeta: metav1.TypeMeta{ - Kind: "Playlist", - APIVersion: APIVersion, - }, ObjectMeta: metav1.ObjectMeta{ Name: "TheUID", ResourceVersion: "12345", diff --git a/public/app/features/playlist/api.ts b/public/app/features/playlist/api.ts index a463eaa023f..27ea44fdf96 100644 --- a/public/app/features/playlist/api.ts +++ b/public/app/features/playlist/api.ts @@ -42,6 +42,8 @@ interface K8sPlaylistList { } interface K8sPlaylist { + apiVersion: string; + kind: 'Playlist'; metadata: { name: string; }; @@ -53,12 +55,13 @@ interface K8sPlaylist { } class K8sAPI implements PlaylistAPI { + readonly apiVersion = 'playlist.grafana.app/v0alpha1'; readonly url: string; readonly legacy: PlaylistAPI | undefined; constructor() { const ns = contextSrv.user.orgId === 1 ? 'default' : `org-${contextSrv.user.orgId}`; - this.url = `/apis/playlist.x.grafana.com/v0alpha1/namespaces/${ns}/playlists`; + this.url = `/apis/${this.apiVersion}/namespaces/${ns}/playlists`; // When undefined, this will use k8s for all CRUD features // if (!config.featureToggles.grafanaAPIServerWithExperimentalAPIs) { @@ -81,35 +84,16 @@ class K8sAPI implements PlaylistAPI { if (this.legacy) { return this.legacy.createPlaylist(playlist); } - await withErrorHandling(() => - getBackendSrv().post(this.url, { - apiVersion: 'playlists.grafana.com/v0alpha1', - kind: 'Playlist', - metadata: { - name: playlist.uid, - }, - spec: playlist, - }) - ); + const body = this.playlistAsK8sResource(playlist); + await withErrorHandling(() => getBackendSrv().post(this.url, body)); } async updatePlaylist(playlist: Playlist): Promise { if (this.legacy) { return this.legacy.updatePlaylist(playlist); } - await withErrorHandling(() => - getBackendSrv().put(`${this.url}/${playlist.uid}`, { - apiVersion: 'playlists.grafana.com/v0alpha1', - kind: 'Playlist', - metadata: { - name: playlist.uid, - }, - spec: { - ...playlist, - title: playlist.name, - }, - }) - ); + const body = this.playlistAsK8sResource(playlist); + await withErrorHandling(() => getBackendSrv().put(`${this.url}/${playlist.uid}`, body)); } async deletePlaylist(uid: string): Promise { @@ -118,6 +102,21 @@ class K8sAPI implements PlaylistAPI { } await withErrorHandling(() => getBackendSrv().delete(`${this.url}/${uid}`), 'Playlist deleted'); } + + playlistAsK8sResource = (playlist: Playlist): K8sPlaylist => { + return { + apiVersion: this.apiVersion, + kind: 'Playlist', + metadata: { + name: playlist.uid, // uid as k8s name + }, + spec: { + title: playlist.name, // name becomes title + interval: playlist.interval, + items: playlist.items ?? [], + }, + }; + }; } // This converts a saved k8s resource into a playlist object