mirror of
https://github.com/niusmallnan/steve.git
synced 2025-08-31 12:48:54 +00:00
Add ability to add multiple schematemplates per type
This commit is contained in:
@@ -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])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -132,30 +132,32 @@ 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 _, t := range templates {
|
for _, templates := range templates {
|
||||||
if t == nil {
|
for _, t := range templates {
|
||||||
continue
|
if t == nil {
|
||||||
}
|
continue
|
||||||
if schema.Formatter == nil {
|
}
|
||||||
schema.Formatter = t.Formatter
|
if schema.Formatter == nil {
|
||||||
} else if t.Formatter != nil {
|
schema.Formatter = t.Formatter
|
||||||
schema.Formatter = types.FormatterChain(t.Formatter, schema.Formatter)
|
} else if t.Formatter != nil {
|
||||||
}
|
schema.Formatter = types.FormatterChain(t.Formatter, schema.Formatter)
|
||||||
if schema.Store == nil {
|
}
|
||||||
if t.StoreFactory == nil {
|
if schema.Store == nil {
|
||||||
schema.Store = t.Store
|
if t.StoreFactory == nil {
|
||||||
} else {
|
schema.Store = t.Store
|
||||||
schema.Store = t.StoreFactory(templates[2].Store)
|
} else {
|
||||||
|
schema.Store = t.StoreFactory(templates[2].Store)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if t.Customize != nil {
|
||||||
|
t.Customize(schema)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if t.Customize != nil {
|
|
||||||
t.Customize(schema)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user