-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeserver.go
355 lines (299 loc) · 11.8 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
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
package driver
import (
"fmt"
"os"
"code.cloudfoundry.org/lager"
"github.com/concourse/baggageclaim"
"github.com/golang/glog"
"golang.org/x/net/context"
"k8s.io/mount-utils"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const TopologyKeyNode = "topology.baggageclaim.csi/node"
type nodeServer struct {
nodeID string
maxVolumesPerNode int64
logger lager.Logger
bagClient baggageclaim.Client
mounter mount.Interface
}
func NewNodeServer(nodeId string, maxVolumesPerNode int64, client baggageclaim.Client, mounter mount.Interface, logger lager.Logger) *nodeServer {
return &nodeServer{
nodeID: nodeId,
maxVolumesPerNode: maxVolumesPerNode,
logger: logger,
bagClient: client,
mounter: mounter,
}
}
func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
//logger := ns.logger.Session("NodePublishVolume", lager.Data{"VolumeId": req.VolumeId})
// Check arguments
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")
}
volumeContext := req.GetVolumeContext()
var strategy baggageclaim.Strategy
glog.V(4).Infof("concourse: volumetContext: %s", volumeContext)
if id, ok := volumeContext["sourceVolumeID"]; ok {
glog.V(4).Info("concourse: using COWStrategy, source volume id found: " + id)
srcVolume, _, err := ns.bagClient.LookupVolume(ns.logger, id)
if err != nil {
return nil, err
}
strategy = baggageclaim.COWStrategy{Parent: srcVolume}
} else {
glog.V(4).Info("concourse: using EmptyStrategy")
strategy = baggageclaim.EmptyStrategy{}
}
volume, err := ns.bagClient.CreateVolume(ns.logger, req.VolumeId, baggageclaim.VolumeSpec{
Strategy: strategy,
Properties: map[string]string{},
})
if err != nil {
return nil, err
}
targetPath := req.GetTargetPath()
notMnt, err := mount.New("").IsLikelyNotMountPoint(targetPath)
if err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(targetPath, 0750); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
notMnt = true
} else {
return nil, status.Error(codes.Internal, err.Error())
}
}
if !notMnt {
return &csi.NodePublishVolumeResponse{}, nil
}
readOnly := req.GetReadonly()
options := []string{"bind"}
if readOnly {
options = append(options, "ro")
}
mounter := mount.New("")
path := volume.Path()
glog.V(4).Infof("concourse: mounting baggageclaim volume at %s", path)
if err := mounter.Mount(path, targetPath, "", options); err != nil {
return nil, err
}
// targetPath := req.GetTargetPath()
// ephemeralVolume := req.GetVolumeContext()["csi.storage.k8s.io/ephemeral"] == "true" ||
// req.GetVolumeContext()["csi.storage.k8s.io/ephemeral"] == "" && ns.ephemeral // Kubernetes 1.15 doesn't have csi.storage.k8s.io/ephemeral.
// if req.GetVolumeCapability().GetBlock() != nil &&
// req.GetVolumeCapability().GetMount() != nil {
// return nil, status.Error(codes.InvalidArgument, "cannot have both block and mount access type")
// }
// // if ephemeral is specified, create volume here to avoid errors
// if ephemeralVolume {
// volID := req.GetVolumeId()
// volName := fmt.Sprintf("ephemeral-%s", volID)
// vol, err := createHostpathVolume(req.GetVolumeId(), volName, maxStorageCapacity, mountAccess, ephemeralVolume)
// if err != nil && !os.IsExist(err) {
// glog.Error("ephemeral mode failed to create volume: ", err)
// return nil, status.Error(codes.Internal, err.Error())
// }
// glog.V(4).Infof("ephemeral mode: created volume: %s", vol.VolPath)
// }
// vol, err := getVolumeByID(req.GetVolumeId())
// if err != nil {
// return nil, status.Error(codes.NotFound, err.Error())
// }
// if req.GetVolumeCapability().GetBlock() != nil {
// if vol.VolAccessType != blockAccess {
// return nil, status.Error(codes.InvalidArgument, "cannot publish a non-block volume as block volume")
// }
// volPathHandler := volumepathhandler.VolumePathHandler{}
// // Get loop device from the volume path.
// loopDevice, err := volPathHandler.GetLoopDevice(vol.VolPath)
// if err != nil {
// return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get the loop device: %v", err))
// }
// mounter := mount.New("")
// // Check if the target path exists. Create if not present.
// _, err = os.Lstat(targetPath)
// if os.IsNotExist(err) {
// if err = mounter.MakeFile(targetPath); err != nil {
// return nil, status.Error(codes.Internal, fmt.Sprintf("failed to create target path: %s: %v", targetPath, err))
// }
// }
// if err != nil {
// return nil, status.Errorf(codes.Internal, "failed to check if the target block file exists: %v", err)
// }
// // Check if the target path is already mounted. Prevent remounting.
// notMount, err := mounter.IsNotMountPoint(targetPath)
// if err != nil {
// if !os.IsNotExist(err) {
// return nil, status.Errorf(codes.Internal, "error checking path %s for mount: %s", targetPath, err)
// }
// notMount = true
// }
// if !notMount {
// // It's already mounted.
// glog.V(5).Infof("Skipping bind-mounting subpath %s: already mounted", targetPath)
// return &csi.NodePublishVolumeResponse{}, nil
// }
// options := []string{"bind"}
// if err := mount.New("").Mount(loopDevice, targetPath, "", options); err != nil {
// return nil, status.Error(codes.Internal, fmt.Sprintf("failed to mount block device: %s at %s: %v", loopDevice, targetPath, err))
// }
// } else if req.GetVolumeCapability().GetMount() != nil {
// if vol.VolAccessType != mountAccess {
// return nil, status.Error(codes.InvalidArgument, "cannot publish a non-mount volume as mount volume")
// }
// notMnt, err := mount.New("").IsNotMountPoint(targetPath)
// if err != nil {
// if os.IsNotExist(err) {
// if err = os.MkdirAll(targetPath, 0750); err != nil {
// return nil, status.Error(codes.Internal, err.Error())
// }
// notMnt = true
// } else {
// return nil, status.Error(codes.Internal, err.Error())
// }
// }
// if !notMnt {
// return &csi.NodePublishVolumeResponse{}, nil
// }
// fsType := req.GetVolumeCapability().GetMount().GetFsType()
// deviceId := ""
// if req.GetPublishContext() != nil {
// deviceId = req.GetPublishContext()[deviceID]
// }
// readOnly := req.GetReadonly()
// volumeId := req.GetVolumeId()
// attrib := req.GetVolumeContext()
// mountFlags := req.GetVolumeCapability().GetMount().GetMountFlags()
// glog.V(4).Infof("target %v\nfstype %v\ndevice %v\nreadonly %v\nvolumeId %v\nattributes %v\nmountflags %v\n",
// targetPath, fsType, deviceId, readOnly, volumeId, attrib, mountFlags)
// options := []string{"bind"}
// if readOnly {
// options = append(options, "ro")
// }
// mounter := mount.New("")
// path := getVolumePath(volumeId)
// if err := mounter.Mount(path, targetPath, "", options); err != nil {
// var errList strings.Builder
// errList.WriteString(err.Error())
// if vol.Ephemeral {
// if rmErr := os.RemoveAll(path); rmErr != nil && !os.IsNotExist(rmErr) {
// errList.WriteString(fmt.Sprintf(" :%s", rmErr.Error()))
// }
// }
// return nil, status.Error(codes.Internal, fmt.Sprintf("failed to mount device: %s at %s: %s", path, targetPath, errList.String()))
// }
// }
return &csi.NodePublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
glog.V(4).Infof("concourse: Calling NodeUnpublishVolume: %v", req)
// Check arguments
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()
vol, _, err := ns.bagClient.LookupVolume(ns.logger, volumeID)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}
if vol != nil {
err := ns.bagClient.DestroyVolume(ns.logger, volumeID)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("concourse: failed to delete volume: %s", err))
}
}
// Unmount only if the target path is really a mount point.
if notMnt, err := mount.IsNotMountPoint(mount.New(""), targetPath); err != nil {
if !os.IsNotExist(err) {
return nil, status.Error(codes.Internal, err.Error())
}
} else if !notMnt {
// Unmounting the image or filesystem.
err = mount.New("").Unmount(targetPath)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
// Delete the mount point.
// Does not return error for non-existent path, repeated calls OK for idempotency.
if err = os.RemoveAll(targetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
glog.V(4).Infof("concourse: volume %s has been unpublished.", targetPath)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ns *nodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
if req.GetVolumeCapability() == nil {
return nil, status.Error(codes.InvalidArgument, "Volume Capability missing in request")
}
var strategy baggageclaim.Strategy
if parentId, ok := req.VolumeContext["parentVolumeId"]; ok {
parentVol, found, err := ns.bagClient.LookupVolume(ns.logger, parentId)
if !found || err != nil {
return nil, status.Error(codes.Internal,
fmt.Sprintf("Could not find parent volume to clone: %v", err))
}
strategy = baggageclaim.COWStrategy{
Parent: parentVol,
}
} else {
strategy = baggageclaim.EmptyStrategy{}
}
_, err := ns.bagClient.CreateVolume(ns.logger, req.VolumeId, baggageclaim.VolumeSpec{Strategy: strategy})
if err != nil {
return nil, status.Error(codes.Internal,
fmt.Sprintf("Failed to create volume in baggageclaim: %v", err))
}
return &csi.NodeStageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
// Check arguments
if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID missing in request")
}
ns.bagClient.DestroyVolume(ns.logger, req.VolumeId)
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ns *nodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
return &csi.NodeGetInfoResponse{
NodeId: ns.nodeID,
MaxVolumesPerNode: ns.maxVolumesPerNode,
}, nil
}
func (ns *nodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
return &csi.NodeGetCapabilitiesResponse{
Capabilities: []*csi.NodeServiceCapability{
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
},
},
},
},
}, nil
}
func (ns *nodeServer) NodeGetVolumeStats(ctx context.Context, in *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}
func (ns *nodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}