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 {
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{