mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Pass prometheus registerer to the dual writer * Fix tests * Remove unused var * Fix tests * Uncomment test * Remove leading line * Fix tests. Reuse registerer if there's already one * Lint * Improve double registering logic * Rebase main
227 lines
7.0 KiB
Go
227 lines
7.0 KiB
Go
package example
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
"k8s.io/apiserver/pkg/authorization/authorizer"
|
|
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
|
|
"k8s.io/apiserver/pkg/endpoints/request"
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
|
genericapiserver "k8s.io/apiserver/pkg/server"
|
|
common "k8s.io/kube-openapi/pkg/common"
|
|
"k8s.io/kube-openapi/pkg/spec3"
|
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
|
|
|
example "github.com/grafana/grafana/pkg/apis/example/v0alpha1"
|
|
"github.com/grafana/grafana/pkg/apiserver/builder"
|
|
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
|
|
"github.com/grafana/grafana/pkg/infra/appcontext"
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
var _ builder.APIGroupBuilder = (*TestingAPIBuilder)(nil)
|
|
|
|
// This is used just so wire has something unique to return
|
|
type TestingAPIBuilder struct {
|
|
codecs serializer.CodecFactory
|
|
gv schema.GroupVersion
|
|
}
|
|
|
|
func NewTestingAPIBuilder() *TestingAPIBuilder {
|
|
return &TestingAPIBuilder{
|
|
gv: schema.GroupVersion{Group: example.GROUP, Version: example.VERSION},
|
|
}
|
|
}
|
|
|
|
func RegisterAPIService(features featuremgmt.FeatureToggles, apiregistration builder.APIRegistrar, reg prometheus.Registerer) *TestingAPIBuilder {
|
|
if !features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
|
|
return nil // skip registration unless opting into experimental apis
|
|
}
|
|
builder := NewTestingAPIBuilder()
|
|
apiregistration.RegisterAPI(builder)
|
|
return builder
|
|
}
|
|
|
|
func (b *TestingAPIBuilder) GetGroupVersion() schema.GroupVersion {
|
|
return b.gv
|
|
}
|
|
|
|
func (b *TestingAPIBuilder) GetDesiredDualWriterMode(dualWrite bool, modeMap map[string]grafanarest.DualWriterMode) grafanarest.DualWriterMode {
|
|
// Add required configuration support in order to enable other modes. For an example, see pkg/registry/apis/playlist/register.go
|
|
return grafanarest.Mode0
|
|
}
|
|
|
|
func addKnownTypes(scheme *runtime.Scheme, gv schema.GroupVersion) {
|
|
scheme.AddKnownTypes(gv,
|
|
&example.RuntimeInfo{},
|
|
&example.DummyResource{},
|
|
&example.DummyResourceList{},
|
|
&example.DummySubresource{},
|
|
)
|
|
}
|
|
|
|
func (b *TestingAPIBuilder) InstallSchema(scheme *runtime.Scheme) error {
|
|
addKnownTypes(scheme, b.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: b.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, b.gv)
|
|
return scheme.SetVersionPriority(b.gv)
|
|
}
|
|
|
|
func (b *TestingAPIBuilder) GetAPIGroupInfo(
|
|
scheme *runtime.Scheme,
|
|
codecs serializer.CodecFactory, // pointer?
|
|
_ generic.RESTOptionsGetter,
|
|
_ grafanarest.DualWriterMode,
|
|
_ prometheus.Registerer,
|
|
) (*genericapiserver.APIGroupInfo, error) {
|
|
b.codecs = codecs
|
|
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(b.gv.Group, scheme, metav1.ParameterCodec, codecs)
|
|
|
|
storage := map[string]rest.Storage{}
|
|
storage[example.RuntimeResourceInfo.StoragePath()] = newDeploymentInfoStorage(b.gv, scheme)
|
|
storage[example.DummyResourceInfo.StoragePath()] = newDummyStorage(b.gv, scheme, "test1", "test2", "test3")
|
|
storage[example.DummyResourceInfo.StoragePath("sub")] = &dummySubresourceREST{}
|
|
apiGroupInfo.VersionedResourcesStorageMap[b.gv.Version] = storage
|
|
return &apiGroupInfo, nil
|
|
}
|
|
|
|
func (b *TestingAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
|
|
return example.GetOpenAPIDefinitions
|
|
}
|
|
|
|
// Register additional routes with the server
|
|
func (b *TestingAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
|
|
return &builder.APIRoutes{
|
|
Root: []builder.APIRouteHandler{
|
|
{
|
|
Path: "aaa",
|
|
Spec: &spec3.PathProps{
|
|
Summary: "an example at the root level",
|
|
Description: "longer description here?",
|
|
Get: &spec3.Operation{
|
|
OperationProps: spec3.OperationProps{
|
|
Parameters: []*spec3.Parameter{
|
|
{ParameterProps: spec3.ParameterProps{
|
|
Name: "a",
|
|
}},
|
|
},
|
|
Responses: &spec3.Responses{
|
|
ResponsesProps: spec3.ResponsesProps{
|
|
StatusCodeResponses: map[int]*spec3.Response{
|
|
200: {
|
|
ResponseProps: spec3.ResponseProps{
|
|
Description: "OK",
|
|
Content: map[string]*spec3.MediaType{
|
|
"text/plain": {
|
|
MediaTypeProps: spec3.MediaTypeProps{
|
|
Schema: &spec.Schema{
|
|
SchemaProps: spec.SchemaProps{
|
|
Type: []string{"string"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte("Root level handler (aaa)"))
|
|
},
|
|
},
|
|
{
|
|
Path: "bbb",
|
|
Spec: &spec3.PathProps{
|
|
Summary: "an example at the root level",
|
|
Description: "longer description here?",
|
|
Get: &spec3.Operation{
|
|
OperationProps: spec3.OperationProps{
|
|
Parameters: []*spec3.Parameter{
|
|
{ParameterProps: spec3.ParameterProps{
|
|
Name: "b",
|
|
}},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte("Root level handler (bbb)"))
|
|
},
|
|
},
|
|
},
|
|
Namespace: []builder.APIRouteHandler{
|
|
{
|
|
Path: "ccc",
|
|
Spec: &spec3.PathProps{
|
|
Summary: "an example at the root level",
|
|
Description: "longer description here?",
|
|
Get: &spec3.Operation{
|
|
OperationProps: spec3.OperationProps{
|
|
Parameters: []*spec3.Parameter{
|
|
{ParameterProps: spec3.ParameterProps{
|
|
Name: "a",
|
|
}},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Handler: func(w http.ResponseWriter, r *http.Request) {
|
|
info, ok := request.RequestInfoFrom(r.Context())
|
|
if !ok {
|
|
responsewriters.ErrorNegotiated(
|
|
apierrors.NewInternalError(fmt.Errorf("no RequestInfo found in the context")),
|
|
b.codecs, schema.GroupVersion{}, w, r,
|
|
)
|
|
return
|
|
}
|
|
|
|
_, _ = w.Write([]byte("Custom namespace route ccc: " + info.Namespace))
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (b *TestingAPIBuilder) GetAuthorizer() authorizer.Authorizer {
|
|
return authorizer.AuthorizerFunc(
|
|
func(ctx context.Context, attr authorizer.Attributes) (authorized authorizer.Decision, reason string, err error) {
|
|
if !attr.IsResourceRequest() {
|
|
return authorizer.DecisionNoOpinion, "", nil
|
|
}
|
|
|
|
// require a user
|
|
_, err = appcontext.User(ctx)
|
|
if err != nil {
|
|
return authorizer.DecisionDeny, "valid user is required", err
|
|
}
|
|
|
|
return authorizer.DecisionNoOpinion, "", err // fallback to org/role logic
|
|
})
|
|
}
|