-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
304 lines (261 loc) · 11.3 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
/*
Copyright 2018 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 gceGCEDriver
import (
"fmt"
"os"
"sync"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/kubernetes/pkg/util/mount"
volumeutils "k8s.io/kubernetes/pkg/volume/util"
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
)
type GCENodeServer struct {
Driver *GCEDriver
Mounter *mount.SafeFormatAndMount
DeviceUtils mountmanager.DeviceUtils
MetadataService metadataservice.MetadataService
// TODO: Only lock mutually exclusive calls and make locking more fine grained
mux sync.Mutex
}
var _ csi.NodeServer = &GCENodeServer{}
func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
ns.mux.Lock()
defer ns.mux.Unlock()
glog.V(4).Infof("NodePublishVolume called with req: %#v", req)
// Validate Arguments
targetPath := req.GetTargetPath()
stagingTargetPath := req.GetStagingTargetPath()
readOnly := req.GetReadonly()
volumeID := req.GetVolumeId()
volumeCapability := req.GetVolumeCapability()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Volume ID must be provided")
}
if len(stagingTargetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Staging Target Path must be provided")
}
if len(targetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Target Path must be provided")
}
if volumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "NodePublishVolume Volume Capability must be provided")
}
notMnt, err := ns.Mounter.Interface.IsLikelyNotMountPoint(targetPath)
if err != nil && !os.IsNotExist(err) {
glog.Errorf("cannot validate mount point: %s %v", targetPath, err)
return nil, err
}
if !notMnt {
// TODO(#95): check if mount is compatible. Return OK if it is, or appropriate error.
/*
1) Target Path MUST be the vol referenced by vol ID
2) VolumeCapability MUST match
3) Readonly MUST match
*/
return &csi.NodePublishVolumeResponse{}, nil
}
if err := ns.Mounter.Interface.MakeDir(targetPath); err != nil {
glog.Errorf("mkdir failed on disk %s (%v)", targetPath, err)
return nil, err
}
// Perform a bind mount to the full path to allow duplicate mounts of the same PD.
options := []string{"bind"}
if readOnly {
options = append(options, "ro")
}
err = ns.Mounter.Interface.Mount(stagingTargetPath, targetPath, "ext4", options)
if err != nil {
notMnt, mntErr := ns.Mounter.Interface.IsLikelyNotMountPoint(targetPath)
if mntErr != nil {
glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
return nil, status.Error(codes.Internal, fmt.Sprintf("NodePublishVolume failed to check whether target path is a mount point: %v", err))
}
if !notMnt {
if mntErr = ns.Mounter.Interface.Unmount(targetPath); mntErr != nil {
glog.Errorf("Failed to unmount: %v", mntErr)
return nil, status.Error(codes.Internal, fmt.Sprintf("NodePublishVolume failed to unmount target path: %v", err))
}
notMnt, mntErr := ns.Mounter.Interface.IsLikelyNotMountPoint(targetPath)
if mntErr != nil {
glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
return nil, status.Error(codes.Internal, fmt.Sprintf("NodePublishVolume failed to check whether target path is a mount point: %v", err))
}
if !notMnt {
// This is very odd, we don't expect it. We'll try again next sync loop.
glog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", targetPath)
return nil, status.Error(codes.Internal, fmt.Sprintf("NodePublishVolume something is wrong with mounting: %v", err))
}
}
os.Remove(targetPath)
glog.Errorf("Mount of disk %s failed: %v", targetPath, err)
return nil, status.Error(codes.Internal, fmt.Sprintf("NodePublishVolume mount of disk failed: %v", err))
}
glog.V(4).Infof("Successfully mounted %s", targetPath)
return &csi.NodePublishVolumeResponse{}, nil
}
func (ns *GCENodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
ns.mux.Lock()
defer ns.mux.Unlock()
glog.V(4).Infof("NodeUnpublishVolume called with args: %v", req)
// Validate Arguments
targetPath := req.GetTargetPath()
volID := req.GetVolumeId()
if len(volID) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeUnpublishVolume Volume ID must be provided")
}
if len(targetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeUnpublishVolume Target Path must be provided")
}
err := volumeutils.UnmountMountPoint(targetPath, ns.Mounter.Interface, false /* bind mount */)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("Unmount failed: %v\nUnmounting arguments: %s\n", err, targetPath))
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
ns.mux.Lock()
defer ns.mux.Unlock()
glog.V(4).Infof("NodeStageVolume called with req: %#v", req)
// Validate Arguments
volumeID := req.GetVolumeId()
stagingTargetPath := req.GetStagingTargetPath()
volumeCapability := req.GetVolumeCapability()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Volume ID must be provided")
}
if len(stagingTargetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Staging Target Path must be provided")
}
if volumeCapability == nil {
return nil, status.Error(codes.InvalidArgument, "NodeStageVolume Volume Capability must be provided")
}
volumeKey, err := common.VolumeIDToKey(volumeID)
if err != nil {
return nil, err
}
// Part 1: Get device path of attached device
partition := ""
if part, ok := req.GetVolumeContext()[common.VolumeAttributePartition]; ok {
partition = part
}
deviceName, err := common.GetDeviceName(volumeKey)
if err != nil {
status.Error(codes.Internal, fmt.Sprintf("error getting device name: %v", err))
}
devicePaths := ns.DeviceUtils.GetDiskByIdPaths(deviceName, partition)
devicePath, err := ns.DeviceUtils.VerifyDevicePath(devicePaths)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("Error verifying GCE PD (%q) is attached: %v", volumeKey.Name, err))
}
if devicePath == "" {
return nil, status.Error(codes.Internal, fmt.Sprintf("Unable to find device path out of attempted paths: %v", devicePaths))
}
glog.V(4).Infof("Successfully found attached GCE PD %q at device path %s.", volumeKey.Name, devicePath)
// Part 2: Check if mount already exists at targetpath
notMnt, err := ns.Mounter.Interface.IsLikelyNotMountPoint(stagingTargetPath)
if err != nil {
if os.IsNotExist(err) {
if err := ns.Mounter.Interface.MakeDir(stagingTargetPath); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("Failed to create directory (%q): %v", stagingTargetPath, err))
}
notMnt = true
} else {
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown error when checking mount point (%q): %v", stagingTargetPath, err))
}
}
if !notMnt {
// TODO(#95): Check who is mounted here. No error if its us
/*
1) Target Path MUST be the vol referenced by vol ID
2) VolumeCapability MUST match
3) Readonly MUST match
*/
return &csi.NodeStageVolumeResponse{}, nil
}
// Part 3: Mount device to stagingTargetPath
// Default fstype is ext4
fstype := "ext4"
options := []string{}
if mnt := volumeCapability.GetMount(); mnt != nil {
if mnt.FsType != "" {
fstype = mnt.FsType
}
for _, flag := range mnt.MountFlags {
options = append(options, flag)
}
} else if blk := volumeCapability.GetBlock(); blk != nil {
// TODO(#64): Block volume support
return nil, status.Error(codes.Unimplemented, fmt.Sprintf("Block volume support is not yet implemented"))
}
err = ns.Mounter.FormatAndMount(devicePath, stagingTargetPath, fstype, options)
if err != nil {
return nil, status.Error(codes.Internal,
fmt.Sprintf("Failed to format and mount device from (%q) to (%q) with fstype (%q) and options (%q): %v",
devicePath, stagingTargetPath, fstype, options, err))
}
return &csi.NodeStageVolumeResponse{}, nil
}
func (ns *GCENodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
ns.mux.Lock()
defer ns.mux.Unlock()
glog.V(4).Infof("NodeUnstageVolume called with req: %#v", req)
// Validate arguments
volumeID := req.GetVolumeId()
stagingTargetPath := req.GetStagingTargetPath()
if len(volumeID) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeUnstageVolume Volume ID must be provided")
}
if len(stagingTargetPath) == 0 {
return nil, status.Error(codes.InvalidArgument, "NodeUnstageVolume Staging Target Path must be provided")
}
err := volumeutils.UnmountMountPoint(stagingTargetPath, ns.Mounter.Interface, false /* bind mount */)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("NodeUnstageVolume failed to unmount at path %s: %v", stagingTargetPath, err))
}
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (ns *GCENodeServer) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
glog.V(4).Infof("NodeGetCapabilities called with req: %#v", req)
return &csi.NodeGetCapabilitiesResponse{
Capabilities: ns.Driver.nscap,
}, nil
}
func (ns *GCENodeServer) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
glog.V(4).Infof("NodeGetInfo called with req: %#v", req)
top := &csi.Topology{
Segments: map[string]string{common.TopologyKeyZone: ns.MetadataService.GetZone()},
}
nodeID := common.CreateNodeID(ns.MetadataService.GetProject(), ns.MetadataService.GetZone(), ns.MetadataService.GetName())
resp := &csi.NodeGetInfoResponse{
NodeId: nodeID,
// TODO(#19): Set MaxVolumesPerNode based on Node Type
// Default of 0 means that CO Decides how many nodes can be published
// Can get from metadata server "machine-type"
MaxVolumesPerNode: 0,
AccessibleTopology: top,
}
return resp, nil
}
func (ns *GCENodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Error(codes.Unimplemented, fmt.Sprintf("NodeGetVolumeStats is not yet implemented"))
}
func (ns *GCENodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, fmt.Sprintf("NodeExpandVolume is not yet implemented"))
}