-
Notifications
You must be signed in to change notification settings - Fork 11
/
node.go
472 lines (393 loc) · 14.8 KB
/
node.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Copyright cloudscale.ch
Copyright 2018 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by protoc-gen-go. DO NOT EDIT.
// NOTE: THIS IS NOT GENERATED. We have to add the line above to prevent golint
// checking this file. This is needed because some methods end with xxxId, but
// golint wants them to be xxxID. But we're not able to change it as the
// official CSI spec is that way and we have to implement the interface
// exactly.
package driver
import (
"context"
"os"
"strconv"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/kubernetes/pkg/util/resizefs"
"k8s.io/mount-utils"
utilexec "k8s.io/utils/exec"
)
const (
// Current technical limit is 128
// - 1 for root
// - 1 for /var/lib/docker
// - 1 additional volume outside of CSI
defaultMaxVolumesPerNode = 125
)
// NodeStageVolume mounts the volume to a staging path on the node. This is
// called by the CO before NodePublishVolume and is used to temporary mount the
// volume to a staging path. Once mounted, NodePublishVolume will make sure to
// mount it to the appropriate path
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
d.log.Info("node stage volume called")
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Volume ID must be provided")
}
if req.StagingTargetPath == "" {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Staging Target Path must be provided")
}
if req.VolumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Volume Capability must be provided")
}
// Apparently sometimes we need to call udevadm trigger to get the volume
// properly registered in /dev/disk. More information can be found here:
// https://github.com/cloudscale-ch/csi-cloudscale/issues/9
sourcePtr, err := d.mounter.FinalizeVolumeAttachmentAndFindPath(d.log.WithFields(logrus.Fields{"volume_id": req.VolumeId}), req.VolumeId)
if err != nil {
return nil, err
}
source := *sourcePtr
publishContext := req.GetPublishContext()
if publishContext == nil {
return nil, status.Error(codes.InvalidArgument, "PublishContext must be provided")
}
volumeName, ok := publishContext[PublishInfoVolumeName]
if !ok {
return nil, status.Error(codes.InvalidArgument, "Could not find the volume by name")
}
luksContext := getLuksContext(req.Secrets, publishContext, VolumeLifecycleNodeStageVolume)
target := req.StagingTargetPath
mnt := req.VolumeCapability.GetMount()
options := mnt.MountFlags
fsType := "ext4"
if mnt.FsType != "" {
fsType = mnt.FsType
}
ll := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"volume_name": volumeName,
"volume_context": req.VolumeContext,
"publish_context": req.PublishContext,
"staging_target_path": req.StagingTargetPath,
"source": source,
"fsType": fsType,
"mount_options": options,
"method": "node_stage_volume",
"luks_encrypted": luksContext.EncryptionEnabled,
})
formatted, err := d.mounter.IsFormatted(source, luksContext)
if err != nil {
return nil, err
}
if !formatted {
ll.Info("formatting the volume for staging")
if err := d.mounter.Format(source, fsType, luksContext); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
} else {
ll.Info("source device is already formatted")
}
ll.Info("mounting the volume for staging")
mounted, err := d.mounter.IsMounted(target)
if err != nil {
return nil, err
}
if !mounted {
if err := d.mounter.Mount(source, target, fsType, luksContext, options...); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
} else {
ll.Info("source device is already mounted to the target path")
}
ll.Info("formatting and mounting stage volume is finished")
return &csi.NodeStageVolumeResponse{}, nil
}
// NodeUnstageVolume unstages the volume from the staging path
func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "NodeUnstageVolume Volume ID must be provided")
}
if req.StagingTargetPath == "" {
return nil, status.Error(codes.InvalidArgument, "NodeUnstageVolume Staging Target Path must be provided")
}
luksContext := LuksContext{VolumeLifecycle: VolumeLifecycleNodeUnstageVolume}
ll := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"staging_target_path": req.StagingTargetPath,
"method": "node_unstage_volume",
})
ll.Info("node unstage volume called")
mounted, err := d.mounter.IsMounted(req.StagingTargetPath)
if err != nil {
return nil, err
}
if mounted {
ll.Info("unmounting the staging target path")
err := d.mounter.Unmount(req.StagingTargetPath, luksContext)
if err != nil {
return nil, err
}
} else {
ll.Info("staging target path is already unmounted")
}
ll.Info("unmounting stage volume is finished")
return &csi.NodeUnstageVolumeResponse{}, nil
}
// NodePublishVolume mounts the volume mounted to the staging path to the target path
func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
d.log.Info("node publish volume called")
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Volume ID must be provided")
}
if req.StagingTargetPath == "" {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Staging Target Path must be provided")
}
if req.TargetPath == "" {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Target Path must be provided")
}
if req.VolumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Volume Capability must be provided")
}
publishContext := req.GetPublishContext()
if publishContext == nil {
return nil, status.Error(codes.InvalidArgument, "PublishContext must be provided")
}
luksContext := getLuksContext(req.Secrets, publishContext, VolumeLifecycleNodePublishVolume)
source := req.StagingTargetPath
target := req.TargetPath
mnt := req.VolumeCapability.GetMount()
options := mnt.MountFlags
// TODO(arslan): do we need bind here? check it out
// Perform a bind mount to the full path to allow duplicate mounts of the same PD.
options = append(options, "bind")
if req.Readonly {
options = append(options, "ro")
}
fsType := "ext4"
if mnt.FsType != "" {
fsType = mnt.FsType
}
ll := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"source": source,
"target": target,
"fsType": fsType,
"mount_options": options,
"method": "node_publish_volume",
"luks_encrypted": luksContext.EncryptionEnabled,
})
mounted, err := d.mounter.IsMounted(target)
if err != nil {
return nil, err
}
if !mounted {
ll.Info("mounting the volume")
if err := d.mounter.Mount(source, target, fsType, luksContext, options...); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
} else {
ll.Info("volume is already mounted")
}
ll.Info("bind mounting the volume is finished")
return &csi.NodePublishVolumeResponse{}, nil
}
// NodeUnpublishVolume unmounts the volume from the target path
func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "NodeUnpublishVolume Volume ID must be provided")
}
if req.TargetPath == "" {
return nil, status.Error(codes.InvalidArgument, "NodeUnpublishVolume Target Path must be provided")
}
luksContext := LuksContext{VolumeLifecycle: VolumeLifecycleNodeUnpublishVolume}
ll := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"target_path": req.TargetPath,
"method": "node_unpublish_volume",
})
ll.Info("node unpublish volume called")
mounted, err := d.mounter.IsMounted(req.TargetPath)
if err != nil {
return nil, err
}
if mounted {
ll.Info("unmounting the target path")
err := d.mounter.Unmount(req.TargetPath, luksContext)
if err != nil {
return nil, err
}
} else {
ll.Info("target path is already unmounted")
}
ll.Info("unmounting volume is finished")
return &csi.NodeUnpublishVolumeResponse{}, nil
}
// NodeGetCapabilities returns the supported capabilities of the node server
func (d *Driver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
nscaps := []*csi.NodeServiceCapability{
&csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
},
&csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_EXPAND_VOLUME,
},
},
},
&csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
},
},
},
}
d.log.WithFields(logrus.Fields{
"node_capabilities": nscaps,
"method": "node_get_capabilities",
}).Info("node get capabilities called")
return &csi.NodeGetCapabilitiesResponse{
Capabilities: nscaps,
}, nil
}
func getEnvAsInt(key string, fallback int64) int64 {
if valueStr, ok := os.LookupEnv(key); ok {
if value, err := strconv.ParseInt(valueStr, 10, 64); err == nil {
return value
}
}
return fallback
}
// NodeGetInfo returns the supported capabilities of the node server. This
// should eventually return the droplet ID if possible. This is used so the CO
// knows where to place the workload. The result of this function will be used
// by the CO in ControllerPublishVolume.
func (d *Driver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
d.log.WithField("method", "node_get_info").Info("node get info called")
maxVolumesPerNode := getEnvAsInt("CLOUDSCALE_MAX_CSI_VOLUMES_PER_NODE", defaultMaxVolumesPerNode)
return &csi.NodeGetInfoResponse{
NodeId: d.serverId,
MaxVolumesPerNode: maxVolumesPerNode,
// make sure that the driver works on this particular region only
AccessibleTopology: &csi.Topology{
Segments: map[string]string{
"region": d.region,
},
},
}, nil
}
// NodeGetVolumeStats returns the volume capacity statistics available for the
// the given volume.
func (d *Driver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
ll := d.log.WithField("method", "node_get_volume_stats")
ll.Info("node get volume stats called")
if req.VolumeId == "" {
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats Volume ID must be provided")
}
volumePath := req.VolumePath
if volumePath == "" {
return nil, status.Error(codes.InvalidArgument, "NodeGetVolumeStats Volume Path must be provided")
}
mounted, err := d.mounter.IsMounted(volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to check if volume path %q is mounted: %s", volumePath, err)
}
if !mounted {
return nil, status.Errorf(codes.NotFound, "volume path %q is not mounted", volumePath)
}
stats, err := d.mounter.GetStatistics(volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to retrieve capacity statistics for volume path %q: %s", volumePath, err)
}
ll.WithFields(logrus.Fields{
"bytes_available": stats.availableBytes,
"bytes_total": stats.totalBytes,
"bytes_used": stats.usedBytes,
"inodes_available": stats.availableInodes,
"inodes_total": stats.totalInodes,
"inodes_used": stats.usedInodes,
}).Info("node capacity statistics retrieved")
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
&csi.VolumeUsage{
Available: stats.availableBytes,
Total: stats.totalBytes,
Used: stats.usedBytes,
Unit: csi.VolumeUsage_BYTES,
},
&csi.VolumeUsage{
Available: stats.availableInodes,
Total: stats.totalInodes,
Used: stats.usedInodes,
Unit: csi.VolumeUsage_INODES,
},
},
}, nil
}
func (d *Driver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
volumeID := req.VolumeId
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeExpandVolume volume ID not provided")
}
volumePath := req.VolumePath
if len(volumePath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeExpandVolume volume path not provided")
}
log := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"volume_path": req.VolumePath,
"method": "node_expand_volume",
})
log.Info("node expand volume called")
// return here once we support block volumes and the current volume is a block volume
mounter := mount.New("")
devicePath, _, err := mount.GetDeviceNameFromMount(mounter, volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "NodeExpandVolume unable to get device path for %q: %v", volumePath, err)
}
isLuks, _, err := isLuksMapping(devicePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "NodeExpandVolume unable to test if volume %q at %q is encrypted with luks: %v", volumePath, devicePath, err)
}
// the luks container must be resized if the volume was resized while the disk was mounted
if isLuks {
log.WithFields(logrus.Fields{
"device_path": devicePath,
}).Info("resizing luks container")
err := luksResize(devicePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "NodeExpandVolume unable resize luks container for volume %q at %q: %v", volumePath, devicePath, err)
}
}
r := resizefs.NewResizeFs(&mount.SafeFormatAndMount{
Interface: mounter,
Exec: utilexec.New(),
})
log = log.WithFields(logrus.Fields{
"device_path": devicePath,
})
log.Info("resizing volume")
if _, err := r.Resize(devicePath, volumePath); err != nil {
return nil, status.Errorf(codes.Internal, "NodeExpandVolume could not resize volume %q (%q): %v", volumeID, req.GetVolumePath(), err)
}
log.Info("volume was resized")
return &csi.NodeExpandVolumeResponse{}, nil
}