K8s: Support multiple versions in builder (#100331)

This commit is contained in:
Todd Treece
2025-02-14 12:29:43 -05:00
committed by GitHub
parent 39db59fc73
commit 30ae434a2e
7 changed files with 165 additions and 119 deletions

View File

@@ -37,11 +37,13 @@ func NewAdmissionFromBuilders(builders []APIGroupBuilder) *builderAdmission {
mutators := make(map[schema.GroupVersion]APIGroupMutation) mutators := make(map[schema.GroupVersion]APIGroupMutation)
validators := make(map[schema.GroupVersion]APIGroupValidation) validators := make(map[schema.GroupVersion]APIGroupValidation)
for _, builder := range builders { for _, builder := range builders {
if m, ok := builder.(APIGroupMutation); ok { for _, gv := range GetGroupVersions(builder) {
mutators[builder.GetGroupVersion()] = m if m, ok := builder.(APIGroupMutation); ok {
} mutators[gv] = m
if v, ok := builder.(APIGroupValidation); ok { }
validators[builder.GetGroupVersion()] = v if v, ok := builder.(APIGroupValidation); ok {
validators[gv] = v
}
} }
} }
return NewAdmission(mutators, validators) return NewAdmission(mutators, validators)

View File

@@ -207,8 +207,8 @@ type mockBuilder struct {
validator builder.APIGroupValidation validator builder.APIGroupValidation
} }
func (m *mockBuilder) GetGroupVersion() schema.GroupVersion { func (m *mockBuilder) GetGroupVersions() []schema.GroupVersion {
return m.groupVersion return []schema.GroupVersion{m.groupVersion}
} }
func (m *mockBuilder) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error { func (m *mockBuilder) Validate(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {

View File

@@ -2,6 +2,7 @@ package builder
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@@ -21,9 +22,6 @@ import (
// TODO: this (or something like it) belongs in grafana-app-sdk, // TODO: this (or something like it) belongs in grafana-app-sdk,
// but lets keep it here while we iterate on a few simple examples // but lets keep it here while we iterate on a few simple examples
type APIGroupBuilder interface { type APIGroupBuilder interface {
// Get the main group name
GetGroupVersion() schema.GroupVersion
// Add the kinds to the server scheme // Add the kinds to the server scheme
InstallSchema(scheme *runtime.Scheme) error InstallSchema(scheme *runtime.Scheme) error
@@ -38,10 +36,17 @@ type APIGroupBuilder interface {
// Get OpenAPI definitions // Get OpenAPI definitions
GetOpenAPIDefinitions() common.GetOpenAPIDefinitions GetOpenAPIDefinitions() common.GetOpenAPIDefinitions
}
// Optionally add an authorization hook type APIGroupVersionProvider interface {
// Standard namespace checking will happen before this is called, specifically GetGroupVersion() schema.GroupVersion
// the namespace must matches an org|stack that the user belongs to }
type APIGroupVersionsProvider interface {
GetGroupVersions() []schema.GroupVersion
}
type APIGroupAuthorizer interface {
GetAuthorizer() authorizer.Authorizer GetAuthorizer() authorizer.Authorizer
} }
@@ -100,3 +105,32 @@ type APIRoutes struct {
type APIRegistrar interface { type APIRegistrar interface {
RegisterAPI(builder APIGroupBuilder) RegisterAPI(builder APIGroupBuilder)
} }
func getGroup(builder APIGroupBuilder) (string, error) {
if v, ok := builder.(APIGroupVersionProvider); ok {
return v.GetGroupVersion().Group, nil
}
if v, ok := builder.(APIGroupVersionsProvider); ok {
if len(v.GetGroupVersions()) == 0 {
return "", fmt.Errorf("unable to get group: builder returned no versions")
}
return v.GetGroupVersions()[0].Group, nil
}
return "", fmt.Errorf("unable to get group: builder does not implement APIGroupVersionProvider or APIGroupVersionsProvider")
}
func GetGroupVersions(builder APIGroupBuilder) []schema.GroupVersion {
if v, ok := builder.(APIGroupVersionProvider); ok {
return []schema.GroupVersion{v.GetGroupVersion()}
}
if v, ok := builder.(APIGroupVersionsProvider); ok {
return v.GetGroupVersions()
}
// this should never happen
panic("builder does not implement APIGroupVersionProvider or APIGroupVersionsProvider")
}

View File

@@ -360,7 +360,10 @@ func InstallAPIs(
// in other places, working with a flat []APIGroupBuilder list is much nicer // in other places, working with a flat []APIGroupBuilder list is much nicer
buildersGroupMap := make(map[string][]APIGroupBuilder, 0) buildersGroupMap := make(map[string][]APIGroupBuilder, 0)
for _, b := range builders { for _, b := range builders {
group := b.GetGroupVersion().Group group, err := getGroup(b)
if err != nil {
return err
}
if _, ok := buildersGroupMap[group]; !ok { if _, ok := buildersGroupMap[group]; !ok {
buildersGroupMap[group] = make([]APIGroupBuilder, 0) buildersGroupMap[group] = make([]APIGroupBuilder, 0)
} }

View File

@@ -49,87 +49,88 @@ func getOpenAPIPostProcessor(version string, builders []APIGroupBuilder) func(*s
} }
for _, b := range builders { for _, b := range builders {
gv := b.GetGroupVersion() for _, gv := range GetGroupVersions(b) {
prefix := "/apis/" + gv.String() + "/" prefix := "/apis/" + gv.String() + "/"
if s.Paths.Paths[prefix] != nil { if s.Paths.Paths[prefix] != nil {
copy := spec3.OpenAPI{ copy := spec3.OpenAPI{
Version: s.Version, Version: s.Version,
Info: &spec.Info{ Info: &spec.Info{
InfoProps: spec.InfoProps{ InfoProps: spec.InfoProps{
Title: gv.String(), Title: gv.String(),
Version: version, Version: version,
},
}, },
}, Components: s.Components,
Components: s.Components, ExternalDocs: s.ExternalDocs,
ExternalDocs: s.ExternalDocs, Servers: s.Servers,
Servers: s.Servers, Paths: s.Paths,
Paths: s.Paths,
}
for k := range copy.Paths.Paths {
// Remove the deprecated watch URL -- can use list with ?watch=true
if strings.HasPrefix(k, prefix+"watch/") {
delete(copy.Paths.Paths, k)
continue
} }
}
sub := copy.Paths.Paths[prefix] for k := range copy.Paths.Paths {
if sub != nil && sub.Get != nil { // Remove the deprecated watch URL -- can use list with ?watch=true
sub.Get.Tags = []string{"API Discovery"} if strings.HasPrefix(k, prefix+"watch/") {
sub.Get.Description = "Describe the available kubernetes resources" delete(copy.Paths.Paths, k)
} continue
// Remove the growing list of kinds
for k, v := range copy.Components.Schemas {
if strings.HasPrefix(k, "io.k8s.apimachinery.pkg.apis.meta.v1") && v.Extensions != nil {
delete(v.Extensions, "x-kubernetes-group-version-kind") // a growing list of everything
}
}
// 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.Namespace { sub := copy.Paths.Paths[prefix]
copy.Paths.Paths[prefix+"namespaces/{namespace}/"+route.Path] = &spec3.Path{ if sub != nil && sub.Get != nil {
PathProps: *route.Spec, sub.Get.Tags = []string{"API Discovery"}
sub.Get.Description = "Describe the available kubernetes resources"
}
// Remove the growing list of kinds
for k, v := range copy.Components.Schemas {
if strings.HasPrefix(k, "io.k8s.apimachinery.pkg.apis.meta.v1") && v.Extensions != nil {
delete(v.Extensions, "x-kubernetes-group-version-kind") // a growing list of everything
}
}
// 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.Namespace {
copy.Paths.Paths[prefix+"namespaces/{namespace}/"+route.Path] = &spec3.Path{
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 {
idx := strings.LastIndex(path, "{name}/") idx := strings.LastIndex(path, "{name}/")
if idx > 0 { if idx > 0 {
parent := copy.Paths.Paths[path[:idx+6]] parent := copy.Paths.Paths[path[:idx+6]]
if parent != nil && parent.Get != nil { if parent != nil && parent.Get != nil {
for _, op := range GetPathOperations(spec) { for _, op := range GetPathOperations(spec) {
if op != nil && op.Extensions != nil { if op != nil && op.Extensions != nil {
action, ok := op.Extensions.GetString("x-kubernetes-action") action, ok := op.Extensions.GetString("x-kubernetes-action")
if ok && action == "connect" { if ok && action == "connect" {
op.Tags = parent.Get.Tags op.Tags = parent.Get.Tags
}
} }
} }
} }
} }
} }
}
// Support direct manipulation of API results // Support direct manipulation of API results
processor, ok := b.(OpenAPIPostProcessor) processor, ok := b.(OpenAPIPostProcessor)
if ok { if ok {
return processor.PostProcessOpenAPI(&copy) return processor.PostProcessOpenAPI(&copy)
}
return &copy, nil
} }
return &copy, nil
} }
} }

View File

@@ -28,42 +28,43 @@ func GetCustomRoutesHandler(delegateHandler http.Handler, restConfig *restclient
continue continue
} }
gv := builder.GetGroupVersion() for _, gv := range GetGroupVersions(builder) {
prefix := "/apis/" + gv.String() prefix := "/apis/" + gv.String()
// Root handlers // Root handlers
var sub *mux.Router var sub *mux.Router
for _, route := range routes.Root { for _, route := range routes.Root {
if sub == nil { if sub == nil {
sub = router.PathPrefix(prefix).Subrouter() sub = router.PathPrefix(prefix).Subrouter()
sub.MethodNotAllowedHandler = &methodNotAllowedHandler{} sub.MethodNotAllowedHandler = &methodNotAllowedHandler{}
}
useful = true
methods, err := methodsFromSpec(route.Path, route.Spec)
if err != nil {
return nil, err
}
sub.HandleFunc("/"+route.Path, route.Handler).
Methods(methods...)
} }
useful = true // Namespace handlers
methods, err := methodsFromSpec(route.Path, route.Spec) sub = nil
if err != nil { prefix += "/namespaces/{namespace}"
return nil, err for _, route := range routes.Namespace {
} if sub == nil {
sub.HandleFunc("/"+route.Path, route.Handler). sub = router.PathPrefix(prefix).Subrouter()
Methods(methods...) sub.MethodNotAllowedHandler = &methodNotAllowedHandler{}
} }
// Namespace handlers useful = true
sub = nil methods, err := methodsFromSpec(route.Path, route.Spec)
prefix += "/namespaces/{namespace}" if err != nil {
for _, route := range routes.Namespace { return nil, err
if sub == nil { }
sub = router.PathPrefix(prefix).Subrouter() sub.HandleFunc("/"+route.Path, route.Handler).
sub.MethodNotAllowedHandler = &methodNotAllowedHandler{} Methods(methods...)
} }
useful = true
methods, err := methodsFromSpec(route.Path, route.Spec)
if err != nil {
return nil, err
}
sub.HandleFunc("/"+route.Path, route.Handler).
Methods(methods...)
} }
} }

View File

@@ -265,24 +265,29 @@ func (s *service) RegisterAPI(b builder.APIGroupBuilder) {
func (s *service) start(ctx context.Context) error { func (s *service) start(ctx context.Context) error {
// Get the list of groups the server will support // Get the list of groups the server will support
builders := s.builders builders := s.builders
groupVersions := make([]schema.GroupVersion, 0, len(builders)) groupVersions := make([]schema.GroupVersion, 0, len(builders))
// Install schemas // Install schemas
initialSize := len(kubeaggregator.APIVersionPriorities) initialSize := len(kubeaggregator.APIVersionPriorities)
for i, b := range builders { for i, b := range builders {
groupVersions = append(groupVersions, b.GetGroupVersion()) gvs := builder.GetGroupVersions(b)
groupVersions = append(groupVersions, gvs...)
if err := b.InstallSchema(Scheme); err != nil { if err := b.InstallSchema(Scheme); err != nil {
return err return err
} }
if s.features.IsEnabledGlobally(featuremgmt.FlagKubernetesAggregator) { for _, gv := range gvs {
// set the priority for the group+version if s.features.IsEnabledGlobally(featuremgmt.FlagKubernetesAggregator) {
kubeaggregator.APIVersionPriorities[b.GetGroupVersion()] = kubeaggregator.Priority{Group: 15000, Version: int32(i + initialSize)} // set the priority for the group+version
} kubeaggregator.APIVersionPriorities[gv] = kubeaggregator.Priority{Group: 15000, Version: int32(i + initialSize)}
}
auth := b.GetAuthorizer() if a, ok := b.(builder.APIGroupAuthorizer); ok {
if auth != nil { auth := a.GetAuthorizer()
s.authorizer.Register(b.GetGroupVersion(), auth) if auth != nil {
s.authorizer.Register(gv, auth)
}
}
} }
} }