forked from libopenstorage/openstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
496 lines (430 loc) · 14.9 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
Package csi is CSI driver interface for OSD
Copyright 2017 Portworx
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 csi
import (
"fmt"
"os"
"strings"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/pkg/grpcutil"
"github.com/portworx/kvdb"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
ephemeralDenyList = []string{
api.SpecPriorityAlias,
api.SpecPriority,
api.SpecSticky,
api.SpecScale,
}
)
func (s *OsdCsiServer) NodeGetInfo(
ctx context.Context,
req *csi.NodeGetInfoRequest,
) (*csi.NodeGetInfoResponse, error) {
clus, err := s.cluster.Enumerate()
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to Enumerate cluster: %s", err)
}
node, err := s.cluster.Inspect(clus.NodeId)
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to Inspect node %s: %s", clus.NodeId, err)
}
topology := &csi.Topology{}
if node.SchedulerTopology != nil && len(node.SchedulerTopology.Labels) > 0 {
topology.Segments = make(map[string]string)
for k, v := range node.SchedulerTopology.Labels {
topology.Segments[k] = v
}
}
return &csi.NodeGetInfoResponse{
NodeId: clus.NodeId,
AccessibleTopology: topology,
}, nil
}
// NodePublishVolume is a CSI API call which mounts the volume on the specified
// target path on the node.
//
// TODO: Support READ ONLY Mounts
func (s *OsdCsiServer) NodePublishVolume(
ctx context.Context,
req *csi.NodePublishVolumeRequest,
) (*csi.NodePublishVolumeResponse, error) {
volumeId := req.GetVolumeId()
targetPath := req.GetTargetPath()
clogger.WithContext(ctx).Infof("csi.NodePublishVolume request received. VolumeID: %s, TargetPath: %s", volumeId, targetPath)
// Check arguments
if len(volumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume id must be provided")
}
if len(targetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path must be provided")
}
if req.GetVolumeCapability() == nil || req.GetVolumeCapability().GetAccessMode() == nil ||
req.GetVolumeCapability().GetAccessMode().Mode == csi.VolumeCapability_AccessMode_UNKNOWN {
return nil, status.Error(codes.InvalidArgument, "Volume access mode must be provided")
}
// Ensure target location is created correctly
isBlockAccessType := false
if req.GetVolumeCapability().GetBlock() != nil {
isBlockAccessType = true
}
if err := ensureMountPathCreated(ctx, targetPath, isBlockAccessType); err != nil {
return nil, status.Errorf(
codes.Aborted,
"Failed to use target location %s: %s",
targetPath,
err.Error())
}
// Get grpc connection
conn, err := s.getConn()
if err != nil {
return nil, status.Errorf(
codes.Unavailable,
"Unable to connect to SDK server: %v", err)
}
// Get secret if any was passed
ctx = s.setupContext(ctx, req.GetSecrets())
ctx, cancel := grpcutil.WithDefaultTimeout(ctx)
defer cancel()
// Check if block device
driverType := s.driver.Type()
if driverType != api.DriverType_DRIVER_TYPE_BLOCK &&
req.GetVolumeCapability().GetBlock() != nil {
return nil, status.Errorf(codes.InvalidArgument, "Trying to attach as block a non block device")
}
// Gather volume attributes
spec, locator, _, err := s.specHandler.SpecFromOpts(req.GetVolumeContext())
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"Invalid volume attributes: %#v",
req.GetVolumeContext())
}
// Get volume encryption info from req.Secrets
driverOpts := s.addEncryptionInfoToLabels(make(map[string]string), req.GetSecrets())
// Parse storage class 'mountOptions' flags from CSI req
// flags from 'mountOptions' will be used as the only source of truth for Pure volumes upon mounting
if req.GetVolumeCapability() != nil && req.GetVolumeCapability().GetMount() != nil {
mountFlags := strings.Join(req.GetVolumeCapability().GetMount().GetMountFlags(), ",")
if mountFlags != "" {
driverOpts[api.SpecCSIMountOptions] = mountFlags
}
}
// can use either spec.Ephemeral or VolumeContext label
if req.GetVolumeContext()["csi.storage.k8s.io/ephemeral"] == "true" || spec.Ephemeral {
if !s.allowInlineVolumes {
return nil, status.Error(codes.InvalidArgument, "CSI ephemeral inline volumes are disabled on this cluster")
}
if err := validateEphemeralVolumeAttributes(req.GetVolumeContext()); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
spec.Ephemeral = true
volumes := api.NewOpenStorageVolumeClient(conn)
resp, err := volumes.Create(ctx, &api.SdkVolumeCreateRequest{
Name: volumeId,
Spec: spec,
Labels: locator.GetVolumeLabels(),
})
if err != nil {
return nil, err
}
volumeId = resp.VolumeId
}
// prepare for mount/attaching
opts := &api.SdkVolumeAttachOptions{
SecretName: spec.GetPassphrase(),
}
mounts := api.NewOpenStorageMountAttachClient(conn)
if driverType == api.DriverType_DRIVER_TYPE_BLOCK {
// attach is assumed to be idempotent
// attach is assumed to return the same DevicePath on each call
if _, err = mounts.Attach(ctx, &api.SdkVolumeAttachRequest{
VolumeId: volumeId,
Options: opts,
DriverOptions: driverOpts,
}); err != nil {
if spec.Ephemeral {
clogger.WithContext(ctx).Errorf("Failed to attach ephemeral volume %s: %v", volumeId, err.Error())
s.cleanupEphemeral(ctx, conn, volumeId, false)
}
return nil, adjustFinalErrors(err)
}
}
// for volumes with mount access type just mount volume onto the path
if _, err := mounts.Mount(ctx, &api.SdkVolumeMountRequest{
VolumeId: volumeId,
MountPath: targetPath,
Options: opts,
DriverOptions: driverOpts,
}); err != nil {
if spec.Ephemeral {
clogger.WithContext(ctx).Errorf("Failed to mount ephemeral volume %s: %v", volumeId, err.Error())
s.cleanupEphemeral(ctx, conn, volumeId, true)
}
return nil, adjustFinalErrors(err)
}
clogger.WithContext(ctx).Infof("CSI Volume %s mounted on %s",
volumeId,
req.GetTargetPath())
return &csi.NodePublishVolumeResponse{}, nil
}
// NodeUnpublishVolume is a CSI API call which unmounts the volume.
func (s *OsdCsiServer) NodeUnpublishVolume(
ctx context.Context,
req *csi.NodeUnpublishVolumeRequest,
) (*csi.NodeUnpublishVolumeResponse, error) {
volumeId := req.GetVolumeId()
targetPath := req.GetTargetPath()
clogger.WithContext(ctx).Infof("csi.NodeUnpublishVolume request received. VolumeID: %s, TargetPath: %s", volumeId, targetPath)
// Check arguments
if len(volumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume id must be provided")
}
if len(targetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path must be provided")
}
// Get volume information
vols, err := s.driver.Enumerate(&api.VolumeLocator{VolumeIds: []string{req.GetVolumeId()}}, nil)
if err != nil || len(vols) < 1 {
if err == kvdb.ErrNotFound {
clogger.WithContext(ctx).Tracef("Volume %s was deleted or cannot be found: %s", req.GetVolumeId(), err.Error())
return &csi.NodeUnpublishVolumeResponse{}, nil
} else if err != nil {
return nil, status.Errorf(codes.NotFound, "Volume id %s not found: %s",
req.GetVolumeId(),
err.Error())
} else {
clogger.WithContext(ctx).Tracef("Volume %s was deleted or cannot be found", req.GetVolumeId())
return &csi.NodeUnpublishVolumeResponse{}, nil
}
}
if err = s.driver.Unmount(ctx, req.GetVolumeId(), req.GetTargetPath(), nil); err != nil {
clogger.WithContext(ctx).Infof("unable to unmount volume %s onto %s: %s",
req.GetVolumeId(),
req.GetTargetPath(),
err.Error(),
)
}
if s.driver.Type() == api.DriverType_DRIVER_TYPE_BLOCK {
if err = s.driver.Detach(ctx, volumeId, nil); err != nil {
return nil, status.Errorf(
codes.Canceled,
"Unable to detach volume: %s",
err.Error())
}
}
// Attempt to remove volume path
// Kubernetes handles this after NodeUnpublishVolume finishes, but this allows for cross-CO compatibility
if err := os.Remove(req.GetTargetPath()); err != nil && !os.IsNotExist(err) {
clogger.WithContext(ctx).Tracef("Failed to delete mount path %s: %s", targetPath, err.Error())
}
// Return error to Kubelet if mount path still exists to force a retry
if _, err := os.Stat(targetPath); !os.IsNotExist(err) {
return nil, status.Errorf(
codes.Canceled,
"Mount path still exists: %s",
targetPath)
}
clogger.WithContext(ctx).Infof("CSI Volume %s unmounted from path %s", volumeId, targetPath)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
// NodeGetCapabilities is a CSI API function which seems to be setup for
// future patches
func (s *OsdCsiServer) NodeGetCapabilities(
ctx context.Context,
req *csi.NodeGetCapabilitiesRequest,
) (*csi.NodeGetCapabilitiesResponse, error) {
clogger.WithContext(ctx).Debugf("csi.NodeGetCapabilities request received")
caps := []csi.NodeServiceCapability_RPC_Type{
// Getting volume stats for volume health monitoring
csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
// Indicates that the Node service can report volume conditions.
csi.NodeServiceCapability_RPC_VOLUME_CONDITION,
}
var serviceCapabilities []*csi.NodeServiceCapability
for _, cap := range caps {
serviceCapabilities = append(serviceCapabilities, &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: cap,
},
},
})
}
return &csi.NodeGetCapabilitiesResponse{
Capabilities: serviceCapabilities,
}, nil
}
func getVolumeCondition(vol *api.Volume) *csi.VolumeCondition {
condition := &csi.VolumeCondition{}
if vol.Status != api.VolumeStatus_VOLUME_STATUS_UP {
condition.Abnormal = true
}
switch vol.Status {
case api.VolumeStatus_VOLUME_STATUS_UP:
condition.Message = "Volume status is up"
case api.VolumeStatus_VOLUME_STATUS_NOT_PRESENT:
condition.Message = "Volume status is not present"
case api.VolumeStatus_VOLUME_STATUS_DOWN:
condition.Message = "Volume status is down"
case api.VolumeStatus_VOLUME_STATUS_DEGRADED:
condition.Message = "Volume status is degraded"
default:
condition.Message = "Volume status is unknown"
}
return condition
}
// NodeGetVolumeStats get volume stats for a given node.
// This function skips auth and directly hits the driver as it is read-only
// and only exposed via the CSI unix domain socket. If a secrets field is added
// in csi.NodeGetVolumeStatsRequest, we can update this to hit the SDK and use auth.
func (s *OsdCsiServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
clogger.WithContext(ctx).Debugf("NodeGetVolumeStats request received. VolumeID: %s, VolumePath: %s", req.GetVolumeId(), req.GetVolumePath())
// Check arguments
id := req.GetVolumeId()
if len(id) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume id must be provided")
}
path := req.GetVolumePath()
if len(path) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume path must be provided")
}
// Driver inspect as NodeGetVolumeStatsRequest does not support secrets
vol, err := s.driverGetVolume(ctx, req.GetVolumeId())
if err != nil {
return nil, err
}
var attachPathMatch bool
sharedPath := fmt.Sprintf("%s/%s", api.SharedVolExportPrefix, id)
for _, attachPath := range vol.AttachPath {
if attachPath == path || attachPath == sharedPath {
attachPathMatch = true
}
}
if !attachPathMatch {
return nil, status.Errorf(codes.NotFound, "Volume %s not mounted on path %s", id, path)
}
// Define volume usage
total := int64(vol.Spec.Size)
used := int64(vol.Usage)
usage := &csi.VolumeUsage{
Available: total - used,
Total: total,
Used: used,
Unit: csi.VolumeUsage_BYTES,
}
// Define volume condition
return &csi.NodeGetVolumeStatsResponse{
Usage: []*csi.VolumeUsage{
usage,
},
VolumeCondition: getVolumeCondition(vol),
}, nil
}
// cleanupEphemeral detaches and deletes an ephemeral volume if either attach or mount fails
func (s *OsdCsiServer) cleanupEphemeral(ctx context.Context, conn *grpc.ClientConn, volumeId string, detach bool) {
if detach {
mounts := api.NewOpenStorageMountAttachClient(conn)
if _, err := mounts.Detach(ctx, &api.SdkVolumeDetachRequest{
VolumeId: volumeId,
}); err != nil {
clogger.WithContext(ctx).Errorf("Failed to detach ephemeral volume %s during cleanup: %v", volumeId, err.Error())
return
}
}
volumes := api.NewOpenStorageVolumeClient(conn)
if _, err := volumes.Delete(ctx, &api.SdkVolumeDeleteRequest{
VolumeId: volumeId,
}); err != nil {
clogger.WithContext(ctx).Errorf("Failed to delete ephemeral volume %s during cleanup: %v", volumeId, err.Error())
}
}
func ensureMountPathCreated(ctx context.Context, targetPath string, isBlock bool) error {
// Check if targetpath exists
fileInfo, err := os.Lstat(targetPath)
if err != nil && os.IsNotExist(err) {
// Create if does not exist
// 1. Block - create targetPath file
// 2. Mount - create targetpath directory
if isBlock {
if err = makeFile(ctx, targetPath); err != nil {
return err
}
} else {
if err = makeDir(targetPath); err != nil {
return err
}
}
return nil
} else if err != nil {
return fmt.Errorf(
"unknown error while verifying target location %s: %s",
targetPath,
err.Error())
}
// Check for directory or file.
// 1. Block - should be file
// 2. Mount - should be directory
if isBlock {
if fileInfo.IsDir() {
return fmt.Errorf("Target location %s is not a file", targetPath)
}
} else {
if !fileInfo.IsDir() {
return fmt.Errorf("Target location %s is not a directory", targetPath)
}
}
return nil
}
func validateEphemeralVolumeAttributes(volumeAttributes map[string]string) error {
for attr := range volumeAttributes {
for _, deny := range ephemeralDenyList {
if attr == deny {
return fmt.Errorf("invalid ephemeral volume attribute provided. "+
"Volume attributes %v are not allowed for ephemeral volumes", ephemeralDenyList)
}
}
}
return nil
}
func makeFile(ctx context.Context, pathname string) error {
f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644))
defer func() {
err := f.Close()
if err != nil {
clogger.WithContext(ctx).Warnf("failed to close file: %s", err.Error())
}
}()
if err != nil {
if !os.IsExist(err) {
return fmt.Errorf("failed to create block file: %s", err.Error())
}
}
return nil
}
func makeDir(targetPath string) error {
err := os.MkdirAll(targetPath, 0750)
if err != nil {
return fmt.Errorf(
"failed to create target path %s: %s",
targetPath,
err.Error())
}
return nil
}