mirror of
https://github.com/niusmallnan/steve.git
synced 2025-09-17 15:30:19 +00:00
Add ability to start on controllers when type is seen
This commit is contained in:
@@ -27,12 +27,15 @@ type SchemasHandler interface {
|
||||
type handler struct {
|
||||
sync.Mutex
|
||||
|
||||
ctx context.Context
|
||||
toSync int32
|
||||
schemas *schema2.Collection
|
||||
client discovery.DiscoveryInterface
|
||||
crd apiextcontrollerv1beta1.CustomResourceDefinitionClient
|
||||
ssar authorizationv1client.SelfSubjectAccessReviewInterface
|
||||
handler SchemasHandler
|
||||
|
||||
running map[string]func()
|
||||
}
|
||||
|
||||
func Register(ctx context.Context,
|
||||
@@ -44,11 +47,13 @@ func Register(ctx context.Context,
|
||||
schemas *schema2.Collection) (init func() error) {
|
||||
|
||||
h := &handler{
|
||||
ctx: ctx,
|
||||
client: discovery,
|
||||
schemas: schemas,
|
||||
handler: schemasHandler,
|
||||
crd: crd,
|
||||
ssar: ssar,
|
||||
running: map[string]func(){},
|
||||
}
|
||||
|
||||
apiService.OnChange(ctx, "schema", h.OnChangeAPIService)
|
||||
@@ -126,6 +131,7 @@ func (h *handler) refreshAll() error {
|
||||
filteredSchemas[id] = schema
|
||||
}
|
||||
|
||||
h.startStopTemplate(filteredSchemas)
|
||||
h.schemas.Reset(filteredSchemas)
|
||||
if h.handler != nil {
|
||||
return h.handler.OnSchemas(h.schemas)
|
||||
@@ -134,6 +140,32 @@ func (h *handler) refreshAll() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *handler) startStopTemplate(schemas map[string]*types.APISchema) {
|
||||
for id := range schemas {
|
||||
if _, ok := h.running[id]; ok {
|
||||
continue
|
||||
}
|
||||
template := h.schemas.TemplateForSchemaID(id)
|
||||
if template == nil || template.Start == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
subCtx, cancel := context.WithCancel(h.ctx)
|
||||
if err := template.Start(subCtx); err != nil {
|
||||
logrus.Errorf("failed to start schema template: %s", id)
|
||||
continue
|
||||
}
|
||||
h.running[id] = cancel
|
||||
}
|
||||
|
||||
for id, cancel := range h.running {
|
||||
if _, ok := schemas[id]; !ok {
|
||||
cancel()
|
||||
delete(h.running, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) allowed(schema *types.APISchema) (bool, error) {
|
||||
gvr := attributes.GVR(schema)
|
||||
ssar, err := h.ssar.Create(&authorizationv1.SelfSubjectAccessReview{
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/steve/pkg/accesscontrol"
|
||||
@@ -38,6 +39,7 @@ type Template struct {
|
||||
Customize func(*types.APISchema)
|
||||
Formatter types.Formatter
|
||||
Store types.Store
|
||||
Start func(ctx context.Context) error
|
||||
StoreFactory func(types.Store) types.Store
|
||||
Mapper schemas.Mapper
|
||||
Columns []table.Column
|
||||
@@ -108,6 +110,10 @@ func (c *Collection) ByGVK(gvk schema.GroupVersionKind) string {
|
||||
return c.byGVK[gvk]
|
||||
}
|
||||
|
||||
func (c *Collection) TemplateForSchemaID(id string) *Template {
|
||||
return c.templates[id]
|
||||
}
|
||||
|
||||
func (c *Collection) AddTemplate(template *Template) {
|
||||
if template.Kind != "" {
|
||||
c.templates[template.Group+"/"+template.Kind] = template
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/rancher/wrangler/pkg/data/convert"
|
||||
"github.com/rancher/wrangler/pkg/schemas/validation"
|
||||
meta2 "k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
@@ -220,6 +221,9 @@ type APIObjectList struct {
|
||||
}
|
||||
|
||||
func (a *APIObject) Data() data.Object {
|
||||
if unstr, ok := a.Object.(*unstructured.Unstructured); ok {
|
||||
return unstr.Object
|
||||
}
|
||||
data, err := convert.EncodeToMap(a.Object)
|
||||
if err != nil {
|
||||
return convert.ToMapInterface(a.Object)
|
||||
|
Reference in New Issue
Block a user