-
Notifications
You must be signed in to change notification settings - Fork 23
/
node.go
285 lines (238 loc) · 8.92 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
/*
Copyright 2019 Tuxera Oy. All Rights Reserved.
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.
*/
package driver
import (
"context"
"path/filepath"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
diskIDPath = "/dev/disk/by-id"
diskDOPrefix = "scsi-0DO_Volume_"
)
// 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 *CSIDriver) 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.VolumeContext["endpoint"] == "" {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Endpoint 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")
}
source := req.VolumeContext["endpoint"]
target := req.StagingTargetPath
mnt := req.VolumeCapability.GetMount()
options := mnt.MountFlags
fsType := "moosefs"
/* if mnt.FsType != "" {
fsType = mnt.FsType
}
*/
ll := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"endpoint": req.VolumeContext["endpoint"],
"staging_target_path": req.StagingTargetPath,
"source": source,
"target": target,
"fsType": fsType,
"mount_options": options,
"method": "node_stage_volume",
})
ll.Info("mounting the volume for staging")
mounted, err := d.mounter.IsMounted(source, target)
if err != nil {
return nil, err
}
if !mounted {
if err := d.mounter.Mount(source, target, fsType, 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 *CSIDriver) 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")
}
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")
source := getDiskSource(req.VolumeId)
mounted, err := d.mounter.IsMounted(source, req.StagingTargetPath)
if err != nil {
return nil, err
}
if mounted {
ll.Info("unmounting the staging target path")
err := d.mounter.UMount(req.StagingTargetPath)
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 *CSIDriver) 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.VolumeContext["endpoint"] == "" {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Endpoint 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
mnt := req.VolumeCapability.GetMount()
options := mnt.MountFlags
// 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 := "moosefs"
/* if mnt.FsType != "" {
fsType = mnt.FsType
}
*/
ll := d.log.WithFields(logrus.Fields{
"volume_id": req.VolumeId,
"endpoint": req.VolumeContext["endpoint"],
"source": source,
"target": target,
"fsType": fsType,
"mount_options": options,
"method": "node_publish_volume",
})
// we can only check if target is mounted with the diskSource directly.
// The staging target path (which is a directory itself) won't work in this case
mounted, err := d.mounter.IsMounted(req.VolumeContext["endpoint"], target)
if err != nil {
return nil, err
}
if !mounted {
ll.Info("mounting the volume")
if err := d.mounter.Mount(source, target, fsType, 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 *CSIDriver) 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")
}
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.VolumeId, req.TargetPath)
if err != nil {
return nil, err
}
if mounted {
ll.Info("unmounting the target path")
err := d.mounter.UMount(req.TargetPath)
if err != nil {
return nil, err
}
} else {
ll.Info("target path is already unmounted")
}
ll.Info("unmounting volume is finished")
return &csi.NodeUnpublishVolumeResponse{}, nil
}
// NodeGetId returns the unique id of the node. 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 *CSIDriver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
d.log.WithField("method", "node_get_info").Info("node get info called")
return &csi.NodeGetInfoResponse{
NodeId: d.nodeID,
AccessibleTopology: &csi.Topology{
Segments: map[string]string{
"region": d.awsRegion,
},
},
}, nil
}
// NodeGetCapabilities returns the supported capabilities of the node server
func (d *CSIDriver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
// currently there is a single NodeServer capability according to the spec
nscap := &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
}
d.log.WithFields(logrus.Fields{
"node_capabilities": nscap,
"method": "node_get_capabilities",
}).Info("node get capabilities called")
return &csi.NodeGetCapabilitiesResponse{
Capabilities: []*csi.NodeServiceCapability{
nscap,
},
}, nil
}
// NodeGetVolumeStats impl
func (d *CSIDriver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return &csi.NodeGetVolumeStatsResponse{}, nil
}
// getDiskSource returns the absolute path of the attached volume for the given
// DO volume name
func getDiskSource(volumeName string) string {
return filepath.Join(diskIDPath, diskDOPrefix+volumeName)
}