K8s: Use grafana.app as the playlist+example group name domains (#77096)

This commit is contained in:
Ryan McKinley 2023-10-25 07:24:19 -07:00 committed by GitHub
parent e12e40fc24
commit 5d44240fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 68 deletions

View File

@ -22,7 +22,7 @@ import (
) )
// GroupName is the group name for this API. // GroupName is the group name for this API.
const GroupName = "example.grafana.com" const GroupName = "example.grafana.app"
const VersionID = "v0alpha1" // const VersionID = "v0alpha1" //
const APIVersion = GroupName + "/" + VersionID const APIVersion = GroupName + "/" + VersionID

View File

@ -39,10 +39,6 @@ func convertToK8sResource(v *playlist.PlaylistDTO, namespacer namespaceMapper) *
}) })
} }
return &Playlist{ return &Playlist{
TypeMeta: metav1.TypeMeta{
Kind: "Playlist",
APIVersion: APIVersion,
},
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: v.Uid, Name: v.Uid,
UID: types.UID(v.Uid), UID: types.UID(v.Uid),

View File

@ -34,8 +34,6 @@ func TestPlaylistConversion(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
//fmt.Printf("%s", string(out)) //fmt.Printf("%s", string(out))
require.JSONEq(t, `{ require.JSONEq(t, `{
"kind": "Playlist",
"apiVersion": "playlist.x.grafana.com/v0alpha1",
"metadata": { "metadata": {
"name": "abc", "name": "abc",
"namespace": "org-3", "namespace": "org-3",

View File

@ -68,12 +68,7 @@ func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListO
return nil, err return nil, err
} }
list := &PlaylistList{ list := &PlaylistList{}
TypeMeta: metav1.TypeMeta{
Kind: "PlaylistList",
APIVersion: APIVersion,
},
}
for _, v := range res { for _, v := range res {
p, err := s.service.Get(ctx, &playlist.GetPlaylistByUidQuery{ p, err := s.service.Get(ctx, &playlist.GetPlaylistByUidQuery{
UID: v.UID, UID: v.UID,

View File

@ -16,6 +16,10 @@ import (
"github.com/grafana/grafana/pkg/setting" "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) var _ grafanaapiserver.APIGroupBuilder = (*PlaylistAPIBuilder)(nil)
// This is used just so wire has something unique to return // 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 { func (b *PlaylistAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
err := AddToScheme(scheme) scheme.AddKnownTypes(SchemeGroupVersion,
if err != nil { &Playlist{},
return err &PlaylistList{},
} )
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return scheme.SetVersionPriority(SchemeGroupVersion) return scheme.SetVersionPriority(SchemeGroupVersion)
} }
@ -90,21 +95,3 @@ var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: VersionI
func Resource(resource string) schema.GroupResource { func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).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
}

View File

@ -4,11 +4,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 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 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type Playlist struct { type Playlist struct {
metav1.TypeMeta `json:",inline"` metav1.TypeMeta `json:",inline"`

View File

@ -11,10 +11,6 @@ import (
func TestPlaylistClone(t *testing.T) { func TestPlaylistClone(t *testing.T) {
src := Playlist{ src := Playlist{
TypeMeta: metav1.TypeMeta{
Kind: "Playlist",
APIVersion: APIVersion,
},
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: "TheUID", Name: "TheUID",
ResourceVersion: "12345", ResourceVersion: "12345",

View File

@ -42,6 +42,8 @@ interface K8sPlaylistList {
} }
interface K8sPlaylist { interface K8sPlaylist {
apiVersion: string;
kind: 'Playlist';
metadata: { metadata: {
name: string; name: string;
}; };
@ -53,12 +55,13 @@ interface K8sPlaylist {
} }
class K8sAPI implements PlaylistAPI { class K8sAPI implements PlaylistAPI {
readonly apiVersion = 'playlist.grafana.app/v0alpha1';
readonly url: string; readonly url: string;
readonly legacy: PlaylistAPI | undefined; readonly legacy: PlaylistAPI | undefined;
constructor() { constructor() {
const ns = contextSrv.user.orgId === 1 ? 'default' : `org-${contextSrv.user.orgId}`; 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 // When undefined, this will use k8s for all CRUD features
// if (!config.featureToggles.grafanaAPIServerWithExperimentalAPIs) { // if (!config.featureToggles.grafanaAPIServerWithExperimentalAPIs) {
@ -81,35 +84,16 @@ class K8sAPI implements PlaylistAPI {
if (this.legacy) { if (this.legacy) {
return this.legacy.createPlaylist(playlist); return this.legacy.createPlaylist(playlist);
} }
await withErrorHandling(() => const body = this.playlistAsK8sResource(playlist);
getBackendSrv().post(this.url, { await withErrorHandling(() => getBackendSrv().post(this.url, body));
apiVersion: 'playlists.grafana.com/v0alpha1',
kind: 'Playlist',
metadata: {
name: playlist.uid,
},
spec: playlist,
})
);
} }
async updatePlaylist(playlist: Playlist): Promise<void> { async updatePlaylist(playlist: Playlist): Promise<void> {
if (this.legacy) { if (this.legacy) {
return this.legacy.updatePlaylist(playlist); return this.legacy.updatePlaylist(playlist);
} }
await withErrorHandling(() => const body = this.playlistAsK8sResource(playlist);
getBackendSrv().put(`${this.url}/${playlist.uid}`, { await withErrorHandling(() => getBackendSrv().put(`${this.url}/${playlist.uid}`, body));
apiVersion: 'playlists.grafana.com/v0alpha1',
kind: 'Playlist',
metadata: {
name: playlist.uid,
},
spec: {
...playlist,
title: playlist.name,
},
})
);
} }
async deletePlaylist(uid: string): Promise<void> { async deletePlaylist(uid: string): Promise<void> {
@ -118,6 +102,21 @@ class K8sAPI implements PlaylistAPI {
} }
await withErrorHandling(() => getBackendSrv().delete(`${this.url}/${uid}`), 'Playlist deleted'); 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 // This converts a saved k8s resource into a playlist object