forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.go
188 lines (163 loc) · 5.5 KB
/
extensions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package store
import (
"errors"
"strings"
"github.com/docker/swarmkit/api"
memdb "github.com/hashicorp/go-memdb"
)
const tableExtension = "extension"
func init() {
register(ObjectStoreConfig{
Table: &memdb.TableSchema{
Name: tableExtension,
Indexes: map[string]*memdb.IndexSchema{
indexID: {
Name: indexID,
Unique: true,
Indexer: extensionIndexerByID{},
},
indexName: {
Name: indexName,
Unique: true,
Indexer: extensionIndexerByName{},
},
indexCustom: {
Name: indexCustom,
Indexer: extensionCustomIndexer{},
AllowMissing: true,
},
},
},
Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error {
var err error
snapshot.Extensions, err = FindExtensions(tx, All)
return err
},
Restore: func(tx Tx, snapshot *api.StoreSnapshot) error {
toStoreObj := make([]api.StoreObject, len(snapshot.Extensions))
for i, x := range snapshot.Extensions {
toStoreObj[i] = extensionEntry{x}
}
return RestoreTable(tx, tableExtension, toStoreObj)
},
ApplyStoreAction: func(tx Tx, sa api.StoreAction) error {
switch v := sa.Target.(type) {
case *api.StoreAction_Extension:
obj := v.Extension
switch sa.Action {
case api.StoreActionKindCreate:
return CreateExtension(tx, obj)
case api.StoreActionKindUpdate:
return UpdateExtension(tx, obj)
case api.StoreActionKindRemove:
return DeleteExtension(tx, obj.ID)
}
}
return errUnknownStoreAction
},
})
}
type extensionEntry struct {
*api.Extension
}
func (e extensionEntry) CopyStoreObject() api.StoreObject {
return extensionEntry{Extension: e.Extension.Copy()}
}
// ensure that when update events are emitted, we unwrap extensionEntry
func (e extensionEntry) EventUpdate(oldObject api.StoreObject) api.Event {
if oldObject != nil {
return api.EventUpdateExtension{Extension: e.Extension, OldExtension: oldObject.(extensionEntry).Extension}
}
return api.EventUpdateExtension{Extension: e.Extension}
}
// CreateExtension adds a new extension to the store.
// Returns ErrExist if the ID is already taken.
func CreateExtension(tx Tx, e *api.Extension) error {
// Ensure the name is not already in use.
if tx.lookup(tableExtension, indexName, strings.ToLower(e.Annotations.Name)) != nil {
return ErrNameConflict
}
// It can't conflict with built-in kinds either.
if _, ok := schema.Tables[e.Annotations.Name]; ok {
return ErrNameConflict
}
return tx.create(tableExtension, extensionEntry{e})
}
// UpdateExtension updates an existing extension in the store.
// Returns ErrNotExist if the object doesn't exist.
func UpdateExtension(tx Tx, e *api.Extension) error {
// TODO(aaronl): For the moment, extensions are immutable
return errors.New("extensions are immutable")
}
// DeleteExtension removes an extension from the store.
// Returns ErrNotExist if the object doesn't exist.
func DeleteExtension(tx Tx, id string) error {
e := tx.get(tableExtension, id)
if e == nil {
return ErrNotExist
}
resources, err := FindResources(tx, ByKind(e.(extensionEntry).Annotations.Name))
if err != nil {
return err
}
if len(resources) != 0 {
return errors.New("cannot delete extension because objects of this type exist in the data store")
}
return tx.delete(tableExtension, id)
}
// GetExtension looks up an extension by ID.
// Returns nil if the object doesn't exist.
func GetExtension(tx ReadTx, id string) *api.Extension {
e := tx.get(tableExtension, id)
if e == nil {
return nil
}
return e.(extensionEntry).Extension
}
// FindExtensions selects a set of extensions and returns them.
func FindExtensions(tx ReadTx, by By) ([]*api.Extension, error) {
checkType := func(by By) error {
switch by.(type) {
case byIDPrefix, byName, byCustom, byCustomPrefix:
return nil
default:
return ErrInvalidFindBy
}
}
extensionList := []*api.Extension{}
appendResult := func(o api.StoreObject) {
extensionList = append(extensionList, o.(extensionEntry).Extension)
}
err := tx.find(tableExtension, by, checkType, appendResult)
return extensionList, err
}
type extensionIndexerByID struct{}
func (indexer extensionIndexerByID) FromArgs(args ...interface{}) ([]byte, error) {
return api.ExtensionIndexerByID{}.FromArgs(args...)
}
func (indexer extensionIndexerByID) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return api.ExtensionIndexerByID{}.PrefixFromArgs(args...)
}
func (indexer extensionIndexerByID) FromObject(obj interface{}) (bool, []byte, error) {
return api.ExtensionIndexerByID{}.FromObject(obj.(extensionEntry).Extension)
}
type extensionIndexerByName struct{}
func (indexer extensionIndexerByName) FromArgs(args ...interface{}) ([]byte, error) {
return api.ExtensionIndexerByName{}.FromArgs(args...)
}
func (indexer extensionIndexerByName) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return api.ExtensionIndexerByName{}.PrefixFromArgs(args...)
}
func (indexer extensionIndexerByName) FromObject(obj interface{}) (bool, []byte, error) {
return api.ExtensionIndexerByName{}.FromObject(obj.(extensionEntry).Extension)
}
type extensionCustomIndexer struct{}
func (indexer extensionCustomIndexer) FromArgs(args ...interface{}) ([]byte, error) {
return api.ExtensionCustomIndexer{}.FromArgs(args...)
}
func (indexer extensionCustomIndexer) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return api.ExtensionCustomIndexer{}.PrefixFromArgs(args...)
}
func (indexer extensionCustomIndexer) FromObject(obj interface{}) (bool, [][]byte, error) {
return api.ExtensionCustomIndexer{}.FromObject(obj.(extensionEntry).Extension)
}