K8s: Make GetAPIRoutes an optional interface (#97531)

pull/97609/head
Ryan McKinley 7 months ago committed by GitHub
parent f276f9b35d
commit 5f39d2eeb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      pkg/registry/apis/alerting/notifications/register.go
  2. 4
      pkg/registry/apis/dashboard/register.go
  3. 4
      pkg/registry/apis/dashboard/v1alpha1/register.go
  4. 4
      pkg/registry/apis/dashboard/v2alpha1/register.go
  5. 5
      pkg/registry/apis/datasource/register.go
  6. 4
      pkg/registry/apis/folders/register.go
  7. 5
      pkg/registry/apis/iam/register.go
  8. 5
      pkg/registry/apis/query/register.go
  9. 5
      pkg/registry/apis/scope/register.go
  10. 5
      pkg/registry/apis/service/register.go
  11. 7
      pkg/registry/apis/userstorage/register.go
  12. 8
      pkg/services/apiserver/builder/common.go
  13. 14
      pkg/services/apiserver/builder/openapi.go
  14. 7
      pkg/services/apiserver/builder/request_handler.go
  15. 10
      pkg/services/apiserver/builder/runner/builder.go

@ -108,10 +108,6 @@ func (t *NotificationsAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefin
return notificationsModels.GetOpenAPIDefinitions return notificationsModels.GetOpenAPIDefinitions
} }
func (t *NotificationsAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil
}
// PostProcessOpenAPI is a hook to alter OpenAPI3 specification of the API server. // PostProcessOpenAPI is a hook to alter OpenAPI3 specification of the API server.
func (t *NotificationsAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) { func (t *NotificationsAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
// The plugin description // The plugin description

@ -74,7 +74,3 @@ func (b *DashboardsAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefiniti
func (b *DashboardsAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) { func (b *DashboardsAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
return oas, nil return oas, nil
} }
func (b *DashboardsAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil // no custom API routes
}

@ -197,7 +197,3 @@ func (b *DashboardsAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op
} }
return oas, nil return oas, nil
} }
func (b *DashboardsAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil // no custom API routes
}

@ -197,7 +197,3 @@ func (b *DashboardsAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op
} }
return oas, nil return oas, nil
} }
func (b *DashboardsAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil // no custom API routes
}

@ -281,8 +281,3 @@ func (b *DataSourceAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.Op
} }
return oas, err return oas, err
} }
// Register additional routes with the server
func (b *DataSourceAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil
}

@ -146,10 +146,6 @@ func (b *FolderAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions
return v0alpha1.GetOpenAPIDefinitions return v0alpha1.GetOpenAPIDefinitions
} }
func (b *FolderAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil // no custom API routes
}
func (b *FolderAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) { func (b *FolderAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
// The plugin description // The plugin description
oas.Info.Description = "Grafana folders" oas.Info.Description = "Grafana folders"

@ -125,11 +125,6 @@ func (b *IdentityAccessManagementAPIBuilder) GetOpenAPIDefinitions() common.GetO
return iamv0.GetOpenAPIDefinitions return iamv0.GetOpenAPIDefinitions
} }
func (b *IdentityAccessManagementAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
// no custom API routes
return nil
}
func (b *IdentityAccessManagementAPIBuilder) GetAuthorizer() authorizer.Authorizer { func (b *IdentityAccessManagementAPIBuilder) GetAuthorizer() authorizer.Authorizer {
return b.authorizer return b.authorizer
} }

@ -165,11 +165,6 @@ func (b *QueryAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
return query.GetOpenAPIDefinitions return query.GetOpenAPIDefinitions
} }
// Register additional routes with the server
func (b *QueryAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil
}
func (b *QueryAPIBuilder) GetAuthorizer() authorizer.Authorizer { func (b *QueryAPIBuilder) GetAuthorizer() authorizer.Authorizer {
return nil // default is OK return nil // default is OK
} }

@ -157,11 +157,6 @@ func (b *ScopeAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
return scope.GetOpenAPIDefinitions return scope.GetOpenAPIDefinitions
} }
// Register additional routes with the server
func (b *ScopeAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil
}
func (b *ScopeAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) { func (b *ScopeAPIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, error) {
// The plugin description // The plugin description
oas.Info.Description = "Grafana scopes" oas.Info.Description = "Grafana scopes"

@ -88,8 +88,3 @@ func (b *ServiceAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.AP
func (b *ServiceAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions { func (b *ServiceAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
return service.GetOpenAPIDefinitions return service.GetOpenAPIDefinitions
} }
// Register additional routes with the server
func (b *ServiceAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil
}

@ -3,6 +3,7 @@ package userstorage
import ( import (
"context" "context"
"github.com/prometheus/client_golang/prometheus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
@ -13,8 +14,6 @@ import (
"github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/apimachinery/identity"
userstorage "github.com/grafana/grafana/pkg/apis/userstorage/v0alpha1" userstorage "github.com/grafana/grafana/pkg/apis/userstorage/v0alpha1"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/grafana/pkg/services/apiserver/builder" "github.com/grafana/grafana/pkg/services/apiserver/builder"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
) )
@ -78,10 +77,6 @@ func (b *UserStorageAPIBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinit
return userstorage.GetOpenAPIDefinitions return userstorage.GetOpenAPIDefinitions
} }
func (b *UserStorageAPIBuilder) GetAPIRoutes() *builder.APIRoutes {
return nil
}
func (b *UserStorageAPIBuilder) GetAuthorizer() authorizer.Authorizer { func (b *UserStorageAPIBuilder) GetAuthorizer() authorizer.Authorizer {
return authorizer.AuthorizerFunc( return authorizer.AuthorizerFunc(
func(ctx context.Context, attr authorizer.Attributes) (authorized authorizer.Decision, reason string, err error) { func(ctx context.Context, attr authorizer.Attributes) (authorized authorizer.Decision, reason string, err error) {

@ -39,9 +39,6 @@ type APIGroupBuilder interface {
// Get OpenAPI definitions // Get OpenAPI definitions
GetOpenAPIDefinitions() common.GetOpenAPIDefinitions GetOpenAPIDefinitions() common.GetOpenAPIDefinitions
// Get the API routes for each version
GetAPIRoutes() *APIRoutes
// Optionally add an authorization hook // Optionally add an authorization hook
// Standard namespace checking will happen before this is called, specifically // Standard namespace checking will happen before this is called, specifically
// the namespace must matches an org|stack that the user belongs to // 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) 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 { type APIGroupOptions struct {
Scheme *runtime.Scheme Scheme *runtime.Scheme
OptsGetter generic.RESTOptionsGetter OptsGetter generic.RESTOptionsGetter

@ -4,11 +4,11 @@ import (
"maps" "maps"
"strings" "strings"
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
common "k8s.io/kube-openapi/pkg/common" common "k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/spec3" "k8s.io/kube-openapi/pkg/spec3"
spec "k8s.io/kube-openapi/pkg/validation/spec" 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" "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
) )
@ -48,7 +48,6 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
return s, nil return s, nil
} }
for _, b := range builders { for _, b := range builders {
routes := b.GetAPIRoutes()
gv := b.GetGroupVersion() gv := b.GetGroupVersion()
prefix := "/apis/" + gv.String() + "/" prefix := "/apis/" + gv.String() + "/"
if s.Paths.Paths[prefix] != nil { if s.Paths.Paths[prefix] != nil {
@ -66,10 +65,11 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
Paths: s.Paths, Paths: s.Paths,
} }
if routes == nil { // Optionally include raw http handlers
routes = &APIRoutes{} provider, ok := b.(APIGroupRouteProvider)
} if ok && provider != nil {
routes := provider.GetAPIRoutes()
if routes != nil {
for _, route := range routes.Root { for _, route := range routes.Root {
copy.Paths.Paths[prefix+route.Path] = &spec3.Path{ copy.Paths.Paths[prefix+route.Path] = &spec3.Path{
PathProps: *route.Spec, PathProps: *route.Spec,
@ -81,6 +81,8 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
PathProps: *route.Spec, PathProps: *route.Spec,
} }
} }
}
}
// Make the sub-resources (connect) share the same tags as the main resource // Make the sub-resources (connect) share the same tags as the main resource
for path, spec := range copy.Paths.Paths { for path, spec := range copy.Paths.Paths {

@ -18,7 +18,12 @@ func GetCustomRoutesHandler(delegateHandler http.Handler, restConfig *restclient
router := mux.NewRouter() router := mux.NewRouter()
for _, builder := range builders { for _, builder := range builders {
routes := builder.GetAPIRoutes() provider, ok := builder.(APIGroupRouteProvider)
if !ok || provider == nil {
continue
}
routes := provider.GetAPIRoutes()
if routes == nil { if routes == nil {
continue continue
} }

@ -1,8 +1,6 @@
package runner package runner
import ( 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"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authorization/authorizer" "k8s.io/apiserver/pkg/authorization/authorizer"
@ -10,6 +8,8 @@ import (
genericapiserver "k8s.io/apiserver/pkg/server" genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/kube-openapi/pkg/common" "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" "github.com/grafana/grafana/pkg/apimachinery/utils"
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic" grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest" grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
@ -116,12 +116,6 @@ func (b *appBuilder) GetOpenAPIDefinitions() common.GetOpenAPIDefinitions {
return b.config.OpenAPIDefGetter 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 // GetAuthorizer implements APIGroupBuilder.GetAuthorizer
func (b *appBuilder) GetAuthorizer() authorizer.Authorizer { func (b *appBuilder) GetAuthorizer() authorizer.Authorizer {
return b.config.Authorizer return b.config.Authorizer

Loading…
Cancel
Save