37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dynamicschema.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-02-20 12:54 . Add HA support and cluster import
package dynamicschema
import (
"sync"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/apis/management.cattle.io/v3"
managementSchema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/config"
)
type Controller struct {
sync.Mutex
Schemas *types.Schemas
lister v3.DynamicSchemaLister
known map[string]bool
}
func Register(management *config.ScaledContext, schemas *types.Schemas) {
c := &Controller{
Schemas: schemas,
}
management.Management.DynamicSchemas("").AddHandler("dynamic-schema", c.Sync)
}
func (c *Controller) Sync(key string, dynamicSchema *v3.DynamicSchema) error {
c.Lock()
defer c.Unlock()
if dynamicSchema == nil {
return c.remove(key)
}
return c.add(dynamicSchema)
}
func (c *Controller) remove(id string) error {
schema := c.Schemas.Schema(&managementSchema.Version, id)
if schema != nil {
c.Schemas.RemoveSchema(*schema)
}
return nil
}
func (c *Controller) add(dynamicSchema *v3.DynamicSchema) error {
schema := types.Schema{}
if err := convert.ToObj(dynamicSchema.Spec, &schema); err != nil {
return err
}
for name, field := range schema.ResourceFields {
defMap, ok := field.Default.(map[string]interface{})
if !ok {
continue
}
// set to nil because if map is len() == 0
field.Default = nil
switch field.Type {
case "string":
field.Default = defMap["stringValue"]
case "int":
field.Default = defMap["intValue"]
case "boolean":
field.Default = defMap["boolValue"]
case "array[string]":
field.Default = defMap["stringSliceValue"]
}
schema.ResourceFields[name] = field
}
schema.ID = dynamicSchema.Name
schema.Version = managementSchema.Version
c.Schemas.AddSchema(schema)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.7

搜索帮助

0d507c66 1850385 C8b1a773 1850385