forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.go
214 lines (188 loc) · 6.35 KB
/
resources.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package store
import (
"strings"
"github.com/docker/swarmkit/api"
memdb "github.com/hashicorp/go-memdb"
"github.com/pkg/errors"
)
const tableResource = "resource"
var (
// ErrNoKind is returned by resource create operations if the provided Kind
// of the resource does not exist
ErrNoKind = errors.New("object kind is unregistered")
)
func init() {
register(ObjectStoreConfig{
Table: &memdb.TableSchema{
Name: tableResource,
Indexes: map[string]*memdb.IndexSchema{
indexID: {
Name: indexID,
Unique: true,
Indexer: resourceIndexerByID{},
},
indexName: {
Name: indexName,
Unique: true,
Indexer: resourceIndexerByName{},
},
indexKind: {
Name: indexKind,
Indexer: resourceIndexerByKind{},
},
indexCustom: {
Name: indexCustom,
Indexer: resourceCustomIndexer{},
AllowMissing: true,
},
},
},
Save: func(tx ReadTx, snapshot *api.StoreSnapshot) error {
var err error
snapshot.Resources, err = FindResources(tx, All)
return err
},
Restore: func(tx Tx, snapshot *api.StoreSnapshot) error {
toStoreObj := make([]api.StoreObject, len(snapshot.Resources))
for i, x := range snapshot.Resources {
toStoreObj[i] = resourceEntry{x}
}
return RestoreTable(tx, tableResource, toStoreObj)
},
ApplyStoreAction: func(tx Tx, sa api.StoreAction) error {
switch v := sa.Target.(type) {
case *api.StoreAction_Resource:
obj := v.Resource
switch sa.Action {
case api.StoreActionKindCreate:
return CreateResource(tx, obj)
case api.StoreActionKindUpdate:
return UpdateResource(tx, obj)
case api.StoreActionKindRemove:
return DeleteResource(tx, obj.ID)
}
}
return errUnknownStoreAction
},
})
}
type resourceEntry struct {
*api.Resource
}
func (r resourceEntry) CopyStoreObject() api.StoreObject {
return resourceEntry{Resource: r.Resource.Copy()}
}
// ensure that when update events are emitted, we unwrap resourceEntry
func (r resourceEntry) EventUpdate(oldObject api.StoreObject) api.Event {
if oldObject != nil {
return api.EventUpdateResource{Resource: r.Resource, OldResource: oldObject.(resourceEntry).Resource}
}
return api.EventUpdateResource{Resource: r.Resource}
}
func confirmExtension(tx Tx, r *api.Resource) error {
// There must be an extension corresponding to the Kind field.
extensions, err := FindExtensions(tx, ByName(r.Kind))
if err != nil {
return errors.Wrap(err, "failed to query extensions")
}
if len(extensions) == 0 {
return ErrNoKind
}
return nil
}
// CreateResource adds a new resource object to the store.
// Returns ErrExist if the ID is already taken.
// Returns ErrNameConflict if a Resource with this Name already exists
// Returns ErrNoKind if the specified Kind does not exist
func CreateResource(tx Tx, r *api.Resource) error {
if err := confirmExtension(tx, r); err != nil {
return err
}
// TODO(dperny): currently the "name" index is unique, which means only one
// Resource of _any_ Kind can exist with that name. This isn't a problem
// right now, but the ideal case would be for names to be namespaced to the
// kind.
if tx.lookup(tableResource, indexName, strings.ToLower(r.Annotations.Name)) != nil {
return ErrNameConflict
}
return tx.create(tableResource, resourceEntry{r})
}
// UpdateResource updates an existing resource object in the store.
// Returns ErrNotExist if the object doesn't exist.
func UpdateResource(tx Tx, r *api.Resource) error {
if err := confirmExtension(tx, r); err != nil {
return err
}
return tx.update(tableResource, resourceEntry{r})
}
// DeleteResource removes a resource object from the store.
// Returns ErrNotExist if the object doesn't exist.
func DeleteResource(tx Tx, id string) error {
return tx.delete(tableResource, id)
}
// GetResource looks up a resource object by ID.
// Returns nil if the object doesn't exist.
func GetResource(tx ReadTx, id string) *api.Resource {
r := tx.get(tableResource, id)
if r == nil {
return nil
}
return r.(resourceEntry).Resource
}
// FindResources selects a set of resource objects and returns them.
func FindResources(tx ReadTx, by By) ([]*api.Resource, error) {
checkType := func(by By) error {
switch by.(type) {
case byIDPrefix, byName, byNamePrefix, byKind, byCustom, byCustomPrefix:
return nil
default:
return ErrInvalidFindBy
}
}
resourceList := []*api.Resource{}
appendResult := func(o api.StoreObject) {
resourceList = append(resourceList, o.(resourceEntry).Resource)
}
err := tx.find(tableResource, by, checkType, appendResult)
return resourceList, err
}
type resourceIndexerByKind struct{}
func (ri resourceIndexerByKind) FromArgs(args ...interface{}) ([]byte, error) {
return fromArgs(args...)
}
func (ri resourceIndexerByKind) FromObject(obj interface{}) (bool, []byte, error) {
r := obj.(resourceEntry)
// Add the null character as a terminator
val := r.Resource.Kind + "\x00"
return true, []byte(val), nil
}
type resourceIndexerByID struct{}
func (indexer resourceIndexerByID) FromArgs(args ...interface{}) ([]byte, error) {
return api.ResourceIndexerByID{}.FromArgs(args...)
}
func (indexer resourceIndexerByID) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return api.ResourceIndexerByID{}.PrefixFromArgs(args...)
}
func (indexer resourceIndexerByID) FromObject(obj interface{}) (bool, []byte, error) {
return api.ResourceIndexerByID{}.FromObject(obj.(resourceEntry).Resource)
}
type resourceIndexerByName struct{}
func (indexer resourceIndexerByName) FromArgs(args ...interface{}) ([]byte, error) {
return api.ResourceIndexerByName{}.FromArgs(args...)
}
func (indexer resourceIndexerByName) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return api.ResourceIndexerByName{}.PrefixFromArgs(args...)
}
func (indexer resourceIndexerByName) FromObject(obj interface{}) (bool, []byte, error) {
return api.ResourceIndexerByName{}.FromObject(obj.(resourceEntry).Resource)
}
type resourceCustomIndexer struct{}
func (indexer resourceCustomIndexer) FromArgs(args ...interface{}) ([]byte, error) {
return api.ResourceCustomIndexer{}.FromArgs(args...)
}
func (indexer resourceCustomIndexer) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return api.ResourceCustomIndexer{}.PrefixFromArgs(args...)
}
func (indexer resourceCustomIndexer) FromObject(obj interface{}) (bool, [][]byte, error) {
return api.ResourceCustomIndexer{}.FromObject(obj.(resourceEntry).Resource)
}