-
Notifications
You must be signed in to change notification settings - Fork 135
/
nodeserver.go
330 lines (281 loc) · 11.3 KB
/
nodeserver.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
/*
Copyright 2017 The Kubernetes Authors.
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 azurefile
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/container-storage-interface/spec/lib/go/csi"
"k8s.io/klog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"golang.org/x/net/context"
"k8s.io/kubernetes/pkg/volume/util"
)
// NodePublishVolume mount the volume from staging to target path
func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
if req.GetVolumeCapability() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capability missing in request")
}
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path missing in request")
}
source := req.GetStagingTargetPath()
if len(source) == 0 {
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
target := req.GetTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
}
mountOptions := []string{"bind"}
if req.GetReadonly() {
mountOptions = append(mountOptions, "ro")
}
if err := d.ensureMountPoint(target); err != nil {
return nil, status.Errorf(codes.Internal, "Could not mount target %q: %v", target, err)
}
klog.V(2).Infof("NodePublishVolume: mounting %s at %s with mountOptions: %v", source, target, mountOptions)
if err := d.mounter.Mount(source, target, "", mountOptions); err != nil {
if removeErr := os.Remove(target); removeErr != nil {
return nil, status.Errorf(codes.Internal, "Could not remove mount target %q: %v", target, removeErr)
}
return nil, status.Errorf(codes.Internal, "Could not mount %q at %q: %v", source, target, err)
}
klog.V(2).Infof("NodePublishVolume: mount %s at %s successfully", source, target)
return &csi.NodePublishVolumeResponse{}, nil
}
// NodeUnpublishVolume unmount the volume from the target path
func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
klog.V(2).Infof("NodeUnPublishVolume: called with args %+v", *req)
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path missing in request")
}
targetPath := req.GetTargetPath()
volumeID := req.GetVolumeId()
klog.V(2).Infof("NodeUnpublishVolume: unmounting volume %s on %s", volumeID, targetPath)
err := d.mounter.Unmount(req.GetTargetPath())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
klog.V(2).Infof("NodeUnpublishVolume: unmount volume %s on %s successfully", volumeID, targetPath)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
// NodeStageVolume mount the volume to a staging path
func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
targetPath := req.GetStagingTargetPath()
if len(targetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
volumeCapability := req.GetVolumeCapability()
if volumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "Volume capability not provided")
}
notMnt, err := d.mounter.IsLikelyNotMountPoint(targetPath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if !notMnt {
// testing original mount point, make sure the mount link is valid
if _, err := ioutil.ReadDir(targetPath); err == nil {
klog.V(2).Infof("azureFile - already mounted to target %s", targetPath)
return &csi.NodeStageVolumeResponse{}, nil
}
// todo: mount link is invalid, now unmount and remount later (built-in functionality)
klog.Warningf("azureFile - ReadDir %s failed with %v, unmount this directory", targetPath, err)
if err := d.mounter.Unmount(targetPath); err != nil {
klog.Errorf("azureFile - Unmount directory %s failed with %v", targetPath, err)
return nil, err
}
// notMnt = true
}
fsType := req.GetVolumeCapability().GetMount().GetFsType()
volumeID := req.GetVolumeId()
attrib := req.GetVolumeContext()
mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags()
var accountName, accountKey, fileShareName string
secrets := req.GetSecrets()
if len(secrets) == 0 {
var resourceGroupName string
resourceGroupName, accountName, fileShareName, err = getFileShareInfo(volumeID)
if err != nil {
return nil, err
}
if resourceGroupName == "" {
resourceGroupName = d.cloud.ResourceGroup
}
accountKey, err = d.cloud.GetStorageAccesskey(accountName, resourceGroupName)
if err != nil {
return nil, fmt.Errorf("no key for storage account(%s) under resource group(%s), err %v", accountName, resourceGroupName, err)
}
} else {
for k, v := range attrib {
switch strings.ToLower(k) {
case "sharename":
fileShareName = v
}
}
if fileShareName == "" {
return nil, fmt.Errorf("could not find sharename from attributes(%v)", attrib)
}
accountName, accountKey, err = getStorageAccount(secrets)
if err != nil {
return nil, err
}
}
var mountOptions []string
source := ""
osSeparator := string(os.PathSeparator)
source = fmt.Sprintf("%s%s%s.file.%s%s%s", osSeparator, osSeparator, accountName, d.cloud.Environment.StorageEndpointSuffix, osSeparator, fileShareName)
if runtime.GOOS == "windows" {
mountOptions = []string{fmt.Sprintf("AZURE\\%s", accountName), accountKey}
} else {
if err := os.MkdirAll(targetPath, 0700); err != nil {
return nil, err
}
// parameters suggested by https://azure.microsoft.com/en-us/documentation/articles/storage-how-to-use-files-linux/
options := []string{fmt.Sprintf("username=%s,password=%s", accountName, accountKey)}
mountOptions = util.JoinMountOptions(mountFlags, options)
mountOptions = appendDefaultMountOptions(mountOptions)
}
klog.V(2).Infof("target %v\nfstype %v\nvolumeId %v\ncontext %v\nmountflags %v\nmountOptions %v\n",
targetPath, fsType, volumeID, attrib, mountFlags, mountOptions)
err = d.mounter.Mount(source, targetPath, "cifs", mountOptions)
if err != nil {
notMnt, mntErr := d.mounter.IsLikelyNotMountPoint(targetPath)
if mntErr != nil {
klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
return nil, err
}
if !notMnt {
if mntErr = d.mounter.Unmount(targetPath); mntErr != nil {
klog.Errorf("Failed to unmount: %v", mntErr)
return nil, err
}
notMnt, mntErr := d.mounter.IsLikelyNotMountPoint(targetPath)
if mntErr != nil {
klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
return nil, err
}
if !notMnt {
// This is very odd, we don't expect it. We'll try again next sync loop.
klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", targetPath)
return nil, err
}
}
os.Remove(targetPath)
return nil, err
}
return &csi.NodeStageVolumeResponse{}, nil
}
// NodeUnstageVolume unmount the volume from the staging path
func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
klog.V(2).Infof("NodeUnstageVolume: called with args %+v", *req)
volumeID := req.GetVolumeId()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID not provided")
}
target := req.GetStagingTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Staging target not provided")
}
klog.V(2).Infof("NodeUnstageVolume: unmounting %s", target)
err := d.mounter.Unmount(target)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not unmount target %q: %v", target, err)
}
klog.V(2).Infof("NodeUnstageVolume: unmount %s successfully", target)
return &csi.NodeUnstageVolumeResponse{}, nil
}
// NodeGetCapabilities return the capabilities of the Node plugin
func (d *Driver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
klog.V(2).Infof("Using default NodeGetCapabilities")
return &csi.NodeGetCapabilitiesResponse{
Capabilities: d.NSCap,
}, nil
}
// NodeGetInfo return info of the node on which this plugin is running
func (d *Driver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
klog.V(5).Infof("Using default NodeGetInfo")
return &csi.NodeGetInfoResponse{
NodeId: d.NodeID,
}, nil
}
// NodeGetVolumeStats get volume stats
func (d *Driver) NodeGetVolumeStats(ctx context.Context, in *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
// NodeExpandVolume node expand volume
func (d *Driver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
volumeID := req.GetVolumeId()
volumePath := req.GetVolumePath()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if len(volumePath) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume Path missing in request")
}
if err := d.ValidateNodeServiceRequest(csi.NodeServiceCapability_RPC_EXPAND_VOLUME); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid expand volume request: %v", req)
}
notMnt, err := d.mounter.IsLikelyNotMountPoint(volumePath)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to check volume path(%s): %v", volumePath, err)
}
if notMnt {
return nil, status.Errorf(codes.InvalidArgument, "the specified volume path(%s) is not a mount path", volumePath)
}
currentQuota, err := d.expandVolume(ctx, volumeID, req.GetCapacityRange().GetRequiredBytes())
if err != nil {
return nil, err
}
return &csi.NodeExpandVolumeResponse{CapacityBytes: currentQuota}, nil
}
// ensureMountPoint: create mount point if not exists
func (d *Driver) ensureMountPoint(target string) error {
notMnt, err := d.mounter.IsLikelyNotMountPoint(target)
if err != nil && !os.IsNotExist(err) {
return err
}
if !notMnt {
// testing original mount point, make sure the mount link is valid
_, err := ioutil.ReadDir(target)
if err == nil {
klog.V(2).Infof("already mounted to target %s", target)
return nil
}
// mount link is invalid, now unmount and remount later
klog.Warningf("ReadDir %s failed with %v, unmount this directory", target, err)
if err := d.mounter.Unmount(target); err != nil {
klog.Errorf("Unmount directory %s failed with %v", target, err)
return err
}
// notMnt = true
}
if err := d.mounter.MakeDir(target); err != nil {
klog.Errorf("MakeDir failed on target: %s (%v)", target, err)
return err
}
return nil
}