-
Notifications
You must be signed in to change notification settings - Fork 6
/
nfs.go
391 lines (319 loc) · 12.2 KB
/
nfs.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
package nfs
import (
"context"
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"path/filepath"
"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 = "nfs-%s"
FilenameFormat = "linstor-gateway-nfs-%s.toml"
)
type NFS struct {
cli *linstorcontrol.Linstor
}
func New(controllers []string) (*NFS, error) {
cli, err := linstorcontrol.Default(controllers)
if err != nil {
return nil, fmt.Errorf("failed to create linstor client: %w", err)
}
return &NFS{cli}, nil
}
func (n *NFS) Get(ctx context.Context, name string) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, n.cli.Client, fmt.Sprintf(IDFormat, name))
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, n.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 (n *NFS) getExistingDeployment(ctx context.Context, rsc *ResourceConfig, cfg *reactor.PromoterConfig, path string) (*ResourceConfig, error) {
resourceDefinition, resourceGroup, volumeDefinitions, resources, err := cfg.DeployedResources(ctx, n.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
}
func (n *NFS) isNFSConfig(config reactor.PromoterConfig) bool {
for _, r := range config.Resources {
for _, s := range r.Start {
if agent, ok := s.(*reactor.ResourceAgent); ok {
if agent.Type == "ocf:heartbeat:nfsserver" {
return true
}
}
}
}
return false
}
// checkExistingConfigs goes through the list of currently deployed configs, and checks:
// - if an NFS config already exists; if so, abort with an error
// - if an existing config has the same name as the one we are trying to create; if so, just return that
// - if the IP address of the new config collides with an existing config; if so, abort with an error
func (n *NFS) checkExistingConfigs(ctx context.Context, newRsc *ResourceConfig) (*reactor.PromoterConfig, string, error) {
configs, paths, err := reactor.ListConfigs(ctx, n.cli.Client)
if err != nil {
return nil, "", fmt.Errorf("failed to check for existing NFS configs: %w", err)
}
var existingConfig *reactor.PromoterConfig
var existingPath string
for i := range configs {
c := configs[i]
p := paths[i]
// filter out any existing config with the same name as the one we are trying to create.
if name, _ := c.FirstResource(); name == newRsc.Name {
existingConfig = &c
existingPath = p
continue
}
if n.isNFSConfig(c) {
return nil, "", fmt.Errorf("an NFS config already exists in %s. Only one NFS resource is allowed", p)
}
if err := common.CheckIPCollision(c, newRsc.ServiceIP.IP()); err != nil {
return nil, "", fmt.Errorf("invalid configuration: %w", err)
}
}
return existingConfig, existingPath, nil
}
// Create creates an NFS export 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 (n *NFS) 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([]VolumeConfig{{VolumeConfig: common.ClusterPrivateVolume()}}, rsc.Volumes...)
err := rsc.Valid()
if err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
existingConfig, existingPath, err := n.checkExistingConfigs(ctx, rsc)
if err != nil {
return nil, err
}
if existingConfig != nil {
deployedCfg, err := n.getExistingDeployment(ctx, rsc, existingConfig, existingPath)
if err != nil {
return nil, err
}
if deployedCfg != nil {
return deployedCfg, nil
}
}
volumes := make([]common.VolumeConfig, len(rsc.Volumes))
for i := range rsc.Volumes {
volumes[i] = rsc.Volumes[i].VolumeConfig
}
resourceDefinition, resourceGroup, deployment, err := n.cli.EnsureResource(ctx, linstorcontrol.Resource{
Name: rsc.Name,
ResourceGroup: rsc.ResourceGroup,
Volumes: 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.Name)
err := n.cli.ResourceDefinitions.Delete(ctx, rsc.Name)
if err != nil {
log.Warnf("Failed to roll back created resource definition: %v", err)
}
}
}()
existingConfig, err = rsc.ToPromoter(deployment)
if err != nil {
return nil, fmt.Errorf("failed to convert resource to promoter configuration: %w", err)
}
err = reactor.EnsureConfig(ctx, n.cli.Client, existingConfig, 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, n.cli.Client, rsc.ID()); err != nil {
log.Warnf("Failed to roll back created reactor config: %v", err)
}
if err := common.WaitUntilResourceCondition(ctx, n.cli.Client, rsc.Name, common.NoResourcesInUse); err != nil {
log.Warnf("Failed to wait for resource to become unused: %v", err)
}
}
}()
_, err = n.Start(ctx, rsc.Name)
if err != nil {
return nil, fmt.Errorf("failed to start resources: %w", err)
}
rsc.Status = linstorcontrol.StatusFromResources(existingPath, resourceDefinition, resourceGroup, deployment)
return rsc, nil
}
func (n *NFS) Start(ctx context.Context, name string) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, n.cli.Client, fmt.Sprintf(IDFormat, name))
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, n.cli.Client, cfg, path)
if err != nil {
return nil, fmt.Errorf("failed to attach reactor configuration: %w", err)
}
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
err = common.WaitUntilResourceCondition(waitCtx, n.cli.Client, name, common.AnyResourcesInUse)
if err != nil {
return nil, fmt.Errorf("error waiting for resource to become used: %w", err)
}
err = common.AssertResourceInUseStable(waitCtx, n.cli.Client, name)
if err != nil {
return nil, fmt.Errorf("error waiting for resource to become stable: %w", err)
}
return n.Get(ctx, name)
}
func (n *NFS) Stop(ctx context.Context, name string) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, n.cli.Client, fmt.Sprintf(IDFormat, name))
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, n.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, n.cli.Client, name, common.NoResourcesInUse)
if err != nil {
return nil, fmt.Errorf("error waiting for resource to become unused: %w", err)
}
return n.Get(ctx, name)
}
func (n *NFS) List(ctx context.Context) ([]*ResourceConfig, error) {
cfgs, paths, err := reactor.ListConfigs(ctx, n.cli.Client)
if err != nil {
return nil, err
}
result := make([]*ResourceConfig, 0, len(cfgs))
for i := range cfgs {
cfg := &cfgs[i]
path := paths[i]
filename := filepath.Base(path)
var rsc string
num, _ := fmt.Sscanf(filename, FilenameFormat, &rsc)
if num == 0 {
log.WithField("filename", filename).Trace("not an nfs resource config, skipping")
continue
}
resourceDefinition, resourceGroup, volumeDefinitions, resources, err := cfg.DeployedResources(ctx, n.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 (n *NFS) Delete(ctx context.Context, name string) error {
err := reactor.DeleteConfig(ctx, n.cli.Client, fmt.Sprintf(IDFormat, name))
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, n.cli.Client, name, common.NoResourcesInUse)
if err != nil {
return fmt.Errorf("error waiting for resource to become unused: %w", err)
}
err = n.cli.ResourceDefinitions.Delete(ctx, name)
if err != nil && err != client.NotFoundError {
return fmt.Errorf("failed to delete resources: %w", err)
}
return nil
}
func (n *NFS) DeleteVolume(ctx context.Context, name string, lun int) (*ResourceConfig, error) {
cfg, path, err := reactor.FindConfig(ctx, n.cli.Client, fmt.Sprintf(IDFormat, name))
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, n.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 i := range rscCfg.Volumes {
if rscCfg.Volumes[i].Number == lun {
err = n.cli.ResourceDefinitions.DeleteVolumeDefinition(ctx, name, lun)
if err != nil && err != client.NotFoundError {
return nil, fmt.Errorf("failed to delete volume definition")
}
rscCfg.Volumes = append(rscCfg.Volumes[:i], rscCfg.Volumes[i+1:]...)
// Manually delete the resources from the current resource config
for j := range resources {
resources[j].Volumes = append(resources[j].Volumes[:i], resources[j].Volumes[i+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, n.cli.Client, cfg, rscCfg.ID())
if err != nil {
return nil, fmt.Errorf("failed to update config")
}
break
}
}
return n.Get(ctx, name)
}