mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s: Use grafana.app as the playlist+example group name domains (#77096)
This commit is contained in:
parent
e12e40fc24
commit
5d44240fca
@ -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
|
||||
|
||||
|
@ -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),
|
||||
|
@ -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",
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"`
|
||||
|
@ -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",
|
||||
|
@ -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<void> {
|
||||
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<void> {
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user