forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicschema.go
80 lines (65 loc) · 1.75 KB
/
dynamicschema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
}