Snapshots: Add support for dual write
This commit is contained in:
@@ -122,6 +122,7 @@ type DashboardsAPIBuilder struct {
|
|||||||
publicDashboardService publicdashboards.Service
|
publicDashboardService publicdashboards.Service
|
||||||
snapshotService dashboardsnapshots.Service
|
snapshotService dashboardsnapshots.Service
|
||||||
snapshotOptions dashv0.SnapshotSharingOptions
|
snapshotOptions dashv0.SnapshotSharingOptions
|
||||||
|
snapshotStorage rest.Storage // for dual-write support in routes
|
||||||
namespacer request.NamespaceMapper
|
namespacer request.NamespaceMapper
|
||||||
dashboardActivityChannel live.DashboardActivityChannel
|
dashboardActivityChannel live.DashboardActivityChannel
|
||||||
isStandalone bool // skips any handling including anything to do with legacy storage
|
isStandalone bool // skips any handling including anything to do with legacy storage
|
||||||
@@ -747,15 +748,26 @@ func (b *DashboardsAPIBuilder) storageForVersion(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy only (for now) and only v0alpha1
|
// Snapshots - only v0alpha1
|
||||||
if snapshots != nil && dashboards.GroupVersion().Version == "v0alpha1" {
|
if snapshots != nil && dashboards.GroupVersion().Version == "v0alpha1" {
|
||||||
snapshotLegacyStore := &snapshot.SnapshotLegacyStore{
|
snapshotLegacyStore := &snapshot.SnapshotLegacyStore{
|
||||||
ResourceInfo: *snapshots,
|
ResourceInfo: *snapshots,
|
||||||
Service: b.snapshotService,
|
Service: b.snapshotService,
|
||||||
Namespacer: b.namespacer,
|
Namespacer: b.namespacer,
|
||||||
}
|
}
|
||||||
storage[snapshots.StoragePath()] = snapshotLegacyStore
|
|
||||||
storage[snapshots.StoragePath("dashboard")], err = snapshot.NewDashboardREST(dashboards, b.snapshotService)
|
unifiedSnapshotStore, err := grafanaregistry.NewRegistryStore(opts.Scheme, *snapshots, opts.OptsGetter)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
snapshotGr := snapshots.GroupResource()
|
||||||
|
snapshotDualWrite, err := opts.DualWriteBuilder(snapshotGr, snapshotLegacyStore, unifiedSnapshotStore)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
storage[snapshots.StoragePath()] = snapshotDualWrite
|
||||||
|
b.snapshotStorage = snapshotDualWrite // store for use in routes
|
||||||
|
storage[snapshots.StoragePath("dashboard")], err = snapshot.NewDashboardREST(snapshotDualWrite)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -979,7 +991,9 @@ func (b *DashboardsAPIBuilder) GetAPIRoutes(gv schema.GroupVersion) *builder.API
|
|||||||
|
|
||||||
defs := b.GetOpenAPIDefinitions()(func(path string) spec.Ref { return spec.Ref{} })
|
defs := b.GetOpenAPIDefinitions()(func(path string) spec.Ref { return spec.Ref{} })
|
||||||
searchAPIRoutes := b.search.GetAPIRoutes(defs)
|
searchAPIRoutes := b.search.GetAPIRoutes(defs)
|
||||||
snapshotAPIRoutes := snapshot.GetRoutes(b.snapshotService, b.snapshotOptions, defs)
|
snapshotAPIRoutes := snapshot.GetRoutes(b.snapshotService, b.snapshotOptions, defs, func() rest.Storage {
|
||||||
|
return b.snapshotStorage
|
||||||
|
})
|
||||||
|
|
||||||
return &builder.APIRoutes{
|
return &builder.APIRoutes{
|
||||||
Namespace: append(searchAPIRoutes.Namespace, snapshotAPIRoutes.Namespace...),
|
Namespace: append(searchAPIRoutes.Namespace, snapshotAPIRoutes.Namespace...),
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
dashV0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
dashV0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||||
|
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
||||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
||||||
@@ -81,3 +82,68 @@ func convertSnapshotToK8sResource(v *dashboardsnapshots.DashboardSnapshot, names
|
|||||||
}
|
}
|
||||||
return snap
|
return snap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertK8sResourceToCreateCommand converts a K8s Snapshot to a CreateDashboardSnapshotCommand
|
||||||
|
func convertK8sResourceToCreateCommand(snap *dashV0.Snapshot, orgID int64, userID int64) *dashboardsnapshots.CreateDashboardSnapshotCommand {
|
||||||
|
cmd := &dashboardsnapshots.CreateDashboardSnapshotCommand{
|
||||||
|
OrgID: orgID,
|
||||||
|
UserID: userID,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map title
|
||||||
|
if snap.Spec.Title != nil {
|
||||||
|
cmd.Name = *snap.Spec.Title
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map dashboard (convert map[string]interface{} to *common.Unstructured)
|
||||||
|
if snap.Spec.Dashboard != nil {
|
||||||
|
cmd.Dashboard = &common.Unstructured{Object: snap.Spec.Dashboard}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map expires
|
||||||
|
if snap.Spec.Expires != nil {
|
||||||
|
cmd.Expires = *snap.Spec.Expires
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map external settings
|
||||||
|
if snap.Spec.External != nil && *snap.Spec.External {
|
||||||
|
cmd.External = true
|
||||||
|
if snap.Spec.ExternalUrl != nil {
|
||||||
|
cmd.ExternalURL = *snap.Spec.ExternalUrl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// convertCreateCmdToK8sSnapshot converts a CreateDashboardSnapshotCommand request to a K8s Snapshot
|
||||||
|
// Used by routes.go to create a Snapshot object from the incoming create command
|
||||||
|
func convertCreateCmdToK8sSnapshot(cmd *dashboardsnapshots.CreateDashboardSnapshotCommand, namespace string) *dashV0.Snapshot {
|
||||||
|
snap := &dashV0.Snapshot{
|
||||||
|
TypeMeta: dashV0.SnapshotResourceInfo.TypeMeta(),
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Namespace: namespace,
|
||||||
|
},
|
||||||
|
Spec: dashV0.SnapshotSpec{
|
||||||
|
Title: &cmd.Name,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert *common.Unstructured to map[string]interface{}
|
||||||
|
if cmd.Dashboard != nil {
|
||||||
|
snap.Spec.Dashboard = cmd.Dashboard.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmd.Expires > 0 {
|
||||||
|
snap.Spec.Expires = &cmd.Expires
|
||||||
|
}
|
||||||
|
|
||||||
|
if cmd.External {
|
||||||
|
snap.Spec.External = &cmd.External
|
||||||
|
if cmd.ExternalURL != "" {
|
||||||
|
snap.Spec.ExternalUrl = &cmd.ExternalURL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return snap
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
k8srequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||||
|
"k8s.io/apiserver/pkg/registry/rest"
|
||||||
"k8s.io/kube-openapi/pkg/common"
|
"k8s.io/kube-openapi/pkg/common"
|
||||||
"k8s.io/kube-openapi/pkg/spec3"
|
"k8s.io/kube-openapi/pkg/spec3"
|
||||||
"k8s.io/kube-openapi/pkg/validation/spec"
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||||
@@ -14,6 +17,7 @@ import (
|
|||||||
authlib "github.com/grafana/authlib/types"
|
authlib "github.com/grafana/authlib/types"
|
||||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||||
|
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||||
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
||||||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
||||||
@@ -22,7 +26,7 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/web"
|
"github.com/grafana/grafana/pkg/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRoutes(service dashboardsnapshots.Service, options dashv0.SnapshotSharingOptions, defs map[string]common.OpenAPIDefinition) *builder.APIRoutes {
|
func GetRoutes(service dashboardsnapshots.Service, options dashv0.SnapshotSharingOptions, defs map[string]common.OpenAPIDefinition, storageGetter func() rest.Storage) *builder.APIRoutes {
|
||||||
prefix := dashv0.SnapshotResourceInfo.GroupResource().Resource
|
prefix := dashv0.SnapshotResourceInfo.GroupResource().Resource
|
||||||
tags := []string{dashv0.SnapshotResourceInfo.GroupVersionKind().Kind}
|
tags := []string{dashv0.SnapshotResourceInfo.GroupVersionKind().Kind}
|
||||||
|
|
||||||
@@ -97,9 +101,10 @@ func GetRoutes(service dashboardsnapshots.Service, options dashv0.SnapshotSharin
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := identity.GetRequester(r.Context())
|
ctx := r.Context()
|
||||||
|
user, err := identity.GetRequester(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errhttp.Write(r.Context(), err, w)
|
errhttp.Write(ctx, err, w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
wrap := &contextmodel.ReqContext{
|
wrap := &contextmodel.ReqContext{
|
||||||
@@ -107,11 +112,15 @@ func GetRoutes(service dashboardsnapshots.Service, options dashv0.SnapshotSharin
|
|||||||
Req: r,
|
Req: r,
|
||||||
Resp: web.NewResponseWriter(r.Method, w),
|
Resp: web.NewResponseWriter(r.Method, w),
|
||||||
},
|
},
|
||||||
// SignedInUser: user, ????????????
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !options.SnapshotsEnabled {
|
||||||
|
wrap.JsonApiErr(http.StatusForbidden, "Dashboard Snapshots are disabled", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
info, err := authlib.ParseNamespace(vars["namespace"])
|
namespace := vars["namespace"]
|
||||||
|
info, err := authlib.ParseNamespace(namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
wrap.JsonApiErr(http.StatusBadRequest, "expected namespace", nil)
|
wrap.JsonApiErr(http.StatusBadRequest, "expected namespace", nil)
|
||||||
return
|
return
|
||||||
@@ -128,8 +137,82 @@ func GetRoutes(service dashboardsnapshots.Service, options dashv0.SnapshotSharin
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the existing snapshot service
|
if cmd.External && !options.ExternalEnabled {
|
||||||
dashboardsnapshots.CreateDashboardSnapshot(wrap, options, cmd, service)
|
wrap.JsonApiErr(http.StatusForbidden, "External dashboard creation is disabled", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// fill cmd data
|
||||||
|
if cmd.Name == "" {
|
||||||
|
cmd.Name = "Unnamed snapshot"
|
||||||
|
}
|
||||||
|
cmd.OrgID = user.GetOrgID()
|
||||||
|
cmd.UserID, _ = identity.UserIdentifier(user.GetID())
|
||||||
|
|
||||||
|
//originalDashboardURL, err := dashboardsnapshots.CreateOriginalDashboardURL(&cmd)
|
||||||
|
|
||||||
|
// TODO: add logic for external and internal snapshots
|
||||||
|
if cmd.External {
|
||||||
|
// TODO: if it is an external dashboard make a POST to the public snapshot server
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: validate dashboard exists. Need to call dashboards api, Maybe in a validation hook?
|
||||||
|
|
||||||
|
storage := storageGetter()
|
||||||
|
if storage == nil {
|
||||||
|
errhttp.Write(ctx, fmt.Errorf("snapshot storage not available"), w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
creater, ok := storage.(rest.Creater)
|
||||||
|
if !ok {
|
||||||
|
errhttp.Write(ctx, fmt.Errorf("snapshot storage does not support create"), w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert command to K8s Snapshot
|
||||||
|
snapshot := convertCreateCmdToK8sSnapshot(&cmd, namespace)
|
||||||
|
|
||||||
|
snapshot.SetGenerateName("snapshot-")
|
||||||
|
|
||||||
|
// Set namespace in context for k8s storage layer
|
||||||
|
ctx = k8srequest.WithNamespace(ctx, namespace)
|
||||||
|
|
||||||
|
// Create via storage (dual-write mode decides legacy, unified, or both)
|
||||||
|
result, err := creater.Create(ctx, snapshot, nil, &metav1.CreateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
errhttp.Write(ctx, err, w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract key and deleteKey from result
|
||||||
|
accessor, err := utils.MetaAccessor(result)
|
||||||
|
if err != nil {
|
||||||
|
errhttp.Write(ctx, fmt.Errorf("failed to access result metadata: %w", err), w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteKey, err := util.GetRandomString(32)
|
||||||
|
if err != nil {
|
||||||
|
errhttp.Write(ctx, fmt.Errorf("failed to generate delete key: %w", err), w)
|
||||||
|
}
|
||||||
|
|
||||||
|
key := accessor.GetName()
|
||||||
|
//deleteKey := ""
|
||||||
|
//if annotations := accessor.GetAnnotations(); annotations != nil {
|
||||||
|
// deleteKey = annotations["grafana.app/delete-key"]
|
||||||
|
//}
|
||||||
|
|
||||||
|
// Build response
|
||||||
|
response := dashv0.DashboardCreateResponse{
|
||||||
|
Key: key,
|
||||||
|
DeleteKey: deleteKey,
|
||||||
|
URL: setting.ToAbsUrl("dashboard/snapshot/" + key),
|
||||||
|
DeleteURL: setting.ToAbsUrl("api/snapshots-delete/" + deleteKey),
|
||||||
|
}
|
||||||
|
|
||||||
|
wrap.JSON(http.StatusOK, response)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package snapshot
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -20,7 +21,10 @@ var (
|
|||||||
_ rest.SingularNameProvider = (*SnapshotLegacyStore)(nil)
|
_ rest.SingularNameProvider = (*SnapshotLegacyStore)(nil)
|
||||||
_ rest.Getter = (*SnapshotLegacyStore)(nil)
|
_ rest.Getter = (*SnapshotLegacyStore)(nil)
|
||||||
_ rest.Lister = (*SnapshotLegacyStore)(nil)
|
_ rest.Lister = (*SnapshotLegacyStore)(nil)
|
||||||
|
_ rest.Creater = (*SnapshotLegacyStore)(nil)
|
||||||
|
_ rest.Updater = (*SnapshotLegacyStore)(nil)
|
||||||
_ rest.GracefulDeleter = (*SnapshotLegacyStore)(nil)
|
_ rest.GracefulDeleter = (*SnapshotLegacyStore)(nil)
|
||||||
|
_ rest.CollectionDeleter = (*SnapshotLegacyStore)(nil)
|
||||||
_ rest.Storage = (*SnapshotLegacyStore)(nil)
|
_ rest.Storage = (*SnapshotLegacyStore)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -129,3 +133,51 @@ func (s *SnapshotLegacyStore) Get(ctx context.Context, name string, options *met
|
|||||||
}
|
}
|
||||||
return nil, s.ResourceInfo.NewNotFound(name)
|
return nil, s.ResourceInfo.NewNotFound(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create implements rest.Creater
|
||||||
|
func (s *SnapshotLegacyStore) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
|
||||||
|
snap, ok := obj.(*dashV0.Snapshot)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("expected Snapshot object, got %T", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run validation if provided
|
||||||
|
if createValidation != nil {
|
||||||
|
if err := createValidation(ctx, obj); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user identity from context
|
||||||
|
requester, err := identity.GetRequester(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get requester: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := requester.GetInternalID()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get user ID: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert K8s resource to service command
|
||||||
|
cmd := convertK8sResourceToCreateCommand(snap, requester.GetOrgID(), userID)
|
||||||
|
|
||||||
|
// Create the snapshot via service
|
||||||
|
result, err := s.Service.CreateDashboardSnapshot(ctx, cmd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert result back to K8s resource
|
||||||
|
return convertSnapshotToK8sResource(result, s.Namespacer), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update implements rest.Updater - snapshots are immutable, so this returns an error
|
||||||
|
func (s *SnapshotLegacyStore) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
|
||||||
|
return nil, false, fmt.Errorf("snapshots are immutable and cannot be updated")
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteCollection implements rest.CollectionDeleter
|
||||||
|
func (s *SnapshotLegacyStore) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *internalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
return nil, fmt.Errorf("delete collection is not supported for snapshots")
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package snapshot
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -10,22 +11,19 @@ import (
|
|||||||
|
|
||||||
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
|
||||||
"github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
"github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
||||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
|
||||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Currently only works with v0alpha1
|
// Currently only works with v0alpha1
|
||||||
type dashboardREST struct {
|
type dashboardREST struct {
|
||||||
Service dashboardsnapshots.Service
|
getter rest.Getter
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDashboardREST(
|
func NewDashboardREST(
|
||||||
resourceInfo utils.ResourceInfo,
|
getter rest.Getter,
|
||||||
service dashboardsnapshots.Service,
|
|
||||||
) (rest.Storage, error) {
|
) (rest.Storage, error) {
|
||||||
return &dashboardREST{
|
return &dashboardREST{
|
||||||
Service: service,
|
getter: getter,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,22 +56,30 @@ func (r *dashboardREST) ProducesObject(verb string) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *dashboardREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
func (r *dashboardREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
||||||
_, err := request.NamespaceInfoFrom(ctx, true)
|
ns, err := request.NamespaceInfoFrom(ctx, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
snap, err := r.Service.GetDashboardSnapshot(ctx, &dashboardsnapshots.GetDashboardSnapshotQuery{Key: name})
|
|
||||||
|
// Get the snapshot from unified storage
|
||||||
|
obj, err := r.getter.Get(ctx, name, &metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
snap, ok := obj.(*dashv0.Snapshot)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("expected Snapshot, got %T", obj)
|
||||||
|
}
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
// TODO... support conversions (not required in v0)
|
// TODO... support conversions (not required in v0)
|
||||||
dash := &dashv0.Dashboard{
|
dash := &dashv0.Dashboard{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Namespace: name,
|
Namespace: ns.Value,
|
||||||
},
|
},
|
||||||
Spec: v0alpha1.Unstructured{
|
Spec: v0alpha1.Unstructured{
|
||||||
Object: snap.Dashboard.MustMap(),
|
Object: snap.Spec.Dashboard,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
responder.Object(200, dash)
|
responder.Object(200, dash)
|
||||||
|
|||||||
@@ -37,10 +37,15 @@ var client = &http.Client{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateDashboardSnapshot(c *contextmodel.ReqContext, cfg snapshot.SnapshotSharingOptions, cmd CreateDashboardSnapshotCommand, svc Service) {
|
func CreateDashboardSnapshot(c *contextmodel.ReqContext, cfg snapshot.SnapshotSharingOptions, cmd CreateDashboardSnapshotCommand, svc Service) {
|
||||||
|
// perform all validations in the beginning
|
||||||
if !cfg.SnapshotsEnabled {
|
if !cfg.SnapshotsEnabled {
|
||||||
c.JsonApiErr(http.StatusForbidden, "Dashboard Snapshots are disabled", nil)
|
c.JsonApiErr(http.StatusForbidden, "Dashboard Snapshots are disabled", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if cmd.External && !cfg.ExternalEnabled {
|
||||||
|
c.JsonApiErr(http.StatusForbidden, "External dashboard creation is disabled", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
uid := cmd.Dashboard.GetNestedString("uid")
|
uid := cmd.Dashboard.GetNestedString("uid")
|
||||||
user, err := identity.GetRequester(c.Req.Context())
|
user, err := identity.GetRequester(c.Req.Context())
|
||||||
@@ -67,17 +72,17 @@ func CreateDashboardSnapshot(c *contextmodel.ReqContext, cfg snapshot.SnapshotSh
|
|||||||
cmd.ExternalURL = ""
|
cmd.ExternalURL = ""
|
||||||
cmd.OrgID = user.GetOrgID()
|
cmd.OrgID = user.GetOrgID()
|
||||||
cmd.UserID, _ = identity.UserIdentifier(user.GetID())
|
cmd.UserID, _ = identity.UserIdentifier(user.GetID())
|
||||||
originalDashboardURL, err := createOriginalDashboardURL(&cmd)
|
originalDashboardURL, err := CreateOriginalDashboardURL(&cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(http.StatusInternalServerError, "Invalid app URL", err)
|
c.JsonApiErr(http.StatusInternalServerError, "Invalid app URL", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.External {
|
if cmd.External {
|
||||||
if !cfg.ExternalEnabled {
|
//if !cfg.ExternalEnabled {
|
||||||
c.JsonApiErr(http.StatusForbidden, "External dashboard creation is disabled", nil)
|
// c.JsonApiErr(http.StatusForbidden, "External dashboard creation is disabled", nil)
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
|
|
||||||
resp, err := createExternalDashboardSnapshot(cmd, cfg.ExternalSnapshotURL)
|
resp, err := createExternalDashboardSnapshot(cmd, cfg.ExternalSnapshotURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -203,7 +208,7 @@ func createExternalDashboardSnapshot(cmd CreateDashboardSnapshotCommand, externa
|
|||||||
return &createSnapshotResponse, nil
|
return &createSnapshotResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createOriginalDashboardURL(cmd *CreateDashboardSnapshotCommand) (string, error) {
|
func CreateOriginalDashboardURL(cmd *CreateDashboardSnapshotCommand) (string, error) {
|
||||||
dashUID := cmd.Dashboard.GetNestedString("uid")
|
dashUID := cmd.Dashboard.GetNestedString("uid")
|
||||||
if ok := util.IsValidShortUID(dashUID); !ok {
|
if ok := util.IsValidShortUID(dashUID); !ok {
|
||||||
return "", fmt.Errorf("invalid dashboard UID")
|
return "", fmt.Errorf("invalid dashboard UID")
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { lastValueFrom, map } from 'rxjs';
|
import { lastValueFrom } from 'rxjs';
|
||||||
|
|
||||||
import { config, getBackendSrv, FetchResponse } from '@grafana/runtime';
|
import { config, getBackendSrv } from '@grafana/runtime';
|
||||||
import { contextSrv } from 'app/core/services/context_srv';
|
import { contextSrv } from 'app/core/services/context_srv';
|
||||||
import { DashboardDTO, SnapshotSpec } from 'app/types/dashboard';
|
import { DashboardDataDTO, DashboardDTO } from 'app/types/dashboard';
|
||||||
|
|
||||||
import { getAPINamespace } from '../../../api/utils';
|
import { getAPINamespace } from '../../../api/utils';
|
||||||
|
|
||||||
@@ -82,11 +82,12 @@ interface DashboardSnapshotList {
|
|||||||
items: K8sSnapshotResource[];
|
items: K8sSnapshotResource[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface K8sDashboardSnapshot {
|
// Response from the /dashboard subresource - returns a Dashboard with raw dashboard data in spec
|
||||||
|
interface K8sDashboardSubresource {
|
||||||
apiVersion: string;
|
apiVersion: string;
|
||||||
kind: 'Snapshot';
|
kind: 'Dashboard';
|
||||||
metadata: K8sMetadata;
|
metadata: K8sMetadata;
|
||||||
spec: SnapshotSpec;
|
spec: DashboardDataDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
class K8sAPI implements DashboardSnapshotSrv {
|
class K8sAPI implements DashboardSnapshotSrv {
|
||||||
@@ -128,32 +129,45 @@ class K8sAPI implements DashboardSnapshotSrv {
|
|||||||
const token = `??? TODO, get anon token for snapshots (${contextSrv.user?.name}) ???`;
|
const token = `??? TODO, get anon token for snapshots (${contextSrv.user?.name}) ???`;
|
||||||
headers['Authorization'] = `Bearer ${token}`;
|
headers['Authorization'] = `Bearer ${token}`;
|
||||||
}
|
}
|
||||||
return lastValueFrom(
|
|
||||||
getBackendSrv()
|
// Fetch both snapshot metadata and dashboard content in parallel
|
||||||
.fetch<K8sDashboardSnapshot>({
|
const [snapshotResponse, dashboardResponse] = await Promise.all([
|
||||||
|
lastValueFrom(
|
||||||
|
getBackendSrv().fetch<K8sSnapshotResource>({
|
||||||
url: this.url + '/' + uid,
|
url: this.url + '/' + uid,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: headers,
|
headers: headers,
|
||||||
})
|
})
|
||||||
.pipe(
|
),
|
||||||
map((response: FetchResponse<K8sDashboardSnapshot>) => {
|
lastValueFrom(
|
||||||
return {
|
getBackendSrv().fetch<K8sDashboardSubresource>({
|
||||||
dashboard: response.data.spec.dashboard,
|
url: this.url + '/' + uid + '/dashboard',
|
||||||
meta: {
|
method: 'GET',
|
||||||
isSnapshot: true,
|
headers: headers,
|
||||||
canSave: false,
|
})
|
||||||
canEdit: false,
|
),
|
||||||
canAdmin: false,
|
]);
|
||||||
canStar: false,
|
|
||||||
canShare: false,
|
const snapshot = snapshotResponse.data;
|
||||||
canDelete: false,
|
const dashboard = dashboardResponse.data;
|
||||||
isFolder: false,
|
|
||||||
provisioned: false,
|
return {
|
||||||
},
|
dashboard: dashboard.spec,
|
||||||
};
|
meta: {
|
||||||
})
|
isSnapshot: true,
|
||||||
)
|
canSave: false,
|
||||||
);
|
canEdit: false,
|
||||||
|
canAdmin: false,
|
||||||
|
canStar: false,
|
||||||
|
canShare: false,
|
||||||
|
canDelete: false,
|
||||||
|
isFolder: false,
|
||||||
|
provisioned: false,
|
||||||
|
created: snapshot.metadata.creationTimestamp,
|
||||||
|
expires: snapshot.spec.expires?.toString(),
|
||||||
|
k8s: snapshot.metadata,
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user