1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-14 14:22:04 +00:00

Adding ability to replace schemas

This change makes it so that schemas can be replaced when they already exist.
This is to support the ability for kontainer drivers to update their dynamic
schemas.

Issue:
https://github.com/rancher/rancher/issues/17712
This commit is contained in:
Nathan Jenan
2019-02-05 13:20:38 -07:00
committed by Craig Jellick
parent 2d37b1235b
commit 362802224f
2 changed files with 43 additions and 25 deletions

View File

@@ -108,10 +108,16 @@ func (s *Schemas) removeReferences(schema *Schema) {
func (s *Schemas) AddSchema(schema Schema) *Schemas {
s.Lock()
defer s.Unlock()
return s.doAddSchema(schema)
return s.doAddSchema(schema, false)
}
func (s *Schemas) doAddSchema(schema Schema) *Schemas {
func (s *Schemas) ForceAddSchema(schema Schema) *Schemas {
s.Lock()
defer s.Unlock()
return s.doAddSchema(schema, true)
}
func (s *Schemas) doAddSchema(schema Schema, replace bool) *Schemas {
s.setupDefaults(&schema)
if s.AddHook != nil {
@@ -125,9 +131,20 @@ func (s *Schemas) doAddSchema(schema Schema) *Schemas {
s.versions = append(s.versions, schema.Version)
}
if _, ok := schemas[schema.ID]; !ok {
if _, ok := schemas[schema.ID]; !ok ||
(replace && schema.DynamicSchemaVersion != schemas[schema.ID].DynamicSchemaVersion) {
schemas[schema.ID] = &schema
if replace {
for i, candidate := range s.schemas {
if candidate.ID == schema.ID {
s.schemas[i] = &schema
break
}
}
} else {
s.schemas = append(s.schemas, &schema)
}
if !schema.Embed {
s.addReferences(&schema)
@@ -159,7 +176,7 @@ func (s *Schemas) removeEmbed(schema *Schema) {
}
s.doRemoveSchema(*target)
s.doAddSchema(newSchema)
s.doAddSchema(newSchema, false)
}
func (s *Schemas) embed(schema *Schema) {
@@ -184,7 +201,7 @@ func (s *Schemas) embed(schema *Schema) {
}
s.doRemoveSchema(*target)
s.doAddSchema(newSchema)
s.doAddSchema(newSchema, false)
}
func (s *Schemas) addReferences(schema *Schema) {

View File

@@ -112,6 +112,7 @@ type Schema struct {
CollectionFields map[string]Field `json:"collectionFields,omitempty"`
CollectionActions map[string]Action `json:"collectionActions,omitempty"`
CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"`
DynamicSchemaVersion string `json:"dynamicSchemaVersion,omitempty"`
Scope TypeScope `json:"-"`
InternalSchema *Schema `json:"-"`