1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-28 03:10:32 +00:00
steve/pkg/schema/collection.go

122 lines
2.9 KiB
Go
Raw Normal View History

2019-08-13 23:36:03 +00:00
package schema
import (
"strings"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/accesscontrol"
"github.com/rancher/steve/pkg/attributes"
2020-01-31 05:37:59 +00:00
"github.com/rancher/steve/pkg/schema/table"
"github.com/rancher/steve/pkg/schemaserver/types"
"github.com/rancher/wrangler/pkg/data"
2019-09-09 21:28:55 +00:00
"github.com/rancher/wrangler/pkg/name"
2020-01-31 05:37:59 +00:00
"github.com/rancher/wrangler/pkg/schemas"
2019-08-13 23:36:03 +00:00
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authentication/user"
)
type Factory interface {
2020-01-31 05:37:59 +00:00
Schemas(user user.Info) (*types.APISchemas, error)
2019-08-13 23:36:03 +00:00
ByGVR(gvr schema.GroupVersionResource) string
ByGVK(gvr schema.GroupVersionKind) string
2019-08-13 23:36:03 +00:00
}
type Collection struct {
toSync int32
2020-01-31 05:37:59 +00:00
baseSchema *types.APISchemas
schemas map[string]*types.APISchema
2019-08-13 23:36:03 +00:00
templates map[string]*Template
byGVR map[schema.GroupVersionResource]string
2019-09-09 21:28:55 +00:00
byGVK map[schema.GroupVersionKind]string
2019-08-13 23:36:03 +00:00
as *accesscontrol.AccessStore
}
type Template struct {
2019-09-09 21:28:55 +00:00
Group string
Kind string
ID string
2020-01-31 05:37:59 +00:00
Customize func(*types.APISchema)
2019-09-09 21:28:55 +00:00
Formatter types.Formatter
Store types.Store
2020-01-31 05:01:21 +00:00
StoreFactory func(types.Store) types.Store
2020-01-31 05:37:59 +00:00
Mapper schemas.Mapper
2019-09-09 21:28:55 +00:00
Columns []table.Column
ComputedColumns func(data.Object)
2019-08-13 23:36:03 +00:00
}
2020-01-31 05:37:59 +00:00
func NewCollection(baseSchema *types.APISchemas, access *accesscontrol.AccessStore) *Collection {
2019-08-13 23:36:03 +00:00
return &Collection{
baseSchema: baseSchema,
2020-01-31 05:37:59 +00:00
schemas: map[string]*types.APISchema{},
2019-08-13 23:36:03 +00:00
templates: map[string]*Template{},
byGVR: map[schema.GroupVersionResource]string{},
2019-09-09 21:28:55 +00:00
byGVK: map[schema.GroupVersionKind]string{},
2019-08-13 23:36:03 +00:00
as: access,
}
}
2020-01-31 05:37:59 +00:00
func (c *Collection) Reset(schemas map[string]*types.APISchema) {
2019-09-09 21:28:55 +00:00
byGVK := map[schema.GroupVersionKind]string{}
byGVR := map[schema.GroupVersionResource]string{}
2019-08-13 23:36:03 +00:00
for _, s := range schemas {
gvr := attributes.GVR(s)
if gvr.Resource != "" {
byGVR[gvr] = s.ID
}
2019-09-09 21:28:55 +00:00
gvk := attributes.GVK(s)
if gvk.Kind != "" {
byGVK[gvk] = s.ID
2019-08-13 23:36:03 +00:00
}
}
c.schemas = schemas
c.byGVR = byGVR
2019-09-09 21:28:55 +00:00
c.byGVK = byGVK
}
2020-01-31 05:37:59 +00:00
func (c *Collection) Schema(id string) *types.APISchema {
2019-09-09 21:28:55 +00:00
return c.schemas[id]
}
func (c *Collection) IDs() (result []string) {
seen := map[string]bool{}
for _, id := range c.byGVR {
if seen[id] {
continue
}
seen[id] = true
result = append(result, id)
}
return
2019-08-13 23:36:03 +00:00
}
func (c *Collection) ByGVR(gvr schema.GroupVersionResource) string {
2019-09-09 21:28:55 +00:00
id, ok := c.byGVR[gvr]
if ok {
return id
}
gvr.Resource = name.GuessPluralName(strings.ToLower(gvr.Resource))
return c.byGVK[schema.GroupVersionKind{
Group: gvr.Group,
Version: gvr.Version,
Kind: gvr.Resource,
}]
2019-08-13 23:36:03 +00:00
}
2019-09-09 21:28:55 +00:00
func (c *Collection) ByGVK(gvk schema.GroupVersionKind) string {
return c.byGVK[gvk]
}
2019-08-13 23:36:03 +00:00
func (c *Collection) AddTemplate(template *Template) {
if template.Kind != "" {
c.templates[template.Group+"/"+template.Kind] = template
}
if template.ID != "" {
c.templates[template.ID] = template
}
if template.Kind == "" && template.Group == "" && template.ID == "" {
c.templates[""] = template
}
}