/
iscsi.go
427 lines (344 loc) · 13.3 KB
/
iscsi.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Package iscsi combines LINSTOR operations and the CRM operations to create highly available iSCSI targets.
package iscsi
import (
"context"
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"path/filepath"
"sort"
"time"
"github.com/LINBIT/golinstor/client"
log "github.com/sirupsen/logrus"
"github.com/LINBIT/linstor-gateway/pkg/common"
"github.com/LINBIT/linstor-gateway/pkg/linstorcontrol"
"github.com/LINBIT/linstor-gateway/pkg/reactor"
)
const (
IDFormat = "iscsi-%s"
FilenameFormat = "linstor-gateway-iscsi-%s.toml"
)
type ISCSI struct {
cli *linstorcontrol.Linstor
}
func New(controllers []string) (*ISCSI, error) {
cli, err := linstorcontrol.Default(controllers)
if err != nil {
return nil, fmt.Errorf("failed to create linstor client: %w", err)
}
return &ISCSI{cli}, nil
}
func (i *ISCSI) Get(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to check for existing config: %w", err)
}
if cfg == nil {
return nil, nil
}
resourceDefinition, resourceGroup, volumeDefinitions, resources, err := cfg.DeployedResources(ctx, i.cli.Client)
if err != nil {
return nil, fmt.Errorf("failed to fetch existing deployment: %w", err)
}
deployedCfg, err := FromPromoter(cfg, resourceDefinition, volumeDefinitions)
if err != nil {
return nil, fmt.Errorf("unknown existing reactor config: %w", err)
}
deployedCfg.Status = linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, resources)
return deployedCfg, nil
}
// getExistingDeployment returns the ResourceConfig for an existing reactor.PromoterConfig.
// If the corresponding LINSTOR resource does not exist, it returns nil (but also a nil error).
// If the LINSTOR resource does exist but is invalid, it returns an error.
func (i *ISCSI) getExistingDeployment(ctx context.Context, rsc *ResourceConfig, cfg *reactor.PromoterConfig, path string) (*ResourceConfig, error) {
resourceDefinition, resourceGroup, volumeDefinitions, resources, err := cfg.DeployedResources(ctx, i.cli.Client)
if err != nil {
log.Warnf("Found an existing promoter config but no corresponding LINSTOR resource. Maybe left over from a previous deployment?")
log.Warnf("Ignoring and overwriting the existing configuration at %s.", path)
return nil, nil
}
deployedCfg, err := FromPromoter(cfg, resourceDefinition, volumeDefinitions)
if err != nil {
return nil, fmt.Errorf("unknown existing reactor config: %w", err)
}
if !rsc.Matches(deployedCfg) {
log.Debugf("existing resource found that does not match config")
log.Debugf("diff: %s", cmp.Diff(deployedCfg, rsc))
return nil, errors.New("resource already exists with incompatible config")
}
deployedCfg.Status = linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, resources)
return deployedCfg, nil
}
// Create creates an iSCSI target according to the resource configuration
// described in rsc. It automatically prepends a "cluster private volume" to the
// list of volumes, so volume numbers must start at 1.
func (i *ISCSI) Create(ctx context.Context, rsc *ResourceConfig) (*ResourceConfig, error) {
rsc.FillDefaults()
// prepend cluster private volume; it should always be the first volume and have number 0
rsc.Volumes = append([]common.VolumeConfig{common.ClusterPrivateVolume()}, rsc.Volumes...)
err := rsc.Valid()
if err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
configs, paths, err := reactor.ListConfigs(ctx, i.cli.Client)
if err != nil {
return nil, fmt.Errorf("failed to retrieve existing configs: %w", err)
}
for j := range configs {
c := configs[j]
p := paths[j]
for _, ip := range rsc.ServiceIPs {
if err := common.CheckIPCollision(c, ip.IP()); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
}
// while looking for ip collisions, filter out any existing config with
// the same name as the one we are trying to create.
if configName, _ := c.FirstResource(); configName == rsc.IQN.WWN() {
deployedCfg, err := i.getExistingDeployment(ctx, rsc, &c, p)
if err != nil {
return nil, err
}
if deployedCfg != nil {
return deployedCfg, nil
}
}
}
resourceDefinition, resourceGroup, deployment, err := i.cli.EnsureResource(ctx, linstorcontrol.Resource{
Name: rsc.IQN.WWN(),
ResourceGroup: rsc.ResourceGroup,
Volumes: rsc.Volumes,
GrossSize: rsc.GrossSize,
}, false)
if err != nil {
return nil, fmt.Errorf("failed to create linstor resource: %w", err)
}
defer func() {
// if we fail beyond this point, roll back by deleting the created resource definition
if err != nil {
log.Debugf("Rollback: deleting just created resource definition %s", rsc.IQN.WWN())
err := i.cli.ResourceDefinitions.Delete(ctx, rsc.IQN.WWN())
if err != nil {
log.Warnf("Failed to roll back created resource definition: %v", err)
}
}
}()
cfg, err := rsc.ToPromoter(deployment)
if err != nil {
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}
err = reactor.EnsureConfig(ctx, i.cli.Client, cfg, rsc.ID())
if err != nil {
return nil, fmt.Errorf("failed to register reactor config file: %w", err)
}
defer func() {
// if we fail beyond this point, delete the just created reactor config
if err != nil {
log.Debugf("Rollback: deleting just created reactor config %s", rsc.ID())
if err := reactor.DeleteConfig(ctx, i.cli.Client, rsc.ID()); err != nil {
log.Warnf("Failed to roll back created reactor config: %v", err)
}
if err := common.WaitUntilResourceCondition(ctx, i.cli.Client, rsc.IQN.WWN(), common.NoResourcesInUse); err != nil {
log.Warnf("Failed to wait for resource to become unused: %v", err)
}
}
}()
_, err = i.Start(ctx, rsc.IQN)
if err != nil {
return nil, fmt.Errorf("failed to start resources: %w", err)
}
path := reactor.ConfigPath(rsc.ID())
rsc.Status = linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, deployment)
return rsc, nil
}
func (i *ISCSI) Start(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to find the resource configuration: %w", err)
}
if cfg == nil {
return nil, nil
}
err = reactor.AttachConfig(ctx, i.cli.Client, cfg, path)
if err != nil {
return nil, fmt.Errorf("failed to detach reactor configuration: %w", err)
}
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
err = common.WaitUntilResourceCondition(waitCtx, i.cli.Client, iqn.WWN(), common.AnyResourcesInUse)
if err != nil {
return nil, fmt.Errorf("error waiting for resource to become used: %w", err)
}
err = common.AssertResourceInUseStable(waitCtx, i.cli.Client, iqn.WWN())
if err != nil {
return nil, fmt.Errorf("error waiting for resource to become stable: %w", err)
}
return i.Get(ctx, iqn)
}
func (i *ISCSI) Stop(ctx context.Context, iqn Iqn) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to find the resource configuration: %w", err)
}
if cfg == nil {
return nil, nil
}
err = reactor.DetachConfig(ctx, i.cli.Client, cfg, path)
if err != nil {
return nil, fmt.Errorf("failed to detach reactor configuration: %w", err)
}
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
err = common.WaitUntilResourceCondition(waitCtx, i.cli.Client, iqn.WWN(), common.NoResourcesInUse)
if err != nil {
return nil, fmt.Errorf("error waiting for resource to become unused: %w", err)
}
return i.Get(ctx, iqn)
}
func (i *ISCSI) List(ctx context.Context) ([]*ResourceConfig, error) {
cfgs, paths, err := reactor.ListConfigs(ctx, i.cli.Client)
if err != nil {
return nil, err
}
result := make([]*ResourceConfig, 0, len(cfgs))
for j := range cfgs {
cfg := &cfgs[j]
path := paths[j]
filename := filepath.Base(path)
var rsc string
num, _ := fmt.Sscanf(filename, FilenameFormat, &rsc)
if num == 0 {
log.WithField("filename", filename).Trace("not an iSCSI resource config, skipping")
continue
}
resourceDefinition, resourceGroup, volumeDefinitions, resources, err := cfg.DeployedResources(ctx, i.cli.Client)
if err != nil {
log.WithError(err).Warn("failed to fetch deployed resources")
}
parsed, err := FromPromoter(cfg, resourceDefinition, volumeDefinitions)
if err != nil {
log.WithError(err).Warn("skipping error while parsing promoter config")
continue
}
parsed.Status = linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, resources)
result = append(result, parsed)
}
return result, nil
}
func (i *ISCSI) Delete(ctx context.Context, iqn Iqn) error {
err := reactor.DeleteConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return fmt.Errorf("failed to delete reactor config: %w", err)
}
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
err = common.WaitUntilResourceCondition(waitCtx, i.cli.Client, iqn.WWN(), common.NoResourcesInUse)
if err != nil {
return fmt.Errorf("error waiting for resource to become unused: %w", err)
}
err = i.cli.ResourceDefinitions.Delete(ctx, iqn.WWN())
if err != nil && err != client.NotFoundError {
return fmt.Errorf("failed to delete resources: %w", err)
}
return nil
}
func (i *ISCSI) AddVolume(ctx context.Context, iqn Iqn, volCfg *common.VolumeConfig) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to check for existing config: %w", err)
}
if cfg == nil {
return nil, nil
}
resourceDefinition, resourceGroup, volumeDefinitions, resources, err := cfg.DeployedResources(ctx, i.cli.Client)
if err != nil {
return nil, fmt.Errorf("failed to fetch existing deployment: %w", err)
}
deployedCfg, err := FromPromoter(cfg, resourceDefinition, volumeDefinitions)
if err != nil {
return nil, fmt.Errorf("unknown existing reactor config: %w", err)
}
exists := false
for i := range deployedCfg.Volumes {
if deployedCfg.Volumes[i].Number == volCfg.Number {
if deployedCfg.Volumes[i].SizeKiB != volCfg.SizeKiB {
return nil, errors.New(fmt.Sprintf("existing volume has differing size %d != %d", deployedCfg.Volumes[i].SizeKiB, volCfg.SizeKiB))
}
exists = true
break
}
}
if !exists {
deployedCfg.Volumes = append(deployedCfg.Volumes, *volCfg)
sort.Slice(deployedCfg.Volumes, func(i, j int) bool {
return deployedCfg.Volumes[i].Number < deployedCfg.Volumes[j].Number
})
err = deployedCfg.Valid()
if err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
resourceDefinition, resourceGroup, resources, err = i.cli.EnsureResource(ctx, linstorcontrol.Resource{
Name: deployedCfg.IQN.WWN(),
ResourceGroup: deployedCfg.ResourceGroup,
Volumes: deployedCfg.Volumes,
GrossSize: deployedCfg.GrossSize,
}, true)
if err != nil {
return nil, fmt.Errorf("failed to reconcile linstor resource: %w", err)
}
}
cfg, err = deployedCfg.ToPromoter(resources)
if err != nil {
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}
err = reactor.EnsureConfig(ctx, i.cli.Client, cfg, deployedCfg.ID())
if err != nil {
return nil, fmt.Errorf("failed to update config: %w", err)
}
deployedCfg.Status = linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, resources)
return deployedCfg, nil
}
func (i *ISCSI) DeleteVolume(ctx context.Context, iqn Iqn, lun int) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, i.cli.Client, fmt.Sprintf(IDFormat, iqn.WWN()))
if err != nil {
return nil, fmt.Errorf("failed to delete reactor config: %w", err)
}
if cfg == nil {
return nil, nil
}
resourceDefinition, resourceGroup, volumeDefinition, resources, err := cfg.DeployedResources(ctx, i.cli.Client)
if err != nil {
return nil, fmt.Errorf("failed to fetch deployed resources: %w", err)
}
rscCfg, err := FromPromoter(cfg, resourceDefinition, volumeDefinition)
if err != nil {
return nil, fmt.Errorf("failed to convert volume definition to resource: %w", err)
}
status := linstorcontrol.StatusFromResources(path, resourceDefinition, resourceGroup, resources)
if status.Service == common.ServiceStateStarted {
return nil, errors.New("cannot delete volume while service is running")
}
for j := range rscCfg.Volumes {
if rscCfg.Volumes[j].Number == lun {
err = i.cli.ResourceDefinitions.DeleteVolumeDefinition(ctx, iqn.WWN(), lun)
if err != nil && err != client.NotFoundError {
return nil, fmt.Errorf("failed to delete volume definition: %w", err)
}
rscCfg.Volumes = append(rscCfg.Volumes[:j], rscCfg.Volumes[j+1:]...)
// Manually delete the resources from the current resource config
for k := range resources {
resources[k].Volumes = append(resources[k].Volumes[:j], resources[k].Volumes[j+1:]...)
}
cfg, err = rscCfg.ToPromoter(resources)
if err != nil {
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}
err = reactor.EnsureConfig(ctx, i.cli.Client, cfg, rscCfg.ID())
if err != nil {
return nil, fmt.Errorf("failed to update config")
}
break
}
}
return i.Get(ctx, iqn)
}