-
Notifications
You must be signed in to change notification settings - Fork 56
/
storagepool.go
403 lines (339 loc) · 11.2 KB
/
storagepool.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package v1
import (
"fmt"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
lclient "github.com/LINBIT/golinstor/client"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
)
type LinstorStoragePool struct {
// Name of the storage pool in linstor.
//+kubebuilder:validation:MinLength=3
Name string `json:"name"`
// Properties to set on the storage pool.
// +listType=map
// +listMapKey=name
// +patchMergeKey=name
// +patchStrategy=merge
Properties []LinstorNodeProperty `json:"properties,omitempty"`
// Configures a LVM Volume Group as storage pool.
// +kubebuilder:validation:Optional
LvmPool *LinstorStoragePoolLvm `json:"lvmPool,omitempty"`
// Configures a LVM Thin Pool as storage pool.
// +kubebuilder:validation:Optional
LvmThinPool *LinstorStoragePoolLvmThin `json:"lvmThinPool,omitempty"`
// Configures a file system based storage pool, allocating a regular file per volume.
// +kubebuilder:validation:Optional
FilePool *LinstorStoragePoolFile `json:"filePool,omitempty"`
// Configures a file system based storage pool, allocating a sparse file per volume.
// +kubebuilder:validation:Optional
FileThinPool *LinstorStoragePoolFile `json:"fileThinPool,omitempty"`
// Configures a ZFS system based storage pool, allocating zvols from the given zpool.
// +kubebuilder:validation:Optional
ZfsPool *LinstorStoragePoolZfs `json:"zfsPool,omitempty"`
// Configures a ZFS system based storage pool, allocating sparse zvols from the given zpool.
// +kubebuilder:validation:Optional
ZfsThinPool *LinstorStoragePoolZfs `json:"zfsThinPool,omitempty"`
Source *LinstorStoragePoolSource `json:"source,omitempty"`
}
func (p *LinstorStoragePool) ProviderKind() lclient.ProviderKind {
switch {
case p.LvmPool != nil:
return lclient.LVM
case p.LvmThinPool != nil:
return lclient.LVM_THIN
case p.FilePool != nil:
return lclient.FILE
case p.FileThinPool != nil:
return lclient.FILE_THIN
case p.ZfsPool != nil:
return lclient.ZFS
case p.ZfsThinPool != nil:
return lclient.ZFS_THIN
}
return ""
}
func (p *LinstorStoragePool) PoolName() string {
switch {
case p.LvmPool != nil:
if p.LvmPool.VolumeGroup != "" {
return p.LvmPool.VolumeGroup
}
return p.Name
case p.LvmThinPool != nil:
lvName := p.LvmThinPool.ThinPool
if lvName == "" {
lvName = p.Name
}
vgName := p.LvmThinPool.VolumeGroup
if vgName == "" {
vgName = fmt.Sprintf("linstor_%s", lvName)
}
return fmt.Sprintf("%s/%s", vgName, lvName)
case p.FilePool != nil:
return p.FilePool.DirectoryOrDefault(p.Name)
case p.FileThinPool != nil:
return p.FileThinPool.DirectoryOrDefault(p.Name)
case p.ZfsPool != nil:
if p.ZfsPool.ZPool == "" {
return p.Name
}
return p.ZfsPool.ZPool
case p.ZfsThinPool != nil:
if p.ZfsThinPool.ZPool == "" {
return p.Name
}
return p.ZfsThinPool.ZPool
}
return ""
}
type LinstorStoragePoolLvm struct {
VolumeGroup string `json:"volumeGroup,omitempty"`
}
type LinstorStoragePoolLvmThin struct {
VolumeGroup string `json:"volumeGroup,omitempty"`
// ThinPool is the name of the thinpool LV (without VG prefix).
ThinPool string `json:"thinPool,omitempty"`
}
type LinstorStoragePoolFile struct {
// Directory is the path to the host directory used to store volume data.
Directory string `json:"directory,omitempty"`
}
type LinstorStoragePoolZfs struct {
// ZPool is the name of the ZFS zpool.
ZPool string `json:"zPool,omitempty"`
}
type LinstorStoragePoolSource struct {
// HostDevices is a list of device paths used to configure the given pool.
// +kubebuilder:validation:Optional
// +kubebuilder:validation:MinItems:=1
HostDevices []string `json:"hostDevices,omitempty"`
}
var (
SPRegexp = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_-]{1,46}[A-Za-z0-9]$")
VGRegexp = regexp.MustCompile("^[A-Za-z0-9.+_-]+$")
)
func ValidateStoragePools(curSPs, oldSPs []LinstorStoragePool, fieldPrefix *field.Path) field.ErrorList {
var result field.ErrorList
devNames := sets.New[string]()
for i := range curSPs {
curSP := &curSPs[i]
if !SPRegexp.MatchString(curSP.Name) {
result = append(result, field.Invalid(
fieldPrefix.Child(strconv.Itoa(i), "name"),
curSP.Name,
"Not a valid LINSTOR Storage Pool name",
))
}
var oldSP *LinstorStoragePool
for j := range oldSPs {
if oldSPs[j].Name == curSP.Name {
oldSP = &oldSPs[j]
break
}
}
numPoolTypes := 0
if curSP.LvmThinPool != nil {
result = append(result, validateStoragePoolType(&numPoolTypes, fieldPrefix.Child(strconv.Itoa(i), "lvmThinPool"))...)
result = append(result, curSP.LvmThinPool.Validate(oldSP, fieldPrefix.Child(strconv.Itoa(i), "lvmThinPool"))...)
}
if curSP.LvmPool != nil {
result = append(result, validateStoragePoolType(&numPoolTypes, fieldPrefix.Child(strconv.Itoa(i), "lvmPool"))...)
result = append(result, curSP.LvmPool.Validate(oldSP, fieldPrefix.Child(strconv.Itoa(i), "lvmPool"))...)
}
if curSP.FilePool != nil {
result = append(result, validateStoragePoolType(&numPoolTypes, fieldPrefix.Child(strconv.Itoa(i), "filePool"))...)
result = append(result, curSP.FilePool.Validate(oldSP, fieldPrefix.Child(strconv.Itoa(i), "filePool"), curSP.Name, false)...)
result = append(result, validateNoSource(curSP.Source, fieldPrefix.Child(strconv.Itoa(i)), "filePool")...)
}
if curSP.FileThinPool != nil {
result = append(result, validateStoragePoolType(&numPoolTypes, fieldPrefix.Child(strconv.Itoa(i), "fileThinPool"))...)
result = append(result, curSP.FileThinPool.Validate(oldSP, fieldPrefix.Child(strconv.Itoa(i), "fileThinPool"), curSP.Name, true)...)
result = append(result, validateNoSource(curSP.Source, fieldPrefix.Child(strconv.Itoa(i)), "fileThinPool")...)
}
if curSP.ZfsPool != nil {
result = append(result, validateStoragePoolType(&numPoolTypes, fieldPrefix.Child(strconv.Itoa(i), "zfsPool"))...)
result = append(result, curSP.ZfsPool.Validate(oldSP, fieldPrefix.Child(strconv.Itoa(i)), "zfsPool", false)...)
}
if curSP.ZfsThinPool != nil {
result = append(result, validateStoragePoolType(&numPoolTypes, fieldPrefix.Child(strconv.Itoa(i), "zfsThinPool"))...)
result = append(result, curSP.ZfsThinPool.Validate(oldSP, fieldPrefix.Child(strconv.Itoa(i)), "zfsThinPool", true)...)
}
if numPoolTypes == 0 {
result = append(result, field.Required(
fieldPrefix.Child(strconv.Itoa(i)),
"Must specify exactly 1 type of storage pool",
))
}
result = append(result,
curSP.Source.Validate(oldSP, devNames, fieldPrefix.Child(strconv.Itoa(i), "source"))...,
)
}
return result
}
func validateNoSource(src *LinstorStoragePoolSource, p *field.Path, name string) field.ErrorList {
if src != nil {
return field.ErrorList{
field.Invalid(p, src, fmt.Sprintf("Storage Pool Type '%s' does not support setting a source", name)),
}
}
return nil
}
func validateStoragePoolType(numPools *int, p *field.Path) field.ErrorList {
*numPools++
if *numPools > 1 {
return field.ErrorList{
field.Forbidden(p, "Must specify exactly 1 type of storage pool"),
}
}
return nil
}
func (l *LinstorStoragePoolLvmThin) Validate(oldSP *LinstorStoragePool, fieldPrefix *field.Path) field.ErrorList {
var result field.ErrorList
if oldSP != nil && oldSP.LvmThinPool == nil {
result = append(result, field.Forbidden(
fieldPrefix,
"Cannot change storage pool type",
))
}
if l.VolumeGroup != "" && !VGRegexp.MatchString(l.VolumeGroup) {
result = append(result, field.Invalid(
fieldPrefix.Child("volumeGroup"),
l.VolumeGroup,
"Not a valid VG name",
))
}
if oldSP != nil && l.VolumeGroup != oldSP.LvmThinPool.VolumeGroup {
result = append(result, field.Forbidden(
fieldPrefix.Child("volumeGroup"),
"Cannot change VG name",
))
}
if l.ThinPool != "" && !VGRegexp.MatchString(l.ThinPool) {
result = append(result, field.Invalid(
fieldPrefix.Child("thinPool"),
l.ThinPool,
"Not a valid thinpool LV name",
))
}
if oldSP != nil && l.ThinPool != oldSP.LvmThinPool.ThinPool {
result = append(result, field.Forbidden(
fieldPrefix.Child("thinPool"),
"Cannot change thinpool LV name",
))
}
return result
}
func (l *LinstorStoragePoolLvm) Validate(oldSP *LinstorStoragePool, fieldPrefix *field.Path) field.ErrorList {
var result field.ErrorList
if oldSP != nil && oldSP.LvmPool == nil {
result = append(result, field.Forbidden(
fieldPrefix,
"Cannot change storage pool type",
))
}
if l.VolumeGroup != "" && !VGRegexp.MatchString(l.VolumeGroup) {
result = append(result, field.Invalid(
fieldPrefix.Child("volumeGroup"),
l.VolumeGroup,
"Not a valid VG name",
))
}
if oldSP != nil && l.VolumeGroup != oldSP.LvmPool.VolumeGroup {
result = append(result, field.Forbidden(
fieldPrefix.Child("volumeGroup"),
"Cannot change VG name",
))
}
return result
}
func (l *LinstorStoragePoolFile) Validate(oldSP *LinstorStoragePool, fieldPrefix *field.Path, name string, thin bool) field.ErrorList {
var result field.ErrorList
if oldSP != nil {
if thin && oldSP.FileThinPool == nil {
result = append(result, field.Forbidden(
fieldPrefix,
"Cannot change storage pool type",
))
} else if !thin && oldSP.FilePool == nil {
result = append(result, field.Forbidden(
fieldPrefix,
"Cannot change storage pool type",
))
}
}
if !filepath.IsAbs(l.DirectoryOrDefault(name)) || filepath.Clean(l.DirectoryOrDefault(name)) != l.DirectoryOrDefault(name) {
result = append(result, field.Invalid(
fieldPrefix.Child("directory"),
l.DirectoryOrDefault(name),
"Not an absolute path",
))
}
return result
}
func (l *LinstorStoragePoolFile) DirectoryOrDefault(name string) string {
if l.Directory == "" {
return filepath.Join("/var/lib/linstor-pools", name)
}
return l.Directory
}
func (l *LinstorStoragePoolZfs) Validate(oldSP *LinstorStoragePool, fieldPrefix *field.Path, name string, thin bool) field.ErrorList {
var result field.ErrorList
if oldSP != nil {
if thin && oldSP.ZfsThinPool == nil {
result = append(result, field.Forbidden(fieldPrefix, "Cannot change storage pool type"))
} else if !thin && oldSP.ZfsPool == nil {
result = append(result, field.Forbidden(fieldPrefix, "Cannot change storage pool type"))
}
}
return result
}
func (s *LinstorStoragePoolSource) Validate(oldSP *LinstorStoragePool, knownDevices sets.Set[string], fieldPrefix *field.Path) field.ErrorList {
if s == nil {
return nil
}
if oldSP != nil {
if !reflect.DeepEqual(s, oldSP.Source) {
return field.ErrorList{
field.Forbidden(fieldPrefix, "Cannot change source"),
}
}
}
var result field.ErrorList
if s.HostDevices != nil {
for j, src := range s.HostDevices {
if !strings.HasPrefix(src, "/dev/") {
result = append(result, field.Invalid(
fieldPrefix.Child("hostDevices", strconv.Itoa(j)),
src,
"Path not rooted in /dev",
))
}
if path.Clean(src) != src {
result = append(result, field.Invalid(
fieldPrefix.Child("hostDevices", strconv.Itoa(j)),
src,
"Not an absolute device path",
))
}
if knownDevices.Has(src) {
result = append(result, field.Duplicate(
fieldPrefix.Child("hostDevices", strconv.Itoa(j)),
src,
))
}
knownDevices.Insert(src)
}
} else {
result = append(result, field.Required(
fieldPrefix,
"Must specify exactly 1 type of storage pool source",
))
}
return result
}