Playlist: Migrate to App SDK (#95691)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/registry"
|
||||
"github.com/grafana/grafana/pkg/registry/apps/playlist"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/builder/runner"
|
||||
@@ -22,6 +23,7 @@ type Service struct {
|
||||
func ProvideRegistryServiceSink(
|
||||
registrar builder.APIRegistrar,
|
||||
restConfigProvider apiserver.RestConfigProvider,
|
||||
playlistAppProvider *playlist.PlaylistAppProvider,
|
||||
) (*Service, error) {
|
||||
cfgWrapper := func(ctx context.Context) *rest.Config {
|
||||
cfg := restConfigProvider.GetRestConfig(ctx)
|
||||
@@ -36,7 +38,7 @@ func ProvideRegistryServiceSink(
|
||||
RestConfigGetter: cfgWrapper,
|
||||
APIRegistrar: registrar,
|
||||
}
|
||||
runner, err := runner.NewAPIGroupRunner(cfg)
|
||||
runner, err := runner.NewAPIGroupRunner(cfg, playlistAppProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package playlist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
playlist "github.com/grafana/grafana/apps/playlist/pkg/apis/playlist/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
gapiutil "github.com/grafana/grafana/pkg/services/apiserver/utils"
|
||||
playlistsvc "github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func LegacyUpdateCommandToUnstructured(cmd playlistsvc.UpdatePlaylistCommand) unstructured.Unstructured {
|
||||
items := []map[string]string{}
|
||||
for _, item := range cmd.Items {
|
||||
items = append(items, map[string]string{
|
||||
"type": item.Type,
|
||||
"value": item.Value,
|
||||
})
|
||||
}
|
||||
obj := unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"spec": map[string]interface{}{
|
||||
"title": cmd.Name,
|
||||
"interval": cmd.Interval,
|
||||
"items": items,
|
||||
},
|
||||
},
|
||||
}
|
||||
if cmd.UID == "" {
|
||||
cmd.UID = util.GenerateShortUID()
|
||||
}
|
||||
obj.SetName(cmd.UID)
|
||||
return obj
|
||||
}
|
||||
|
||||
func UnstructuredToLegacyPlaylist(item unstructured.Unstructured) *playlistsvc.Playlist {
|
||||
spec := item.Object["spec"].(map[string]any)
|
||||
return &playlistsvc.Playlist{
|
||||
UID: item.GetName(),
|
||||
Name: spec["title"].(string),
|
||||
Interval: spec["interval"].(string),
|
||||
Id: getLegacyID(&item),
|
||||
}
|
||||
}
|
||||
|
||||
func UnstructuredToLegacyPlaylistDTO(item unstructured.Unstructured) *playlistsvc.PlaylistDTO {
|
||||
spec := item.Object["spec"].(map[string]any)
|
||||
dto := &playlistsvc.PlaylistDTO{
|
||||
Uid: item.GetName(),
|
||||
Name: spec["title"].(string),
|
||||
Interval: spec["interval"].(string),
|
||||
Id: getLegacyID(&item),
|
||||
}
|
||||
items := spec["items"]
|
||||
if items != nil {
|
||||
b, err := json.Marshal(items)
|
||||
if err == nil {
|
||||
_ = json.Unmarshal(b, &dto.Items)
|
||||
}
|
||||
}
|
||||
return dto
|
||||
}
|
||||
|
||||
func convertToK8sResource(v *playlistsvc.PlaylistDTO, namespacer request.NamespaceMapper) *playlist.Playlist {
|
||||
spec := playlist.PlaylistSpec{
|
||||
Title: v.Name,
|
||||
Interval: v.Interval,
|
||||
}
|
||||
for _, item := range v.Items {
|
||||
spec.Items = append(spec.Items, playlist.PlaylistItem{
|
||||
Type: playlist.PlaylistItemType(item.Type),
|
||||
Value: item.Value,
|
||||
})
|
||||
}
|
||||
|
||||
p := &playlist.Playlist{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: v.Uid,
|
||||
UID: types.UID(v.Uid),
|
||||
ResourceVersion: fmt.Sprintf("%d", v.UpdatedAt),
|
||||
CreationTimestamp: metav1.NewTime(time.UnixMilli(v.CreatedAt)),
|
||||
Namespace: namespacer(v.OrgID),
|
||||
},
|
||||
Spec: spec,
|
||||
}
|
||||
meta, err := utils.MetaAccessor(p)
|
||||
if err == nil {
|
||||
meta.SetUpdatedTimestampMillis(v.UpdatedAt)
|
||||
if v.Id > 0 {
|
||||
createdAt := time.UnixMilli(v.CreatedAt)
|
||||
meta.SetOriginInfo(&utils.ResourceOriginInfo{
|
||||
Name: "SQL",
|
||||
Path: fmt.Sprintf("%d", v.Id),
|
||||
Timestamp: &createdAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
p.UID = gapiutil.CalculateClusterWideUID(p)
|
||||
return p
|
||||
}
|
||||
|
||||
func convertToLegacyUpdateCommand(p *playlist.Playlist, orgId int64) (*playlistsvc.UpdatePlaylistCommand, error) {
|
||||
spec := p.Spec
|
||||
cmd := &playlistsvc.UpdatePlaylistCommand{
|
||||
UID: p.Name,
|
||||
Name: spec.Title,
|
||||
Interval: spec.Interval,
|
||||
OrgId: orgId,
|
||||
}
|
||||
for _, item := range spec.Items {
|
||||
if item.Type == playlist.PlaylistItemTypeDashboardById {
|
||||
return nil, fmt.Errorf("unsupported item type: %s", item.Type)
|
||||
}
|
||||
cmd.Items = append(cmd.Items, playlistsvc.PlaylistItem{
|
||||
Type: string(item.Type),
|
||||
Value: item.Value,
|
||||
})
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// Read legacy ID from metadata annotations
|
||||
func getLegacyID(item *unstructured.Unstructured) int64 {
|
||||
meta, err := utils.MetaAccessor(item)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
info, _ := meta.GetOriginInfo()
|
||||
if info != nil && info.Name == "SQL" {
|
||||
i, err := strconv.ParseInt(info.Path, 10, 64)
|
||||
if err == nil {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package playlist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
)
|
||||
|
||||
func TestPlaylistConversion(t *testing.T) {
|
||||
src := &playlist.PlaylistDTO{
|
||||
Id: 123,
|
||||
OrgID: 3,
|
||||
Uid: "abc", // becomes k8s name
|
||||
Name: "MyPlaylists", // becomes title
|
||||
Interval: "10s",
|
||||
CreatedAt: 12345,
|
||||
UpdatedAt: 54321,
|
||||
Items: []playlist.PlaylistItemDTO{
|
||||
{Type: "dashboard_by_uid", Value: "UID0"},
|
||||
{Type: "dashboard_by_tag", Value: "tagA"},
|
||||
{Type: "dashboard_by_id", Value: "123"}, // deprecated
|
||||
},
|
||||
}
|
||||
dst := convertToK8sResource(src, request.GetNamespaceMapper(nil))
|
||||
|
||||
require.Equal(t, "abc", src.Uid)
|
||||
require.Equal(t, "abc", dst.Name)
|
||||
require.Equal(t, src.Name, dst.Spec.Title)
|
||||
|
||||
out, err := json.MarshalIndent(dst, "", " ")
|
||||
require.NoError(t, err)
|
||||
// fmt.Printf("%s", string(out))
|
||||
require.JSONEq(t, `{
|
||||
"metadata": {
|
||||
"name": "abc",
|
||||
"namespace": "org-3",
|
||||
"uid": "f0zxjm7ApxOafsn6DLQZ4Ezp78WRUsZqSc4taOSHq1gX",
|
||||
"resourceVersion": "54321",
|
||||
"creationTimestamp": "1970-01-01T00:00:12Z",
|
||||
"annotations": {
|
||||
"grafana.app/originPath": "123",
|
||||
"grafana.app/originName": "SQL",
|
||||
"grafana.app/originTimestamp":"1970-01-01T00:00:12Z",
|
||||
"grafana.app/updatedTimestamp": "1970-01-01T00:00:54Z"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"title": "MyPlaylists",
|
||||
"interval": "10s",
|
||||
"items": [
|
||||
{
|
||||
"type": "dashboard_by_uid",
|
||||
"value": "UID0"
|
||||
},
|
||||
{
|
||||
"type": "dashboard_by_tag",
|
||||
"value": "tagA"
|
||||
},
|
||||
{
|
||||
"type": "dashboard_by_id",
|
||||
"value": "123"
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {}
|
||||
}`, string(out))
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package playlist
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"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"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
playlistsvc "github.com/grafana/grafana/pkg/services/playlist"
|
||||
)
|
||||
|
||||
var (
|
||||
_ rest.Scoper = (*legacyStorage)(nil)
|
||||
_ rest.SingularNameProvider = (*legacyStorage)(nil)
|
||||
_ rest.Getter = (*legacyStorage)(nil)
|
||||
_ rest.Lister = (*legacyStorage)(nil)
|
||||
_ rest.Storage = (*legacyStorage)(nil)
|
||||
_ rest.Creater = (*legacyStorage)(nil)
|
||||
_ rest.Updater = (*legacyStorage)(nil)
|
||||
_ rest.GracefulDeleter = (*legacyStorage)(nil)
|
||||
)
|
||||
|
||||
type legacyStorage struct {
|
||||
service playlistsvc.Service
|
||||
namespacer request.NamespaceMapper
|
||||
tableConverter rest.TableConvertor
|
||||
}
|
||||
|
||||
func (s *legacyStorage) New() runtime.Object {
|
||||
return playlist.PlaylistKind().ZeroValue()
|
||||
}
|
||||
|
||||
func (s *legacyStorage) Destroy() {}
|
||||
|
||||
func (s *legacyStorage) NamespaceScoped() bool {
|
||||
return true // namespace == org
|
||||
}
|
||||
|
||||
func (s *legacyStorage) GetSingularName() string {
|
||||
return strings.ToLower(playlist.PlaylistKind().Kind())
|
||||
}
|
||||
|
||||
func (s *legacyStorage) NewList() runtime.Object {
|
||||
return playlist.PlaylistKind().ZeroListValue()
|
||||
}
|
||||
|
||||
func (s *legacyStorage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||
return s.tableConverter.ConvertToTable(ctx, object, tableOptions)
|
||||
}
|
||||
|
||||
func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
||||
orgId, err := request.OrgIDForList(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res, err := s.service.List(ctx, orgId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list := &playlist.PlaylistList{}
|
||||
for idx := range res {
|
||||
list.Items = append(list.Items, *convertToK8sResource(&res[idx], s.namespacer))
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *legacyStorage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
||||
info, err := request.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dto, err := s.service.Get(ctx, &playlistsvc.GetPlaylistByUidQuery{
|
||||
UID: name,
|
||||
OrgId: info.OrgID,
|
||||
})
|
||||
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)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return convertToK8sResource(dto, s.namespacer), nil
|
||||
}
|
||||
|
||||
func (s *legacyStorage) Create(ctx context.Context,
|
||||
obj runtime.Object,
|
||||
createValidation rest.ValidateObjectFunc,
|
||||
options *metav1.CreateOptions,
|
||||
) (runtime.Object, error) {
|
||||
info, err := request.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, ok := obj.(*playlist.Playlist)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected playlist?")
|
||||
}
|
||||
cmd, err := convertToLegacyUpdateCommand(p, info.OrgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := s.service.Create(ctx, &playlistsvc.CreatePlaylistCommand{
|
||||
UID: p.Name,
|
||||
Name: cmd.Name,
|
||||
Interval: cmd.Interval,
|
||||
Items: cmd.Items,
|
||||
OrgId: cmd.OrgId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.Get(ctx, out.UID, nil)
|
||||
}
|
||||
|
||||
func (s *legacyStorage) Update(ctx context.Context,
|
||||
name string,
|
||||
objInfo rest.UpdatedObjectInfo,
|
||||
createValidation rest.ValidateObjectFunc,
|
||||
updateValidation rest.ValidateObjectUpdateFunc,
|
||||
forceAllowCreate bool,
|
||||
options *metav1.UpdateOptions,
|
||||
) (runtime.Object, bool, error) {
|
||||
info, err := request.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
created := false
|
||||
old, err := s.Get(ctx, name, nil)
|
||||
if err != nil {
|
||||
return old, created, err
|
||||
}
|
||||
|
||||
obj, err := objInfo.UpdatedObject(ctx, old)
|
||||
if err != nil {
|
||||
return old, created, err
|
||||
}
|
||||
p, ok := obj.(*playlist.Playlist)
|
||||
if !ok {
|
||||
return nil, created, fmt.Errorf("expected playlist after update")
|
||||
}
|
||||
|
||||
cmd, err := convertToLegacyUpdateCommand(p, info.OrgID)
|
||||
if err != nil {
|
||||
return old, created, err
|
||||
}
|
||||
_, err = s.service.Update(ctx, cmd)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
r, err := s.Get(ctx, name, nil)
|
||||
return r, created, err
|
||||
}
|
||||
|
||||
// GracefulDeleter
|
||||
func (s *legacyStorage) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
||||
v, err := s.Get(ctx, name, &metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return v, false, err // includes the not-found error
|
||||
}
|
||||
info, err := request.NamespaceInfoFrom(ctx, true)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
p, ok := v.(*playlist.Playlist)
|
||||
if !ok {
|
||||
return v, false, fmt.Errorf("expected a playlist response from Get")
|
||||
}
|
||||
err = s.service.Delete(ctx, &playlistsvc.DeletePlaylistCommand{
|
||||
UID: name,
|
||||
OrgId: info.OrgID,
|
||||
})
|
||||
return p, true, err // true is instant delete
|
||||
}
|
||||
|
||||
// CollectionDeleter
|
||||
func (s *legacyStorage) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) {
|
||||
return nil, fmt.Errorf("DeleteCollection for playlists not implemented")
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package playlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/app"
|
||||
"github.com/grafana/grafana-app-sdk/simple"
|
||||
"github.com/grafana/grafana/apps/playlist/pkg/apis"
|
||||
playlistv0alpha1 "github.com/grafana/grafana/apps/playlist/pkg/apis/playlist/v0alpha1"
|
||||
playlistapp "github.com/grafana/grafana/apps/playlist/pkg/app"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/builder/runner"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
playlistsvc "github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type PlaylistAppProvider struct {
|
||||
app.Provider
|
||||
cfg *setting.Cfg
|
||||
service playlistsvc.Service
|
||||
}
|
||||
|
||||
func RegisterApp(
|
||||
p playlistsvc.Service,
|
||||
cfg *setting.Cfg,
|
||||
features featuremgmt.FeatureToggles,
|
||||
) *PlaylistAppProvider {
|
||||
provider := &PlaylistAppProvider{
|
||||
cfg: cfg,
|
||||
service: p,
|
||||
}
|
||||
appCfg := &runner.AppBuilderConfig{
|
||||
OpenAPIDefGetter: playlistv0alpha1.GetOpenAPIDefinitions,
|
||||
LegacyStorageGetter: provider.legacyStorageGetter,
|
||||
ManagedKinds: playlistapp.GetKinds(),
|
||||
CustomConfig: any(&playlistapp.PlaylistConfig{
|
||||
EnableWatchers: features.IsEnabledGlobally(featuremgmt.FlagPlaylistsWatcher),
|
||||
}),
|
||||
}
|
||||
provider.Provider = simple.NewAppProvider(apis.LocalManifest(), appCfg, playlistapp.New)
|
||||
return provider
|
||||
}
|
||||
|
||||
func (p *PlaylistAppProvider) legacyStorageGetter(requested schema.GroupVersionResource) grafanarest.LegacyStorage {
|
||||
gvr := schema.GroupVersionResource{
|
||||
Group: playlistv0alpha1.PlaylistKind().Group(),
|
||||
Version: playlistv0alpha1.PlaylistKind().Version(),
|
||||
Resource: playlistv0alpha1.PlaylistKind().Plural(),
|
||||
}
|
||||
if requested.String() != gvr.String() {
|
||||
return nil
|
||||
}
|
||||
legacyStore := &legacyStorage{
|
||||
service: p.service,
|
||||
namespacer: request.GetNamespaceMapper(p.cfg),
|
||||
}
|
||||
legacyStore.tableConverter = utils.NewTableConverter(
|
||||
gvr.GroupResource(),
|
||||
utils.TableColumns{
|
||||
Definition: []metav1.TableColumnDefinition{
|
||||
{Name: "Name", Type: "string", Format: "name"},
|
||||
{Name: "Title", Type: "string", Format: "string", Description: "The playlist name"},
|
||||
{Name: "Interval", Type: "string", Format: "string", Description: "How often the playlist will update"},
|
||||
{Name: "Created At", Type: "date"},
|
||||
},
|
||||
Reader: func(obj any) ([]interface{}, error) {
|
||||
m, ok := obj.(*playlistv0alpha1.Playlist)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected playlist")
|
||||
}
|
||||
return []interface{}{
|
||||
m.Name,
|
||||
m.Spec.Title,
|
||||
m.Spec.Interval,
|
||||
m.CreationTimestamp.UTC().Format(time.RFC3339),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
)
|
||||
return legacyStore
|
||||
}
|
||||
@@ -2,8 +2,11 @@ package appregistry
|
||||
|
||||
import (
|
||||
"github.com/google/wire"
|
||||
|
||||
"github.com/grafana/grafana/pkg/registry/apps/playlist"
|
||||
)
|
||||
|
||||
var WireSet = wire.NewSet(
|
||||
ProvideRegistryServiceSink,
|
||||
playlist.RegisterApp,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user