-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_class.go
315 lines (279 loc) · 9.04 KB
/
storage_class.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2018 NetApp, Inc. All Rights Reserved.
package storageclass
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/netapp/trident/config"
"github.com/netapp/trident/storage"
"github.com/netapp/trident/storage_attribute"
)
func New(c *Config) *StorageClass {
if c.Version == "" {
c.Version = config.OrchestratorAPIVersion
}
return &StorageClass{
config: c,
pools: make([]*storage.Pool, 0),
}
}
func NewForConfig(configJSON string) (*StorageClass, error) {
var scConfig Config
err := json.Unmarshal([]byte(configJSON), &scConfig)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal config: %v", err)
}
return New(&scConfig), nil
}
func NewFromPersistent(persistent *Persistent) *StorageClass {
return New(persistent.Config)
}
func (s *StorageClass) regexMatcherImpl(storagePool *storage.Pool, storagePoolBackendName string, storagePoolList []string) bool {
if storagePool == nil {
return false
}
if storagePoolBackendName == "" {
return false
}
if storagePoolList == nil {
return false
}
if !strings.HasPrefix(storagePoolBackendName, "^") {
storagePoolBackendName = "^" + storagePoolBackendName
}
if !strings.HasSuffix(storagePoolBackendName, "$") {
storagePoolBackendName = storagePoolBackendName + "$"
}
poolsMatch := false
for _, storagePoolName := range storagePoolList {
backendMatch, err := regexp.MatchString(storagePoolBackendName, storagePool.Backend.Name)
if err != nil {
log.WithFields(log.Fields{
"storagePoolName": storagePoolName,
"storagePool.Name": storagePool.Name,
"storagePool.Backend.Name": storagePool.Backend.Name,
"storagePoolBackendName": storagePoolBackendName,
"err": err,
}).Warning("Error comparing backend names in regexMatcher.")
continue
}
log.WithFields(log.Fields{
"storagePool.Backend.Name": storagePool.Backend.Name,
"storagePoolBackendName": storagePoolBackendName,
"backendMatch": backendMatch,
}).Debug("Compared backend names in regexMatcher.")
if !backendMatch {
continue
}
matched, err := regexp.MatchString(storagePoolName, storagePool.Name)
if err != nil {
log.WithFields(log.Fields{
"storagePoolName": storagePoolName,
"storagePool.Name": storagePool.Name,
"storagePool.Backend.Name": storagePool.Backend.Name,
"poolsMatch": poolsMatch,
"err": err,
}).Warning("Error comparing pool names in regexMatcher.")
continue
}
if matched {
poolsMatch = true
}
log.WithFields(log.Fields{
"storagePoolName": storagePoolName,
"storagePool.Name": storagePool.Name,
"storagePool.Backend.Name": storagePool.Backend.Name,
"poolsMatch": poolsMatch,
}).Debug("Compared pool names in regexMatcher.")
}
return poolsMatch
}
func (s *StorageClass) regexMatcher(storagePool *storage.Pool, poolMap map[string][]string) bool {
poolsMatch := false
if len(poolMap) > 0 {
for storagePoolBackendName, storagePoolList := range poolMap {
poolsMatch = s.regexMatcherImpl(storagePool, storagePoolBackendName, storagePoolList)
if poolsMatch {
return true
}
}
}
return poolsMatch
}
func (s *StorageClass) Matches(storagePool *storage.Pool) bool {
log.WithFields(log.Fields{
"storageClass": s.GetName(),
"config": s.config,
"pool": storagePool.Name,
"poolBackend": storagePool.Backend.Name,
}).Debug("Checking if storage pool matches.")
// Check excludeStoragePools first, since it can reject a match
if len(s.config.ExcludePools) > 0 {
if matches := s.regexMatcher(storagePool, s.config.ExcludePools); matches {
return false
}
}
// Check additionalStoragePools next, since it can yield a match result by itself
if len(s.config.AdditionalPools) > 0 {
if matches := s.regexMatcher(storagePool, s.config.AdditionalPools); matches {
return true
}
// Handle the sub-case where additionalStoragePools is specified (but didn't match) and
// there are no attributes or storagePools specified in the storage class. This should
// always return false.
if len(s.config.Attributes) == 0 && len(s.config.Pools) == 0 {
log.WithFields(log.Fields{
"storageClass": s.GetName(),
"pool": storagePool.Name,
}).Debug("Pool failed to match storage class additionalStoragePools attribute.")
return false
}
}
// Attributes are used to narrow the pool selection. Therefore if no attributes are
// specified, then all pools can match. If one or more attributes are specified in the
// storage class, then all must match.
attributesMatch := true
for name, request := range s.config.Attributes {
if offer, ok := storagePool.Attributes[name]; !ok || !offer.Matches(request) {
log.WithFields(log.Fields{
"offer": offer,
"request": request,
"storageClass": s.GetName(),
"pool": storagePool.Name,
"attribute": name,
"found": ok,
}).Debug("Attribute for storage pool failed to match storage class.")
attributesMatch = false
break
}
}
// The storagePools list is used to narrow the pool selection. Therefore, if no pools are
// specified, then all pools can match. If one or more pools are listed in the storage
// class, then the pool must be in the list.
poolsMatch := true
if len(s.config.Pools) > 0 {
poolsMatch = s.regexMatcher(storagePool, s.config.Pools)
}
result := attributesMatch && poolsMatch
log.WithFields(log.Fields{
"attributesMatch": attributesMatch,
"poolsMatch": poolsMatch,
"match": result,
"pool": storagePool.Name,
"storageClass": s.GetName(),
}).Debug("Result of pool match for storage class.")
return result
}
// CheckAndAddBackend iterates through each of the storage pools
// for a given backend. If the pool satisfies the storage class, it
// adds that pool. Returns the number of storage pools added.
func (s *StorageClass) CheckAndAddBackend(b *storage.Backend) int {
log.WithFields(log.Fields{
"backend": b.Name,
"storageClass": s.GetName(),
}).Debug("Checking backend for storage class")
if !b.Online {
log.WithField("backend", b.Name).Warn("Backend not online.")
return 0
}
added := 0
for _, storagePool := range b.Storage {
if s.Matches(storagePool) {
s.pools = append(s.pools, storagePool)
storagePool.AddStorageClass(s.GetName())
added++
log.WithFields(log.Fields{
"pool": storagePool.Name,
"storageClass": s.GetName(),
}).Debug("Storage class added to the storage pool.")
}
}
return added
}
func (s *StorageClass) RemovePoolsForBackend(backend *storage.Backend) {
newStoragePools := make([]*storage.Pool, 0)
for _, storagePool := range s.pools {
if storagePool.Backend != backend {
newStoragePools = append(newStoragePools, storagePool)
}
}
s.pools = newStoragePools
}
func (s *StorageClass) GetAttributes() map[string]storageattribute.Request {
return s.config.Attributes
}
func (s *StorageClass) GetName() string {
return s.config.Name
}
func (s *StorageClass) GetStoragePools() map[string][]string {
return s.config.Pools
}
func (s *StorageClass) GetAdditionalStoragePools() map[string][]string {
return s.config.AdditionalPools
}
func (s *StorageClass) GetStoragePoolsForProtocol(p config.Protocol) []*storage.Pool {
ret := make([]*storage.Pool, 0, len(s.pools))
// TODO: Change this to work with indices of backends?
for _, storagePool := range s.pools {
if p == config.ProtocolAny || storagePool.Backend.GetProtocol() == p {
ret = append(ret, storagePool)
}
}
return ret
}
func (s *StorageClass) ConstructExternal() *External {
ret := &External{
Config: s.config,
StoragePools: make(map[string][]string),
}
for _, storagePool := range s.pools {
backendName := storagePool.Backend.Name
if storagePoolList, ok := ret.StoragePools[backendName]; ok {
log.WithFields(log.Fields{
"storageClass": s.GetName(),
"pool": storagePool.Name,
"Backend": backendName,
"Method": "ConstructExternal",
}).Debug("Appending to existing storage pool list for backend.")
ret.StoragePools[backendName] = append(storagePoolList, storagePool.Name)
} else {
log.WithFields(log.Fields{
"storageClass": s.GetName(),
"pool": storagePool.Name,
"Backend": backendName,
"Method": "ConstructExternal",
}).Debug("Creating new storage pool list for backend.")
ret.StoragePools[backendName] = make([]string, 1, 1)
ret.StoragePools[backendName][0] = storagePool.Name
}
}
for _, list := range ret.StoragePools {
sort.Strings(list)
}
for _, list := range ret.Config.Pools {
sort.Strings(list)
}
for _, list := range ret.Config.AdditionalPools {
sort.Strings(list)
}
return ret
}
func (s *External) GetName() string {
return s.Config.Name
}
func (s *StorageClass) ConstructPersistent() *Persistent {
ret := &Persistent{Config: s.config}
for _, list := range ret.Config.Pools {
sort.Strings(list)
}
for _, list := range ret.Config.AdditionalPools {
sort.Strings(list)
}
return ret
}
func (s *Persistent) GetName() string {
return s.Config.Name
}