github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/state/store/configs.go (about) 1 package store 2 3 import ( 4 "strings" 5 6 "github.com/docker/swarmkit/api" 7 memdb "github.com/hashicorp/go-memdb" 8 ) 9 10 const tableConfig = "config" 11 12 func init() { 13 register(ObjectStoreConfig{ 14 Table: &memdb.TableSchema{ 15 Name: tableConfig, 16 Indexes: map[string]*memdb.IndexSchema{ 17 indexID: { 18 Name: indexID, 19 Unique: true, 20 Indexer: api.ConfigIndexerByID{}, 21 }, 22 indexName: { 23 Name: indexName, 24 Unique: true, 25 Indexer: api.ConfigIndexerByName{}, 26 }, 27 indexCustom: { 28 Name: indexCustom, 29 Indexer: api.ConfigCustomIndexer{}, 30 AllowMissing: true, 31 }, 32 }, 33 }, 34 Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error { 35 var err error 36 snapshot.Configs, err = FindConfigs(tx, All) 37 return err 38 }, 39 Restore: func(tx Tx, snapshot *api.StoreSnapshot) error { 40 toStoreObj := make([]api.StoreObject, len(snapshot.Configs)) 41 for i, x := range snapshot.Configs { 42 toStoreObj[i] = x 43 } 44 return RestoreTable(tx, tableConfig, toStoreObj) 45 }, 46 ApplyStoreAction: func(tx Tx, sa api.StoreAction) error { 47 switch v := sa.Target.(type) { 48 case *api.StoreAction_Config: 49 obj := v.Config 50 switch sa.Action { 51 case api.StoreActionKindCreate: 52 return CreateConfig(tx, obj) 53 case api.StoreActionKindUpdate: 54 return UpdateConfig(tx, obj) 55 case api.StoreActionKindRemove: 56 return DeleteConfig(tx, obj.ID) 57 } 58 } 59 return errUnknownStoreAction 60 }, 61 }) 62 } 63 64 // CreateConfig adds a new config to the store. 65 // Returns ErrExist if the ID is already taken. 66 func CreateConfig(tx Tx, c *api.Config) error { 67 // Ensure the name is not already in use. 68 if tx.lookup(tableConfig, indexName, strings.ToLower(c.Spec.Annotations.Name)) != nil { 69 return ErrNameConflict 70 } 71 72 return tx.create(tableConfig, c) 73 } 74 75 // UpdateConfig updates an existing config in the store. 76 // Returns ErrNotExist if the config doesn't exist. 77 func UpdateConfig(tx Tx, c *api.Config) error { 78 // Ensure the name is either not in use or already used by this same Config. 79 if existing := tx.lookup(tableConfig, indexName, strings.ToLower(c.Spec.Annotations.Name)); existing != nil { 80 if existing.GetID() != c.ID { 81 return ErrNameConflict 82 } 83 } 84 85 return tx.update(tableConfig, c) 86 } 87 88 // DeleteConfig removes a config from the store. 89 // Returns ErrNotExist if the config doesn't exist. 90 func DeleteConfig(tx Tx, id string) error { 91 return tx.delete(tableConfig, id) 92 } 93 94 // GetConfig looks up a config by ID. 95 // Returns nil if the config doesn't exist. 96 func GetConfig(tx ReadTx, id string) *api.Config { 97 c := tx.get(tableConfig, id) 98 if c == nil { 99 return nil 100 } 101 return c.(*api.Config) 102 } 103 104 // FindConfigs selects a set of configs and returns them. 105 func FindConfigs(tx ReadTx, by By) ([]*api.Config, error) { 106 checkType := func(by By) error { 107 switch by.(type) { 108 case byName, byNamePrefix, byIDPrefix, byCustom, byCustomPrefix: 109 return nil 110 default: 111 return ErrInvalidFindBy 112 } 113 } 114 115 configList := []*api.Config{} 116 appendResult := func(o api.StoreObject) { 117 configList = append(configList, o.(*api.Config)) 118 } 119 120 err := tx.find(tableConfig, by, checkType, appendResult) 121 return configList, err 122 }