2024-01-10 04:26:24 +08:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
|
|
|
genericapiserver "k8s.io/apiserver/pkg/server"
|
2024-01-13 04:05:30 +08:00
|
|
|
openapi "k8s.io/kube-openapi/pkg/common"
|
2024-03-02 06:26:04 +08:00
|
|
|
"k8s.io/kube-openapi/pkg/spec3"
|
2024-01-10 04:26:24 +08:00
|
|
|
"k8s.io/utils/strings/slices"
|
|
|
|
|
2024-01-23 03:32:25 +08:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
|
2024-02-24 04:15:43 +08:00
|
|
|
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
2024-03-02 06:26:04 +08:00
|
|
|
datasource "github.com/grafana/grafana/pkg/apis/datasource/v0alpha1"
|
2024-02-01 02:36:51 +08:00
|
|
|
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
2024-02-24 04:15:43 +08:00
|
|
|
"github.com/grafana/grafana/pkg/apiserver/builder"
|
2024-01-10 04:26:24 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2024-02-02 06:27:30 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/utils"
|
2024-01-10 04:26:24 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
|
|
|
)
|
|
|
|
|
2024-02-02 06:27:30 +08:00
|
|
|
var _ builder.APIGroupBuilder = (*DataSourceAPIBuilder)(nil)
|
2024-01-10 04:26:24 +08:00
|
|
|
|
2024-01-19 22:56:52 +08:00
|
|
|
// DataSourceAPIBuilder is used just so wire has something unique to return
|
2024-01-10 04:26:24 +08:00
|
|
|
type DataSourceAPIBuilder struct {
|
2024-01-13 04:05:30 +08:00
|
|
|
connectionResourceInfo common.ResourceInfo
|
2024-01-10 04:26:24 +08:00
|
|
|
|
2024-01-23 03:32:25 +08:00
|
|
|
pluginJSON plugins.JSONData
|
2024-02-26 20:02:55 +08:00
|
|
|
client PluginClient // will only ever be called with the same pluginid!
|
2024-01-24 23:44:40 +08:00
|
|
|
datasources PluginDatasourceProvider
|
2024-01-23 03:32:25 +08:00
|
|
|
contextProvider PluginContextWrapper
|
|
|
|
accessControl accesscontrol.AccessControl
|
2024-01-10 04:26:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterAPIService(
|
|
|
|
features featuremgmt.FeatureToggles,
|
2024-02-02 06:27:30 +08:00
|
|
|
apiRegistrar builder.APIRegistrar,
|
2024-01-23 03:32:25 +08:00
|
|
|
pluginClient plugins.Client, // access to everything
|
2024-01-24 23:44:40 +08:00
|
|
|
datasources PluginDatasourceProvider,
|
2024-01-23 03:32:25 +08:00
|
|
|
contextProvider PluginContextWrapper,
|
2024-01-10 04:26:24 +08:00
|
|
|
pluginStore pluginstore.Store,
|
|
|
|
accessControl accesscontrol.AccessControl,
|
|
|
|
) (*DataSourceAPIBuilder, error) {
|
|
|
|
// This requires devmode!
|
|
|
|
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
|
|
|
|
return nil, nil // skip registration unless opting into experimental apis
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var builder *DataSourceAPIBuilder
|
|
|
|
all := pluginStore.Plugins(context.Background(), plugins.TypeDataSource)
|
|
|
|
ids := []string{
|
|
|
|
"grafana-testdata-datasource",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range all {
|
|
|
|
if !slices.Contains(ids, ds.ID) {
|
|
|
|
continue // skip this one
|
|
|
|
}
|
|
|
|
|
2024-01-23 03:32:25 +08:00
|
|
|
builder, err = NewDataSourceAPIBuilder(ds.JSONData,
|
|
|
|
pluginClient,
|
2024-01-24 23:44:40 +08:00
|
|
|
datasources,
|
2024-01-23 03:32:25 +08:00
|
|
|
contextProvider,
|
|
|
|
accessControl,
|
|
|
|
)
|
2024-01-10 04:26:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-19 22:56:52 +08:00
|
|
|
apiRegistrar.RegisterAPI(builder)
|
2024-01-10 04:26:24 +08:00
|
|
|
}
|
|
|
|
return builder, nil // only used for wire
|
|
|
|
}
|
|
|
|
|
2024-02-26 20:02:55 +08:00
|
|
|
// PluginClient is a subset of the plugins.Client interface with only the
|
|
|
|
// functions supported (yet) by the datasource API
|
|
|
|
type PluginClient interface {
|
|
|
|
backend.QueryDataHandler
|
|
|
|
backend.CheckHealthHandler
|
|
|
|
backend.CallResourceHandler
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:26:24 +08:00
|
|
|
func NewDataSourceAPIBuilder(
|
2024-01-10 23:45:23 +08:00
|
|
|
plugin plugins.JSONData,
|
2024-02-26 20:02:55 +08:00
|
|
|
client PluginClient,
|
2024-01-24 23:44:40 +08:00
|
|
|
datasources PluginDatasourceProvider,
|
2024-01-23 03:32:25 +08:00
|
|
|
contextProvider PluginContextWrapper,
|
2024-01-10 23:45:23 +08:00
|
|
|
accessControl accesscontrol.AccessControl) (*DataSourceAPIBuilder, error) {
|
2024-01-19 22:56:52 +08:00
|
|
|
ri, err := resourceFromPluginID(plugin.ID)
|
2024-01-10 04:26:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-19 22:56:52 +08:00
|
|
|
|
2024-01-10 04:26:24 +08:00
|
|
|
return &DataSourceAPIBuilder{
|
2024-01-19 22:56:52 +08:00
|
|
|
connectionResourceInfo: ri,
|
|
|
|
pluginJSON: plugin,
|
2024-01-23 03:32:25 +08:00
|
|
|
client: client,
|
2024-01-24 23:44:40 +08:00
|
|
|
datasources: datasources,
|
2024-01-23 03:32:25 +08:00
|
|
|
contextProvider: contextProvider,
|
2024-01-10 04:26:24 +08:00
|
|
|
accessControl: accessControl,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *DataSourceAPIBuilder) GetGroupVersion() schema.GroupVersion {
|
|
|
|
return b.connectionResourceInfo.GroupVersion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func addKnownTypes(scheme *runtime.Scheme, gv schema.GroupVersion) {
|
|
|
|
scheme.AddKnownTypes(gv,
|
2024-03-02 06:26:04 +08:00
|
|
|
&datasource.DataSourceConnection{},
|
|
|
|
&datasource.DataSourceConnectionList{},
|
|
|
|
&datasource.HealthCheckResult{},
|
2024-01-10 04:26:24 +08:00
|
|
|
&unstructured.Unstructured{},
|
2024-02-01 02:36:51 +08:00
|
|
|
// Query handler
|
|
|
|
&query.QueryDataResponse{},
|
2024-01-10 04:26:24 +08:00
|
|
|
&metav1.Status{},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *DataSourceAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
|
|
|
gv := b.connectionResourceInfo.GroupVersion()
|
|
|
|
addKnownTypes(scheme, gv)
|
|
|
|
|
|
|
|
// Link this version to the internal representation.
|
|
|
|
// This is used for server-side-apply (PATCH), and avoids the error:
|
|
|
|
// "no kind is registered for the type"
|
|
|
|
addKnownTypes(scheme, schema.GroupVersion{
|
|
|
|
Group: gv.Group,
|
|
|
|
Version: runtime.APIVersionInternal,
|
|
|
|
})
|
|
|
|
|
|
|
|
// If multiple versions exist, then register conversions from zz_generated.conversion.go
|
|
|
|
// if err := playlist.RegisterConversions(scheme); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
metav1.AddToGroupVersion(scheme, gv)
|
|
|
|
return scheme.SetVersionPriority(gv)
|
|
|
|
}
|
|
|
|
|
2024-01-19 22:56:52 +08:00
|
|
|
func resourceFromPluginID(pluginID string) (common.ResourceInfo, error) {
|
2024-02-01 02:36:51 +08:00
|
|
|
group, err := plugins.GetDatasourceGroupNameFromPluginID(pluginID)
|
2024-01-19 22:56:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return common.ResourceInfo{}, err
|
|
|
|
}
|
2024-03-02 06:26:04 +08:00
|
|
|
return datasource.GenericConnectionResourceInfo.WithGroupAndShortName(group, pluginID+"-connection"), nil
|
2024-01-19 22:56:52 +08:00
|
|
|
}
|
|
|
|
|
2024-01-10 04:26:24 +08:00
|
|
|
func (b *DataSourceAPIBuilder) GetAPIGroupInfo(
|
|
|
|
scheme *runtime.Scheme,
|
|
|
|
codecs serializer.CodecFactory, // pointer?
|
2024-01-19 22:56:52 +08:00
|
|
|
_ generic.RESTOptionsGetter,
|
2024-02-02 06:27:30 +08:00
|
|
|
_ bool,
|
2024-01-10 04:26:24 +08:00
|
|
|
) (*genericapiserver.APIGroupInfo, error) {
|
|
|
|
storage := map[string]rest.Storage{}
|
|
|
|
|
|
|
|
conn := b.connectionResourceInfo
|
|
|
|
storage[conn.StoragePath()] = &connectionAccess{
|
2024-01-23 03:32:25 +08:00
|
|
|
pluginID: b.pluginJSON.ID,
|
2024-01-24 23:44:40 +08:00
|
|
|
datasources: b.datasources,
|
2024-01-10 04:26:24 +08:00
|
|
|
resourceInfo: conn,
|
|
|
|
tableConverter: utils.NewTableConverter(
|
|
|
|
conn.GroupResource(),
|
|
|
|
[]metav1.TableColumnDefinition{
|
|
|
|
{Name: "Name", Type: "string", Format: "name"},
|
|
|
|
{Name: "Title", Type: "string", Format: "string", Description: "The datasource title"},
|
|
|
|
{Name: "APIVersion", Type: "string", Format: "string", Description: "API Version"},
|
|
|
|
{Name: "Created At", Type: "date"},
|
|
|
|
},
|
|
|
|
func(obj any) ([]interface{}, error) {
|
2024-03-02 06:26:04 +08:00
|
|
|
m, ok := obj.(*datasource.DataSourceConnection)
|
2024-01-10 04:26:24 +08:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("expected connection")
|
|
|
|
}
|
|
|
|
return []interface{}{
|
|
|
|
m.Name,
|
|
|
|
m.Title,
|
|
|
|
m.APIVersion,
|
|
|
|
m.CreationTimestamp.UTC().Format(time.RFC3339),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
storage[conn.StoragePath("query")] = &subQueryREST{builder: b}
|
|
|
|
storage[conn.StoragePath("health")] = &subHealthREST{builder: b}
|
|
|
|
|
|
|
|
// TODO! only setup this endpoint if it is implemented
|
|
|
|
storage[conn.StoragePath("resource")] = &subResourceREST{builder: b}
|
|
|
|
|
|
|
|
// Frontend proxy
|
2024-01-19 22:56:52 +08:00
|
|
|
if len(b.pluginJSON.Routes) > 0 {
|
|
|
|
storage[conn.StoragePath("proxy")] = &subProxyREST{pluginJSON: b.pluginJSON}
|
2024-01-10 04:26:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(
|
|
|
|
conn.GroupResource().Group, scheme,
|
|
|
|
metav1.ParameterCodec, codecs)
|
|
|
|
|
|
|
|
apiGroupInfo.VersionedResourcesStorageMap[conn.GroupVersion().Version] = storage
|
|
|
|
return &apiGroupInfo, nil
|
|
|
|
}
|
|
|
|
|
2024-01-23 03:32:25 +08:00
|
|
|
func (b *DataSourceAPIBuilder) getPluginContext(ctx context.Context, uid string) (backend.PluginContext, error) {
|
2024-01-24 23:44:40 +08:00
|
|
|
instance, err := b.datasources.GetInstanceSettings(ctx, b.pluginJSON.ID, uid)
|
2024-01-23 03:32:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return backend.PluginContext{}, err
|
|
|
|
}
|
|
|
|
return b.contextProvider.PluginContextForDataSource(ctx, instance)
|
|
|
|
}
|
|
|
|
|
2024-01-13 04:05:30 +08:00
|
|
|
func (b *DataSourceAPIBuilder) GetOpenAPIDefinitions() openapi.GetOpenAPIDefinitions {
|
2024-02-02 14:06:28 +08:00
|
|
|
return func(ref openapi.ReferenceCallback) map[string]openapi.OpenAPIDefinition {
|
|
|
|
defs := query.GetOpenAPIDefinitions(ref) // required when running standalone
|
2024-03-02 06:26:04 +08:00
|
|
|
for k, v := range datasource.GetOpenAPIDefinitions(ref) {
|
2024-02-02 14:06:28 +08:00
|
|
|
defs[k] = v
|
|
|
|
}
|
|
|
|
return defs
|
|
|
|
}
|
2024-01-10 04:26:24 +08:00
|
|
|
}
|
|
|
|
|
2024-03-02 06:26:04 +08:00
|
|
|
func (b *DataSourceAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
|
|
|
|
// The plugin description
|
|
|
|
oas.Info.Description = b.pluginJSON.Info.Description
|
|
|
|
|
|
|
|
// The root api URL
|
|
|
|
root := "/apis/" + b.connectionResourceInfo.GroupVersion().String() + "/"
|
|
|
|
|
|
|
|
// Hide the ability to list all connections across tenants
|
|
|
|
delete(oas.Paths.Paths, root+b.connectionResourceInfo.GroupResource().Resource)
|
|
|
|
|
|
|
|
// The root API discovery list
|
|
|
|
sub := oas.Paths.Paths[root]
|
|
|
|
if sub != nil && sub.Get != nil {
|
|
|
|
sub.Get.Tags = []string{"API Discovery"} // sorts first in the list
|
|
|
|
}
|
|
|
|
return oas, nil
|
|
|
|
}
|
|
|
|
|
2024-01-10 04:26:24 +08:00
|
|
|
// Register additional routes with the server
|
2024-02-02 06:27:30 +08:00
|
|
|
func (b *DataSourceAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
|
2024-01-10 04:26:24 +08:00
|
|
|
return nil
|
|
|
|
}
|