Add ability to add multiple schematemplates per type

This commit is contained in:
Darren Shepherd
2021-03-02 07:30:00 -07:00
parent c1ea7b9626
commit 362617a677
2 changed files with 41 additions and 33 deletions

View File

@@ -10,7 +10,6 @@ import (
"github.com/rancher/apiserver/pkg/types" "github.com/rancher/apiserver/pkg/types"
"github.com/rancher/steve/pkg/accesscontrol" "github.com/rancher/steve/pkg/accesscontrol"
"github.com/rancher/steve/pkg/attributes" "github.com/rancher/steve/pkg/attributes"
"github.com/rancher/steve/pkg/schema/converter"
"github.com/rancher/wrangler/pkg/name" "github.com/rancher/wrangler/pkg/name"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
@@ -31,7 +30,7 @@ type Collection struct {
toSync int32 toSync int32
baseSchema *types.APISchemas baseSchema *types.APISchemas
schemas map[string]*types.APISchema schemas map[string]*types.APISchema
templates map[string]*Template templates map[string][]*Template
notifiers map[int]func() notifiers map[int]func()
notifierID int notifierID int
byGVR map[schema.GroupVersionResource]string byGVR map[schema.GroupVersionResource]string
@@ -81,7 +80,7 @@ func NewCollection(ctx context.Context, baseSchema *types.APISchemas, access acc
return &Collection{ return &Collection{
baseSchema: baseSchema, baseSchema: baseSchema,
schemas: map[string]*types.APISchema{}, schemas: map[string]*types.APISchema{},
templates: map[string]*Template{}, templates: map[string][]*Template{},
byGVR: map[schema.GroupVersionResource]string{}, byGVR: map[schema.GroupVersionResource]string{},
byGVK: map[schema.GroupVersionKind]string{}, byGVK: map[schema.GroupVersionKind]string{},
cache: cache.NewLRUExpireCache(1000), cache: cache.NewLRUExpireCache(1000),
@@ -140,18 +139,30 @@ func (c *Collection) Reset(schemas map[string]*types.APISchema) {
c.lock.RUnlock() c.lock.RUnlock()
} }
func start(ctx context.Context, templates []*Template) error {
for _, template := range templates {
if template.Start == nil {
continue
}
if err := template.Start(ctx); err != nil {
return err
}
}
return nil
}
func (c *Collection) startStopTemplate(schemas map[string]*types.APISchema) { func (c *Collection) startStopTemplate(schemas map[string]*types.APISchema) {
for id := range schemas { for id := range schemas {
if _, ok := c.running[id]; ok { if _, ok := c.running[id]; ok {
continue continue
} }
template := c.templates[id] templates := c.templates[id]
if template == nil || template.Start == nil { if len(templates) == 0 {
continue continue
} }
subCtx, cancel := context.WithCancel(c.ctx) subCtx, cancel := context.WithCancel(c.ctx)
if err := template.Start(subCtx); err != nil { if err := start(subCtx, templates); err != nil {
cancel() cancel()
logrus.Errorf("failed to start schema template: %s", id) logrus.Errorf("failed to start schema template: %s", id)
continue continue
@@ -218,17 +229,12 @@ func (c *Collection) AddTemplate(templates ...Template) {
for i, template := range templates { for i, template := range templates {
if template.Kind != "" { if template.Kind != "" {
c.templates[template.Group+"/"+template.Kind] = &templates[i] c.templates[template.Group+"/"+template.Kind] = append(c.templates[template.Group+"/"+template.Kind], &templates[i])
c.templates[converter.GVKToSchemaID(schema.GroupVersionKind{ } else if template.ID != "" {
Group: template.Group, c.templates[template.ID] = append(c.templates[template.ID], &templates[i])
Kind: template.Kind,
})] = &templates[i]
}
if template.ID != "" {
c.templates[template.ID] = &templates[i]
} }
if template.Kind == "" && template.Group == "" && template.ID == "" { if template.Kind == "" && template.Group == "" && template.ID == "" {
c.templates[""] = &templates[i] c.templates[""] = append(c.templates[""], &templates[i])
} }
} }
} }

View File

@@ -132,12 +132,13 @@ func (c *Collection) applyTemplates(schema *types.APISchema) {
c.lock.RLock() c.lock.RLock()
defer c.lock.RUnlock() defer c.lock.RUnlock()
templates := []*Template{ templates := [][]*Template{
c.templates[schema.ID], c.templates[schema.ID],
c.templates[fmt.Sprintf("%s/%s", attributes.Group(schema), attributes.Kind(schema))], c.templates[fmt.Sprintf("%s/%s", attributes.Group(schema), attributes.Kind(schema))],
c.templates[""], c.templates[""],
} }
for _, templates := range templates {
for _, t := range templates { for _, t := range templates {
if t == nil { if t == nil {
continue continue
@@ -159,3 +160,4 @@ func (c *Collection) applyTemplates(schema *types.APISchema) {
} }
} }
} }
}