-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
368 lines (288 loc) · 10.4 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
package driver
import (
"context"
"fmt"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"os"
"path"
)
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
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")
}
log := d.logger.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"staging_target_path": req.StagingTargetPath,
"method": "node_stage_volume",
"hostname": d.hostname,
})
log.Info("node stage volume called")
source := path.Base(req.VolumeId)
target := req.StagingTargetPath
mnt := req.VolumeCapability.GetMount()
options := mnt.MountFlags
log = d.logger.WithFields(logrus.Fields{
"volume_context": req.VolumeContext,
"publish_context": req.PublishContext,
"source": source,
"target": target,
"vol_id": req.VolumeId,
"mount_options": options,
})
deviceName, err := d.provider.Attach(ctx, req.VolumeId)
if err != nil {
return nil, err
}
log.Infof("start import ZPool, device: %s", deviceName)
err = d.zfsUtil.ImportZPool(source)
if err != nil {
if err == NoSuchPoolErr {
log.Infof("start create ZPool, source: %s", source)
err = d.zfsUtil.CreateZPool(source, []string{deviceName})
if err != nil {
errMsg := fmt.Sprintf("Create zpool failed, %v", err)
log.Error(errMsg)
return nil, status.Errorf(codes.Internal, errMsg)
}
} else {
errMsg := fmt.Sprintf("Import zpool failed, %v", err)
log.Error(errMsg)
return nil, status.Errorf(codes.Internal, errMsg)
}
}
log.Info("mounting the volume for staging")
zfsDirectory := path.Join(source, "globalmount")
err = d.zfsUtil.CreateZFS(zfsDirectory)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
err = d.zfsUtil.Mount(path.Join("/", zfsDirectory), target, []string{"bind"}...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
log.Info("formatting and mounting stage volume is finished")
return &csi.NodeStageVolumeResponse{}, nil
}
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")
}
log := d.logger.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"staging_target_path": req.StagingTargetPath,
"method": "node_unstage_volume",
})
log.Info("node unstage volume called")
log.Info("start umount staging_target_path")
err := d.zfsUtil.Unmount(req.StagingTargetPath)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
log.Info("start export ZPool")
source := path.Base(req.VolumeId)
err = d.zfsUtil.ExportZPool(source)
if err != nil && err != NoSuchPoolErr {
return nil, status.Errorf(codes.Internal, "Export zpool failed: %v", err)
}
err = d.provider.Detach(ctx, req.VolumeId)
if err != nil {
return nil, err
}
log.Info("export ZPool is finished")
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
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")
}
source := req.StagingTargetPath
target := req.TargetPath
log := d.logger.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"staging_target_path": source,
"target_path": target,
"method": "node_publish_volume",
"fstype": req.VolumeCapability.GetMount().FsType,
})
log.Info("node publish volume called")
err := d.zfsUtil.Mount(source, target, []string{"bind"}...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
log.Infof("bind mounting the volume is finished")
return &csi.NodePublishVolumeResponse{}, nil
}
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")
}
log := d.logger.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"target_path": req.TargetPath,
"method": "node_unpublish_volume",
})
log.Info("node unpublish volume called")
err := d.zfsUtil.Unmount(req.TargetPath)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
log.Info("unmounting volume is finished")
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (d *Driver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
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")
}
log := d.logger.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"volume_path": volumePath,
"method": "node_get_volume_stats",
})
log.Info("node get volume stats called")
found, err := d.provider.HasVolume(req.VolumeId)
if err != nil {
return nil, err
}
if !found {
return nil, status.Errorf(codes.NotFound, "volume %q does not exist", req.VolumeId)
}
stats, err := d.zfsUtil.GetStatistics(volumePath)
if err != nil {
if os.IsNotExist(err) {
return nil, status.Errorf(codes.NotFound,
"volume path %q not found, err: %s", volumePath, err.Error())
}
return nil, status.Errorf(codes.Internal,
"failed to retrieve capacity statistics for volume path %q: %s", volumePath, err)
}
log.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.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeExpandVolume volume ID not provided")
}
volumePath := req.GetVolumePath()
if len(volumePath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeExpandVolume volume path not provided")
}
log := d.logger.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"volume_path": req.VolumePath,
"method": "node_expand_volume",
})
log.Info("node expand volume called")
resizeBytes, err := extractStorage(req.GetCapacityRange())
if err != nil {
return nil, status.Errorf(codes.OutOfRange, "ControllerExpandVolume invalid capacity range: %v", err)
}
log.Infof("resize to %d", resizeBytes)
err = d.provider.Resize(req.VolumeId, resizeBytes/miB)
if err != nil {
return nil, status.Errorf(codes.Internal, "Ceph resize %s failed, err: %s", req.VolumeId, err.Error())
}
deviceName, err := d.provider.GetMapDevice(req.VolumeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "Get map device for %s failed, err: %s",
req.VolumeId, err.Error())
}
err = d.zfsUtil.Resize(req.VolumeId, deviceName)
if err != nil {
return nil, status.Errorf(codes.Internal, "Zpool Resize %s failed, err: %s", req.VolumeId, err.Error())
}
log.Info("volume was resized")
return &csi.NodeExpandVolumeResponse{}, nil
}
func (d *Driver) NodeGetCapabilities(context.Context, *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.logger.WithFields(logrus.Fields{
"node_capabilities": nscaps,
"method": "node_get_capabilities",
}).Info("node get capabilities called")
return &csi.NodeGetCapabilitiesResponse{
Capabilities: nscaps,
}, nil
}
func (d *Driver) NodeGetInfo(context.Context, *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
d.logger.WithField("method", "node_get_info").Info("node get info called")
return &csi.NodeGetInfoResponse{
NodeId: d.hostname,
MaxVolumesPerNode: d.maxVolumesPerNode,
AccessibleTopology: &csi.Topology{
Segments: map[string]string{},
},
}, nil
}