/
node.go
297 lines (257 loc) · 8.37 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
/*
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"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/pkg/options"
"github.com/libopenstorage/openstorage/pkg/util"
csi "github.com/libopenstorage/openstorage/csi/v0.3/spec"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
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)
}
result := &csi.NodeGetInfoResponse{
NodeId: clus.NodeId,
}
return result, nil
}
// NodeGetId is a CSI API which gets the PX NodeId for the local node
func (s *OsdCsiServer) NodeGetId(
ctx context.Context,
req *csi.NodeGetIdRequest,
) (*csi.NodeGetIdResponse, error) {
clus, err := s.cluster.Enumerate()
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to Enumerate cluster: %s", err)
}
result := &csi.NodeGetIdResponse{
NodeId: clus.NodeId,
}
logrus.Infof("NodeId is %s", result.NodeId)
return result, 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) {
logrus.Debugf("NodePublishVolume req[%#v]", req)
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume id must be provided")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path must be provided")
}
if req.GetVolumeCapability() == nil || req.GetVolumeCapability().GetAccessMode() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume access mode must be provided")
}
// Get volume information
v, err := util.VolumeFromName(s.driver, req.GetVolumeId())
if err != nil {
return nil, status.Errorf(codes.NotFound, "Volume id %s not found: %s",
req.GetVolumeId(),
err.Error())
}
if s.driver.Type() != 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, _, _, err := s.specHandler.SpecFromOpts(req.GetVolumeAttributes())
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"Invalid volume attributes: %#v",
req.GetVolumeAttributes())
}
// This seems weird as a way to change opts to map[string]string
opts := make(map[string]string)
if len(spec.GetPassphrase()) != 0 {
opts[options.OptionsSecret] = spec.GetPassphrase()
}
// If this is for a block driver, first attach the volume
var devicePath string
if s.driver.Type() == api.DriverType_DRIVER_TYPE_BLOCK {
if devicePath, err = s.driver.Attach(ctx, req.GetVolumeId(), opts); err != nil {
return nil, status.Errorf(
codes.Internal,
"Unable to attach volume: %s",
err.Error())
}
}
if req.GetVolumeCapability().GetBlock() != nil {
// As block create a sym link to the attached location
err = os.Symlink(devicePath, req.GetTargetPath())
if err != nil {
detachErr := s.driver.Detach(ctx, v.GetId(), opts)
if detachErr != nil {
logrus.Errorf("Unable to detach volume %s: %s",
v.GetId(),
detachErr.Error())
}
return nil, status.Errorf(
codes.Internal,
"Failed to create symlink %s -> %s: %v",
req.GetTargetPath(),
devicePath,
err)
}
} else {
// Verify target location is an existing directory
if err := verifyTargetLocation(req.GetTargetPath()); err != nil {
return nil, status.Errorf(
codes.Aborted,
"Failed to use target location %s: %s",
req.GetTargetPath(),
err.Error())
}
// Mount volume onto the path
if err := s.driver.Mount(ctx, req.GetVolumeId(), req.GetTargetPath(), nil); err != nil {
// Detach on error
detachErr := s.driver.Detach(ctx, v.GetId(), opts)
if detachErr != nil {
logrus.Errorf("Unable to detach volume %s: %s",
v.GetId(),
detachErr.Error())
}
return nil, status.Errorf(
codes.Internal,
"Unable to mount volume %s onto %s: %s",
req.GetVolumeId(),
req.GetTargetPath(),
err.Error())
}
}
logrus.Infof("Volume %s mounted on %s",
req.GetVolumeId(),
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) {
logrus.Debugf("NodeUnPublishVolume req[%#v]", req)
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume id must be provided")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path must be provided")
}
// Get volume information
_, err := util.VolumeFromName(s.driver, req.GetVolumeId())
if err != nil {
return nil, status.Errorf(codes.NotFound, "Volume id %s not found: %s",
req.GetVolumeId(),
err.Error())
}
// Get information about the target since the request does not
// tell us if it is for block or mount point.
// https://github.com/container-storage-interface/spec/issues/285
fileInfo, err := os.Lstat(req.GetTargetPath())
if err != nil && os.IsNotExist(err) {
// For idempotency, return that there is nothing to unmount
logrus.Infof("NodeUnpublishVolume on target path %s but it does "+
"not exist, returning there is nothing to do", req.GetTargetPath())
return &csi.NodeUnpublishVolumeResponse{}, nil
} else if err != nil {
return nil, status.Errorf(
codes.Internal,
"Unknown error while verifying target location %s: %s",
req.GetTargetPath(),
err.Error())
}
// Check if it is block or not
if fileInfo.Mode()&os.ModeSymlink != 0 {
// If block, we just need to remove the link.
os.Remove(req.GetTargetPath())
} else {
if !fileInfo.IsDir() {
return nil, status.Errorf(
codes.NotFound,
"Target location %s is not a directory", req.GetTargetPath())
}
// Mount volume onto the path
if err = s.driver.Unmount(ctx, req.GetVolumeId(), req.GetTargetPath(), nil); err != nil {
return nil, status.Errorf(
codes.Internal,
"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, req.GetVolumeId(), nil); err != nil {
return nil, status.Errorf(
codes.Internal,
"Unable to detach volume: %s",
err.Error())
}
}
logrus.Infof("Volume %s unmounted", req.GetVolumeId())
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) {
logrus.Debugf("NodeGetCapabilities req[%#v]", req)
return &csi.NodeGetCapabilitiesResponse{
Capabilities: []*csi.NodeServiceCapability{
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_UNKNOWN,
},
},
},
},
}, nil
}
func verifyTargetLocation(targetPath string) error {
fileInfo, err := os.Lstat(targetPath)
if err != nil && os.IsNotExist(err) {
return fmt.Errorf("Target location %s does not exist", targetPath)
} else if err != nil {
return fmt.Errorf(
"Unknown error while verifying target location %s: %s",
targetPath,
err.Error())
}
if !fileInfo.IsDir() {
return fmt.Errorf("Target location %s is not a directory", targetPath)
}
return nil
}