Query Library: Move backend to enterprise (#100371)
* Move files to enterprise * Remove last parts of QL api * Fix CODEOWNERS
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/registry/apis/featuretoggle"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/folders"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/iam"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/peakq"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/query"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/scope"
|
||||
@@ -31,7 +30,6 @@ func ProvideRegistryServiceSink(
|
||||
_ *featuretoggle.FeatureFlagAPIBuilder,
|
||||
_ *datasource.DataSourceAPIBuilder,
|
||||
_ *folders.FolderAPIBuilder,
|
||||
_ *peakq.PeakQAPIBuilder,
|
||||
_ *iam.IdentityAccessManagementAPIBuilder,
|
||||
_ *scope.ScopeAPIBuilder,
|
||||
_ *query.QueryAPIBuilder,
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
package peakq
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/authorization/authorizer"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
genericapiserver "k8s.io/apiserver/pkg/server"
|
||||
"k8s.io/kube-openapi/pkg/common"
|
||||
"k8s.io/kube-openapi/pkg/spec3"
|
||||
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||
|
||||
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
|
||||
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
)
|
||||
|
||||
var _ builder.APIGroupBuilder = (*PeakQAPIBuilder)(nil)
|
||||
|
||||
// This is used just so wire has something unique to return
|
||||
type PeakQAPIBuilder struct{}
|
||||
|
||||
func NewPeakQAPIBuilder() *PeakQAPIBuilder {
|
||||
return &PeakQAPIBuilder{}
|
||||
}
|
||||
|
||||
func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, reg prometheus.Registerer) *PeakQAPIBuilder {
|
||||
if !featuremgmt.AnyEnabled(features,
|
||||
featuremgmt.FlagQueryService,
|
||||
featuremgmt.FlagQueryLibrary,
|
||||
featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
|
||||
return nil // skip registration unless explicitly added (or all experimental are added)
|
||||
}
|
||||
builder := NewPeakQAPIBuilder()
|
||||
apiregistration.RegisterAPI(builder)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (b *PeakQAPIBuilder) GetAuthorizer() authorizer.Authorizer {
|
||||
return nil // default authorizer is fine
|
||||
}
|
||||
|
||||
func (b *PeakQAPIBuilder) GetGroupVersion() schema.GroupVersion {
|
||||
return peakq.SchemeGroupVersion
|
||||
}
|
||||
|
||||
func (b *PeakQAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
||||
gv := peakq.SchemeGroupVersion
|
||||
err := peakq.AddToScheme(scheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 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: peakq.GROUP,
|
||||
// Version: runtime.APIVersionInternal,
|
||||
// })
|
||||
metav1.AddToGroupVersion(scheme, gv)
|
||||
return scheme.SetVersionPriority(gv)
|
||||
}
|
||||
|
||||
func (b *PeakQAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupInfo, opts builder.APIGroupOptions) error {
|
||||
resourceInfo := peakq.QueryTemplateResourceInfo
|
||||
storage := map[string]rest.Storage{}
|
||||
|
||||
peakqStorage, err := grafanaregistry.NewRegistryStore(opts.Scheme, resourceInfo, opts.OptsGetter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
storage[resourceInfo.StoragePath()] = peakqStorage
|
||||
storage[resourceInfo.StoragePath("render")] = &renderREST{
|
||||
getter: peakqStorage,
|
||||
}
|
||||
|
||||
apiGroupInfo.VersionedResourcesStorageMap[peakq.VERSION] = storage
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *PeakQAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
|
||||
return peakq.GetOpenAPIDefinitions
|
||||
}
|
||||
|
||||
// NOT A GREAT APPROACH... BUT will make a UI for statically defined
|
||||
func (b *PeakQAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
|
||||
defs := peakq.GetOpenAPIDefinitions(func(path string) spec.Ref { return spec.Ref{} })
|
||||
renderedQuerySchema := defs["github.com/grafana/grafana/pkg/apis/peakq/v0alpha1.RenderedQuery"].Schema
|
||||
queryTemplateSpecSchema := defs["github.com/grafana/grafana/pkg/apis/peakq/v0alpha1.QueryTemplateSpec"].Schema
|
||||
|
||||
params := []*spec3.Parameter{
|
||||
{
|
||||
ParameterProps: spec3.ParameterProps{
|
||||
// Arbitrary name. It won't appear in the request URL,
|
||||
// but will be used in code generated from this OAS spec
|
||||
Name: "variables",
|
||||
In: "query",
|
||||
Schema: spec.MapProperty(spec.ArrayProperty(spec.StringProperty())),
|
||||
Style: "form",
|
||||
Explode: true,
|
||||
Description: "Each variable is prefixed with var-{variable}={value}",
|
||||
Example: map[string][]string{
|
||||
"var-metricName": {"up"},
|
||||
"var-another": {"first", "second"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return &builder.APIRoutes{
|
||||
Root: []builder.APIRouteHandler{
|
||||
{
|
||||
Path: "render",
|
||||
Spec: &spec3.PathProps{
|
||||
Summary: "an example at the root level",
|
||||
Description: "longer description here?",
|
||||
Post: &spec3.Operation{
|
||||
OperationProps: spec3.OperationProps{
|
||||
Parameters: params,
|
||||
RequestBody: &spec3.RequestBody{
|
||||
RequestBodyProps: spec3.RequestBodyProps{
|
||||
Content: map[string]*spec3.MediaType{
|
||||
"application/json": {
|
||||
MediaTypeProps: spec3.MediaTypeProps{
|
||||
Schema: &queryTemplateSpecSchema,
|
||||
// Example: basicTemplateSpec,
|
||||
Examples: map[string]*spec3.Example{
|
||||
"test": {
|
||||
ExampleProps: spec3.ExampleProps{
|
||||
Summary: "hello",
|
||||
Value: basicTemplateSpec,
|
||||
},
|
||||
},
|
||||
"test2": {
|
||||
ExampleProps: spec3.ExampleProps{
|
||||
Summary: "hello2",
|
||||
Value: basicTemplateSpec,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Responses: &spec3.Responses{
|
||||
ResponsesProps: spec3.ResponsesProps{
|
||||
StatusCodeResponses: map[int]*spec3.Response{
|
||||
200: {
|
||||
ResponseProps: spec3.ResponseProps{
|
||||
Description: "OK",
|
||||
Content: map[string]*spec3.MediaType{
|
||||
"application/json": {
|
||||
MediaTypeProps: spec3.MediaTypeProps{
|
||||
Schema: &renderedQuerySchema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Handler: renderPOSTHandler,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package peakq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
peakq "github.com/grafana/grafana/pkg/apis/peakq/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
|
||||
)
|
||||
|
||||
type renderREST struct {
|
||||
getter rest.Getter
|
||||
}
|
||||
|
||||
var _ = rest.Connecter(&renderREST{})
|
||||
|
||||
func (r *renderREST) New() runtime.Object {
|
||||
return &peakq.RenderedQuery{}
|
||||
}
|
||||
|
||||
func (r *renderREST) Destroy() {
|
||||
}
|
||||
|
||||
func (r *renderREST) ConnectMethods() []string {
|
||||
return []string{"GET"}
|
||||
}
|
||||
|
||||
func (r *renderREST) NewConnectOptions() (runtime.Object, bool, string) {
|
||||
return nil, false, "" // true means you can use the trailing path as a variable
|
||||
}
|
||||
|
||||
func (r *renderREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
||||
obj, err := r.getter.Get(ctx, name, &v1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, ok := obj.(*peakq.QueryTemplate)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected template")
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
input, err := makeVarMapFromParams(req.URL.Query())
|
||||
if err != nil {
|
||||
responder.Error(err)
|
||||
return
|
||||
}
|
||||
out, err := template.RenderTemplate(t.Spec, input)
|
||||
if err != nil {
|
||||
responder.Error(fmt.Errorf("failed to render: %w", err))
|
||||
return
|
||||
}
|
||||
responder.Object(http.StatusOK, &peakq.RenderedQuery{
|
||||
Targets: out,
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
|
||||
func renderPOSTHandler(w http.ResponseWriter, req *http.Request) {
|
||||
input, err := makeVarMapFromParams(req.URL.Query())
|
||||
if err != nil {
|
||||
_, _ = w.Write([]byte("ERROR: " + err.Error()))
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
|
||||
var qT peakq.QueryTemplate
|
||||
err = json.NewDecoder(req.Body).Decode(&qT.Spec)
|
||||
if err != nil {
|
||||
_, _ = w.Write([]byte("ERROR: " + err.Error()))
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
results, err := template.RenderTemplate(qT.Spec, input)
|
||||
if err != nil {
|
||||
_, _ = w.Write([]byte("ERROR: " + err.Error()))
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(peakq.RenderedQuery{
|
||||
Targets: results,
|
||||
})
|
||||
}
|
||||
|
||||
// Replicate the grafana dashboard URL syntax
|
||||
// &var-abc=1&var=abc=2&var-xyz=3...
|
||||
func makeVarMapFromParams(v url.Values) (map[string][]string, error) {
|
||||
input := make(map[string][]string, len(v))
|
||||
for key, vals := range v {
|
||||
if !strings.HasPrefix(key, "var-") {
|
||||
continue
|
||||
}
|
||||
input[key[4:]] = vals
|
||||
}
|
||||
return input, nil
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package peakq
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
apidata "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
|
||||
)
|
||||
|
||||
var basicTemplateSpec = template.QueryTemplate{
|
||||
Title: "Test",
|
||||
Variables: []template.TemplateVariable{
|
||||
{
|
||||
Key: "metricName",
|
||||
DefaultValues: []string{`down`},
|
||||
},
|
||||
},
|
||||
Targets: []template.Target{
|
||||
{
|
||||
DataType: data.FrameTypeUnknown,
|
||||
//DataTypeVersion: data.FrameTypeVersion{0, 0},
|
||||
Variables: map[string][]template.VariableReplacement{
|
||||
"metricName": {
|
||||
{
|
||||
Path: "$.expr",
|
||||
Position: &template.Position{
|
||||
Start: 0,
|
||||
End: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
Path: "$.expr",
|
||||
Position: &template.Position{
|
||||
Start: 13,
|
||||
End: 23,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Properties: apidata.NewDataQuery(map[string]any{
|
||||
"refId": "A", // TODO: Set when Where?
|
||||
"datasource": map[string]any{
|
||||
"type": "prometheus",
|
||||
"uid": "foo", // TODO: Probably a default templating thing to set this.
|
||||
},
|
||||
"editorMode": "builder",
|
||||
"expr": "metricName + metricName + 42",
|
||||
"instant": true,
|
||||
"range": false,
|
||||
"exemplar": false,
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var basicTemplateRenderedTargets = []template.Target{
|
||||
{
|
||||
DataType: data.FrameTypeUnknown,
|
||||
//DataTypeVersion: data.FrameTypeVersion{0, 0},
|
||||
Properties: apidata.NewDataQuery(map[string]any{
|
||||
"refId": "A", // TODO: Set when Where?
|
||||
"datasource": map[string]any{
|
||||
"type": "prometheus",
|
||||
"uid": "foo", // TODO: Probably a default templating thing to set this.
|
||||
},
|
||||
"editorMode": "builder",
|
||||
"expr": "up + up + 42",
|
||||
"instant": true,
|
||||
"range": false,
|
||||
"exemplar": false,
|
||||
}),
|
||||
},
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package peakq
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apis/query/v0alpha1/template"
|
||||
)
|
||||
|
||||
func TestRender(t *testing.T) {
|
||||
rT, err := template.RenderTemplate(basicTemplateSpec, map[string][]string{"metricName": {"up"}})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t,
|
||||
basicTemplateRenderedTargets[0].Properties.GetString("expr"),
|
||||
rT[0].Properties.GetString("expr"))
|
||||
b, _ := json.MarshalIndent(basicTemplateSpec, "", " ")
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/registry/apis/featuretoggle"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/folders"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/iam"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/peakq"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/query"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/scope"
|
||||
@@ -40,7 +39,6 @@ var WireSet = wire.NewSet(
|
||||
datasource.RegisterAPIService,
|
||||
folders.RegisterAPIService,
|
||||
iam.RegisterAPIService,
|
||||
peakq.RegisterAPIService,
|
||||
provisioning.RegisterAPIService,
|
||||
service.RegisterAPIService,
|
||||
query.RegisterAPIService,
|
||||
|
||||
Reference in New Issue
Block a user