K8s: Make GetAPIRoutes an optional interface (#97531)

This commit is contained in:
Ryan McKinley
2024-12-07 03:08:18 +03:00
committed by GitHub
parent f276f9b35d
commit 5f39d2eeb0
15 changed files with 30 additions and 77 deletions
+5 -3
View File
@@ -39,9 +39,6 @@ type APIGroupBuilder interface {
// Get OpenAPI definitions
GetOpenAPIDefinitions() common.GetOpenAPIDefinitions
// Get the API routes for each version
GetAPIRoutes() *APIRoutes
// Optionally add an authorization hook
// Standard namespace checking will happen before this is called, specifically
// the namespace must matches an org|stack that the user belongs to
@@ -60,6 +57,11 @@ type APIGroupValidation interface {
Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error)
}
type APIGroupRouteProvider interface {
// Support direct HTTP routes from an APIGroup
GetAPIRoutes() *APIRoutes
}
type APIGroupOptions struct {
Scheme *runtime.Scheme
OptsGetter generic.RESTOptionsGetter
+16 -14
View File
@@ -4,11 +4,11 @@ import (
"maps"
"strings"
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
common "k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/spec3"
spec "k8s.io/kube-openapi/pkg/validation/spec"
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
"github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
)
@@ -48,7 +48,6 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
return s, nil
}
for _, b := range builders {
routes := b.GetAPIRoutes()
gv := b.GetGroupVersion()
prefix := "/apis/" + gv.String() + "/"
if s.Paths.Paths[prefix] != nil {
@@ -66,19 +65,22 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
Paths: s.Paths,
}
if routes == nil {
routes = &APIRoutes{}
}
// Optionally include raw http handlers
provider, ok := b.(APIGroupRouteProvider)
if ok && provider != nil {
routes := provider.GetAPIRoutes()
if routes != nil {
for _, route := range routes.Root {
copy.Paths.Paths[prefix+route.Path] = &spec3.Path{
PathProps: *route.Spec,
}
}
for _, route := range routes.Root {
copy.Paths.Paths[prefix+route.Path] = &spec3.Path{
PathProps: *route.Spec,
}
}
for _, route := range routes.Namespace {
copy.Paths.Paths[prefix+"namespaces/{namespace}/"+route.Path] = &spec3.Path{
PathProps: *route.Spec,
for _, route := range routes.Namespace {
copy.Paths.Paths[prefix+"namespaces/{namespace}/"+route.Path] = &spec3.Path{
PathProps: *route.Spec,
}
}
}
}
@@ -18,7 +18,12 @@ func GetCustomRoutesHandler(delegateHandler http.Handler, restConfig *restclient
router := mux.NewRouter()
for _, builder := range builders {
routes := builder.GetAPIRoutes()
provider, ok := builder.(APIGroupRouteProvider)
if !ok || provider == nil {
continue
}
routes := provider.GetAPIRoutes()
if routes == nil {
continue
}
@@ -1,8 +1,6 @@
package runner
import (
"github.com/grafana/grafana-app-sdk/app"
"github.com/grafana/grafana-app-sdk/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authorization/authorizer"
@@ -10,6 +8,8 @@ import (
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/kube-openapi/pkg/common"
"github.com/grafana/grafana-app-sdk/app"
"github.com/grafana/grafana-app-sdk/resource"
"github.com/grafana/grafana/pkg/apimachinery/utils"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
@@ -116,12 +116,6 @@ func (b *appBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
return b.config.OpenAPIDefGetter
}
// GetAPIRoutes implements APIGroupBuilder.GetAPIRoutes
func (b *appBuilder) GetAPIRoutes() *builder.APIRoutes {
// TODO: The API routes are not yet exposed by the app.App interface.
return nil
}
// GetAuthorizer implements APIGroupBuilder.GetAuthorizer
func (b *appBuilder) GetAuthorizer() authorizer.Authorizer {
return b.config.Authorizer