-
Notifications
You must be signed in to change notification settings - Fork 118
/
enumerator.go
208 lines (179 loc) · 4.9 KB
/
enumerator.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
package volume
import (
"encoding/json"
"fmt"
_ "sync"
"github.com/portworx/kvdb"
"github.com/libopenstorage/openstorage/api"
)
const (
keyBase = "openstorage/"
locks = "/locks/"
volumes = "/volumes/"
)
type Store interface {
// Lock volume specified by volID.
Lock(volID api.VolumeID) (interface{}, error)
// Lock volume with token obtained from call to Lock.
Unlock(token interface{}) error
// CreateVol returns error if volume with the same ID already existe.
CreateVol(vol *api.Volume) error
// GetVol from volID.
GetVol(volID api.VolumeID) (*api.Volume, error)
// UpdateVol with vol
UpdateVol(vol *api.Volume) error
// DeleteVol. Returns error if volume does not exist.
DeleteVol(volID api.VolumeID) error
}
// DefaultEnumerator for volume information. Implements the Enumerator Interface
type DefaultEnumerator struct {
kvdb kvdb.Kvdb
driver string
lockKeyPrefix string
volKeyPrefix string
}
func (e *DefaultEnumerator) lockKey(volID api.VolumeID) string {
return e.volKeyPrefix + string(volID) + ".lock"
}
func (e *DefaultEnumerator) volKey(volID api.VolumeID) string {
return e.volKeyPrefix + string(volID)
}
func hasSubset(set api.Labels, subset api.Labels) bool {
if subset == nil || len(subset) == 0 {
return true
}
if set == nil {
return false
}
for k := range subset {
if _, ok := set[k]; !ok {
return false
}
}
return true
}
func contains(volID api.VolumeID, set []api.VolumeID) bool {
if len(set) == 0 {
return true
}
for _, v := range set {
if v == volID {
return true
}
}
return false
}
func match(v *api.Volume, locator api.VolumeLocator, configLabels api.Labels) bool {
if locator.Name != "" && v.Locator.Name != locator.Name {
return false
}
if !hasSubset(v.Locator.VolumeLabels, locator.VolumeLabels) {
return false
}
return hasSubset(v.Spec.ConfigLabels, configLabels)
}
// NewDefaultEnumerator initializes store with specified kvdb.
func NewDefaultEnumerator(driver string, kvdb kvdb.Kvdb) *DefaultEnumerator {
return &DefaultEnumerator{
kvdb: kvdb,
driver: driver,
lockKeyPrefix: keyBase + driver + locks,
volKeyPrefix: keyBase + driver + volumes,
}
}
// Lock volume specified by volID.
func (e *DefaultEnumerator) Lock(volID api.VolumeID) (interface{}, error) {
return e.kvdb.Lock(e.lockKey(volID), 10)
}
// Lock volume with token obtained from call to Lock.
func (e *DefaultEnumerator) Unlock(token interface{}) error {
v, ok := token.(*kvdb.KVPair)
if !ok {
return fmt.Errorf("Invalid token of type %T", token)
}
return e.kvdb.Unlock(v)
}
// CreateVol returns error if volume with the same ID already existe.
func (e *DefaultEnumerator) CreateVol(vol *api.Volume) error {
_, err := e.kvdb.Create(e.volKey(vol.ID), vol, 0)
return err
}
// GetVol from volID.
func (e *DefaultEnumerator) GetVol(volID api.VolumeID) (*api.Volume, error) {
var v api.Volume
_, err := e.kvdb.GetVal(e.volKey(volID), &v)
return &v, err
}
// UpdateVol with vol
func (e *DefaultEnumerator) UpdateVol(vol *api.Volume) error {
_, err := e.kvdb.Put(e.volKey(vol.ID), vol, 0)
return err
}
// DeleteVol. Returns error if volume does not exist.
func (e *DefaultEnumerator) DeleteVol(volID api.VolumeID) error {
_, err := e.kvdb.Delete(e.volKey(volID))
return err
}
// Inspect specified volumes.
// Returns slice of volumes that were found.
func (e *DefaultEnumerator) Inspect(ids []api.VolumeID) ([]api.Volume, error) {
var err error
var vol *api.Volume
vols := make([]api.Volume, 0, len(ids))
for _, v := range ids {
vol, err = e.GetVol(v)
// XXX Distinguish between ENOENT and an internal error from KVDB
if err != nil {
continue
}
vols = append(vols, *vol)
}
return vols, nil
}
// Enumerate volumes that map to the volumeLocator. Locator fields may be regexp.
// If locator fields are left blank, this will return all volumee.
func (e *DefaultEnumerator) Enumerate(locator api.VolumeLocator,
labels api.Labels) ([]api.Volume, error) {
kvp, err := e.kvdb.Enumerate(e.volKeyPrefix)
if err != nil {
return nil, err
}
vols := make([]api.Volume, 0, len(kvp))
for _, v := range kvp {
var elem api.Volume
err = json.Unmarshal(v.Value, &elem)
if err != nil {
return nil, err
}
if match(&elem, locator, labels) {
vols = append(vols, elem)
}
}
return vols, nil
}
// SnapEnumerate for specified volume
func (e *DefaultEnumerator) SnapEnumerate(
volIDs []api.VolumeID,
labels api.Labels) ([]api.Volume, error) {
kvp, err := e.kvdb.Enumerate(e.volKeyPrefix)
if err != nil {
return nil, err
}
vols := make([]api.Volume, 0, len(kvp))
for _, v := range kvp {
var elem api.Volume
err = json.Unmarshal(v.Value, &elem)
if err != nil {
return nil, err
}
if elem.Source == nil ||
elem.Source.Parent == api.BadVolumeID ||
(volIDs != nil && !contains(elem.Source.Parent, volIDs)) {
continue
}
if hasSubset(elem.Locator.VolumeLabels, labels) {
vols = append(vols, elem)
}
}
return vols, nil
}