2024-02-01 17:27:30 -05:00
|
|
|
package builder
|
2023-09-22 18:29:43 -07:00
|
|
|
|
|
|
|
|
import (
|
2024-10-28 16:40:25 +01:00
|
|
|
"context"
|
2023-10-06 11:55:22 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2024-10-28 16:40:25 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2023-09-22 18:29:43 -07:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2023-10-06 11:55:22 -07:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2024-10-28 16:40:25 +01:00
|
|
|
"k8s.io/apiserver/pkg/admission"
|
2023-12-19 09:12:35 -08:00
|
|
|
"k8s.io/apiserver/pkg/authorization/authorizer"
|
2023-09-26 17:15:15 -04:00
|
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
2023-09-22 18:29:43 -07:00
|
|
|
genericapiserver "k8s.io/apiserver/pkg/server"
|
|
|
|
|
"k8s.io/kube-openapi/pkg/common"
|
|
|
|
|
"k8s.io/kube-openapi/pkg/spec3"
|
2024-07-11 13:23:31 -07:00
|
|
|
|
|
|
|
|
grafanarest "github.com/grafana/grafana/pkg/apiserver/rest"
|
2024-11-09 08:09:46 +03:00
|
|
|
"github.com/grafana/grafana/pkg/storage/unified/apistore"
|
2023-09-22 18:29:43 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TODO: this (or something like it) belongs in grafana-app-sdk,
|
|
|
|
|
// but lets keep it here while we iterate on a few simple examples
|
|
|
|
|
type APIGroupBuilder interface {
|
2023-10-06 11:55:22 -07:00
|
|
|
// Get the main group name
|
|
|
|
|
GetGroupVersion() schema.GroupVersion
|
|
|
|
|
|
2023-09-22 18:29:43 -07:00
|
|
|
// Add the kinds to the server scheme
|
|
|
|
|
InstallSchema(scheme *runtime.Scheme) error
|
|
|
|
|
|
2024-09-23 19:07:52 -07:00
|
|
|
// UpdateAPIGroupInfo used to be a getter until we ran into the issue
|
|
|
|
|
// where separate API Group Info for the same group (different versions) aren't handled well by
|
|
|
|
|
// the InstallAPIGroup facility of genericapiserver. Also, we can only ever call InstallAPIGroup
|
|
|
|
|
// once on the genericapiserver per group, or we run into double registration startup errors.
|
|
|
|
|
//
|
|
|
|
|
// The caller should share the apiGroupInfo passed into this function across builder versions of the same group.
|
|
|
|
|
// UpdateAPIGroupInfo builds the group+version behavior updating the passed in apiGroupInfo in place
|
2024-10-15 07:46:08 +03:00
|
|
|
UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupInfo, opts APIGroupOptions) error
|
2023-09-22 18:29:43 -07:00
|
|
|
|
|
|
|
|
// Get OpenAPI definitions
|
|
|
|
|
GetOpenAPIDefinitions() common.GetOpenAPIDefinitions
|
|
|
|
|
|
2023-10-06 11:55:22 -07:00
|
|
|
// Get the API routes for each version
|
|
|
|
|
GetAPIRoutes() *APIRoutes
|
2023-12-19 09:12:35 -08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
GetAuthorizer() authorizer.Authorizer
|
2023-10-06 11:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-29 02:14:07 +01:00
|
|
|
type APIGroupMutation interface {
|
|
|
|
|
// Mutate allows the builder to make changes to the object before it is persisted.
|
|
|
|
|
// Context is used only for timeout/deadline/cancellation and tracing information.
|
|
|
|
|
Mutate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 16:40:25 +01:00
|
|
|
type APIGroupValidation interface {
|
2024-10-29 02:14:07 +01:00
|
|
|
// Validate makes an admission decision based on the request attributes. It is NOT allowed to mutate
|
|
|
|
|
// Context is used only for timeout/deadline/cancellation and tracing information.
|
2024-10-28 16:40:25 +01:00
|
|
|
Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) (err error)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-15 07:46:08 +03:00
|
|
|
type APIGroupOptions struct {
|
|
|
|
|
Scheme *runtime.Scheme
|
|
|
|
|
OptsGetter generic.RESTOptionsGetter
|
|
|
|
|
DualWriteBuilder grafanarest.DualWriteBuilder
|
|
|
|
|
MetricsRegister prometheus.Registerer
|
2024-11-09 08:09:46 +03:00
|
|
|
StorageOptions apistore.StorageOptionsRegister
|
2024-10-15 07:46:08 +03:00
|
|
|
}
|
|
|
|
|
|
2024-02-02 14:19:45 -08:00
|
|
|
// Builders that implement OpenAPIPostProcessor are given a chance to modify the schema directly
|
|
|
|
|
type OpenAPIPostProcessor interface {
|
|
|
|
|
PostProcessOpenAPI(*spec3.OpenAPI) (*spec3.OpenAPI, error)
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 11:55:22 -07:00
|
|
|
// This is used to implement dynamic sub-resources like pods/x/logs
|
|
|
|
|
type APIRouteHandler struct {
|
|
|
|
|
Path string // added to the appropriate level
|
|
|
|
|
Spec *spec3.PathProps // Exposed in the open api service discovery
|
|
|
|
|
Handler http.HandlerFunc // when Level = resource, the resource will be available in context
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 19:51:58 -08:00
|
|
|
// APIRoutes define explicit HTTP handlers in an apiserver
|
|
|
|
|
// TBD: is this actually necessary -- there may be more k8s native options for this
|
2023-10-06 11:55:22 -07:00
|
|
|
type APIRoutes struct {
|
|
|
|
|
// Root handlers are registered directly after the apiVersion identifier
|
|
|
|
|
Root []APIRouteHandler
|
|
|
|
|
|
|
|
|
|
// Namespace handlers are mounted under the namespace
|
|
|
|
|
Namespace []APIRouteHandler
|
2023-09-22 18:29:43 -07:00
|
|
|
}
|
2024-02-01 17:27:30 -05:00
|
|
|
|
|
|
|
|
type APIRegistrar interface {
|
|
|
|
|
RegisterAPI(builder APIGroupBuilder)
|
|
|
|
|
}
|