Add ability to start on controllers when type is seen

This commit is contained in:
Darren Shepherd
2020-01-31 21:29:43 -07:00
parent 91dde5ead6
commit b61ac7d555
3 changed files with 42 additions and 0 deletions

View File

@@ -27,12 +27,15 @@ type SchemasHandler interface {
type handler struct { type handler struct {
sync.Mutex sync.Mutex
ctx context.Context
toSync int32 toSync int32
schemas *schema2.Collection schemas *schema2.Collection
client discovery.DiscoveryInterface client discovery.DiscoveryInterface
crd apiextcontrollerv1beta1.CustomResourceDefinitionClient crd apiextcontrollerv1beta1.CustomResourceDefinitionClient
ssar authorizationv1client.SelfSubjectAccessReviewInterface ssar authorizationv1client.SelfSubjectAccessReviewInterface
handler SchemasHandler handler SchemasHandler
running map[string]func()
} }
func Register(ctx context.Context, func Register(ctx context.Context,
@@ -44,11 +47,13 @@ func Register(ctx context.Context,
schemas *schema2.Collection) (init func() error) { schemas *schema2.Collection) (init func() error) {
h := &handler{ h := &handler{
ctx: ctx,
client: discovery, client: discovery,
schemas: schemas, schemas: schemas,
handler: schemasHandler, handler: schemasHandler,
crd: crd, crd: crd,
ssar: ssar, ssar: ssar,
running: map[string]func(){},
} }
apiService.OnChange(ctx, "schema", h.OnChangeAPIService) apiService.OnChange(ctx, "schema", h.OnChangeAPIService)
@@ -126,6 +131,7 @@ func (h *handler) refreshAll() error {
filteredSchemas[id] = schema filteredSchemas[id] = schema
} }
h.startStopTemplate(filteredSchemas)
h.schemas.Reset(filteredSchemas) h.schemas.Reset(filteredSchemas)
if h.handler != nil { if h.handler != nil {
return h.handler.OnSchemas(h.schemas) return h.handler.OnSchemas(h.schemas)
@@ -134,6 +140,32 @@ func (h *handler) refreshAll() error {
return nil 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) { func (h *handler) allowed(schema *types.APISchema) (bool, error) {
gvr := attributes.GVR(schema) gvr := attributes.GVR(schema)
ssar, err := h.ssar.Create(&authorizationv1.SelfSubjectAccessReview{ ssar, err := h.ssar.Create(&authorizationv1.SelfSubjectAccessReview{

View File

@@ -1,6 +1,7 @@
package schema package schema
import ( import (
"context"
"strings" "strings"
"github.com/rancher/steve/pkg/accesscontrol" "github.com/rancher/steve/pkg/accesscontrol"
@@ -38,6 +39,7 @@ type Template struct {
Customize func(*types.APISchema) Customize func(*types.APISchema)
Formatter types.Formatter Formatter types.Formatter
Store types.Store Store types.Store
Start func(ctx context.Context) error
StoreFactory func(types.Store) types.Store StoreFactory func(types.Store) types.Store
Mapper schemas.Mapper Mapper schemas.Mapper
Columns []table.Column Columns []table.Column
@@ -108,6 +110,10 @@ func (c *Collection) ByGVK(gvk schema.GroupVersionKind) string {
return c.byGVK[gvk] return c.byGVK[gvk]
} }
func (c *Collection) TemplateForSchemaID(id string) *Template {
return c.templates[id]
}
func (c *Collection) AddTemplate(template *Template) { func (c *Collection) AddTemplate(template *Template) {
if template.Kind != "" { if template.Kind != "" {
c.templates[template.Group+"/"+template.Kind] = template c.templates[template.Group+"/"+template.Kind] = template

View File

@@ -10,6 +10,7 @@ import (
"github.com/rancher/wrangler/pkg/data/convert" "github.com/rancher/wrangler/pkg/data/convert"
"github.com/rancher/wrangler/pkg/schemas/validation" "github.com/rancher/wrangler/pkg/schemas/validation"
meta2 "k8s.io/apimachinery/pkg/api/meta" meta2 "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/authentication/user" "k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/endpoints/request"
@@ -220,6 +221,9 @@ type APIObjectList struct {
} }
func (a *APIObject) Data() data.Object { func (a *APIObject) Data() data.Object {
if unstr, ok := a.Object.(*unstructured.Unstructured); ok {
return unstr.Object
}
data, err := convert.EncodeToMap(a.Object) data, err := convert.EncodeToMap(a.Object)
if err != nil { if err != nil {
return convert.ToMapInterface(a.Object) return convert.ToMapInterface(a.Object)