-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathstoragepool.go
More file actions
302 lines (258 loc) · 9.03 KB
/
storagepool.go
File metadata and controls
302 lines (258 loc) · 9.03 KB
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
package v1
import (
"fmt"
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
lclient "github.com/LINBIT/golinstor/client"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
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 ""
}
func (p *LinstorStoragePool) PvCreateArguments() []string {
switch {
case p.LvmPool != nil:
return p.LvmPool.PhysicalVolumeCreateArguments
case p.LvmThinPool != nil:
return p.LvmThinPool.PhysicalVolumeCreateArguments
default:
return nil
}
}
func (p *LinstorStoragePool) VgCreateArguments() []string {
switch {
case p.LvmPool != nil:
return p.LvmPool.VolumeGroupCreateArguments
case p.LvmThinPool != nil:
return p.LvmThinPool.VolumeGroupCreateArguments
default:
return nil
}
}
func (p *LinstorStoragePool) LvCreateArguments() []string {
switch {
case p.LvmThinPool != nil:
return p.LvmThinPool.LogicalVolumeCreateArguments
default:
return nil
}
}
func (p *LinstorStoragePool) ZpoolCreateArguments() []string {
switch {
case p.ZfsPool != nil:
if len(p.ZfsPool.ZpoolCreateArguments) == 0 {
return []string{"-o", "failmode=continue"}
}
return p.ZfsPool.ZpoolCreateArguments
case p.ZfsThinPool != nil:
if len(p.ZfsThinPool.ZpoolCreateArguments) == 0 {
return []string{"-o", "failmode=continue"}
}
return p.ZfsThinPool.ZpoolCreateArguments
default:
return nil
}
}
type LinstorStoragePoolLvm struct {
// VolumeGroup is the name of the Volume Group (VG) to use.
VolumeGroup string `json:"volumeGroup,omitempty"`
// PhysicalVolumeCreateArguments are arguments to pass to "pvcreate".
// This has no effect on an existing VG, it only applies the VG initially gets created.
// +kubebuilder:validation:Optional
PhysicalVolumeCreateArguments []string `json:"physicalVolumeCreateArguments,omitempty"`
// VolumeGroupCreateArguments are arguments to pass to "vgcreate".
// This has no effect on an existing VG, it only applies the VG initially gets created.
// +kubebuilder:validation:Optional
VolumeGroupCreateArguments []string `json:"volumeGroupCreateArguments,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"`
// PhysicalVolumeCreateArguments are arguments to pass to "pvcreate".
// This has no effect on an existing VG, it only applies the VG initially gets created.
// +kubebuilder:validation:Optional
PhysicalVolumeCreateArguments []string `json:"physicalVolumeCreateArguments,omitempty"`
// VolumeGroupCreateArguments are arguments to pass to "vgcreate".
// This has no effect on an existing VG, it only applies the VG initially gets created.
// +kubebuilder:validation:Optional
VolumeGroupCreateArguments []string `json:"volumeGroupCreateArguments,omitempty"`
// LogicalVolumeCreateArguments are arguments to pass to "lvcreate".
// This has no effect on an existing thinpool, it only applies the thinpool initially gets created.
// +kubebuilder:validation:Optional
LogicalVolumeCreateArguments []string `json:"logicalVolumeCreateArguments,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"`
// ZpoolCreateArguments are arguments to pass to "zpool create".
// This has no effect on an existing zpool, it only applies the zpool initially gets created.
//
// If not set, "-o failmode=continue" is automatically added to ensure a failed zpool does not stop replication
// to and from other nodes in the cluster.
//
// +kubebuilder:validation:Optional
ZpoolCreateArguments []string `json:"zpoolCreateArguments,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"`
}
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) (admission.Warnings, field.ErrorList) {
var result field.ErrorList
var warnings admission.Warnings
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"))
}
}
if oldSP != nil && thin && oldSP.ZfsThinPool != nil && !slices.Equal(l.ZpoolCreateArguments, oldSP.ZfsThinPool.ZpoolCreateArguments) {
warnings = append(warnings, fmt.Sprintf("%s: Update will only apply to new nodes", fieldPrefix.Child("zpoolCreateArguments")))
}
if oldSP != nil && !thin && oldSP.ZfsPool != nil && !slices.Equal(l.ZpoolCreateArguments, oldSP.ZfsPool.ZpoolCreateArguments) {
warnings = append(warnings, fmt.Sprintf("%s: Update will only apply to new nodes", fieldPrefix.Child("zpoolCreateArguments")))
}
return warnings, 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
}