@@ -69,6 +69,21 @@ func getDefaultFsType() string {
69
69
return defaultLinuxFsType
70
70
}
71
71
}
72
+ func (ns * GCENodeServer ) isVolumePathMounted (path string ) bool {
73
+ notMnt , err := ns .Mounter .Interface .IsLikelyNotMountPoint (path )
74
+ klog .V (4 ).Infof ("NodePublishVolume check volume path %s is mounted %d: error %v" , path , ! notMnt , err )
75
+ if err == nil && ! notMnt {
76
+ // TODO(#95): check if mount is compatible. Return OK if it is, or appropriate error.
77
+ /*
78
+ 1) Target Path MUST be the vol referenced by vol ID
79
+ 2) TODO(#253): Check volume capability matches for ALREADY_EXISTS
80
+ 3) Readonly MUST match
81
+
82
+ */
83
+ return true
84
+ }
85
+ return false
86
+ }
72
87
73
88
func (ns * GCENodeServer ) NodePublishVolume (ctx context.Context , req * csi.NodePublishVolumeRequest ) (* csi.NodePublishVolumeResponse , error ) {
74
89
// Validate Arguments
@@ -99,18 +114,7 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
99
114
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("VolumeCapability is invalid: %v" , err ))
100
115
}
101
116
102
- notMnt , err := ns .Mounter .Interface .IsLikelyNotMountPoint (targetPath )
103
- if err != nil && ! os .IsNotExist (err ) {
104
- return nil , status .Error (codes .Internal , fmt .Sprintf ("cannot validate mount point: %s %v" , targetPath , err ))
105
- }
106
- if ! notMnt {
107
- // TODO(#95): check if mount is compatible. Return OK if it is, or appropriate error.
108
- /*
109
- 1) Target Path MUST be the vol referenced by vol ID
110
- 2) TODO(#253): Check volume capability matches for ALREADY_EXISTS
111
- 3) Readonly MUST match
112
-
113
- */
117
+ if ns .isVolumePathMounted (targetPath ) {
114
118
klog .V (4 ).Infof ("NodePublishVolume succeeded on volume %v to %s, mount already exists." , volumeID , targetPath )
115
119
return & csi.NodePublishVolumeResponse {}, nil
116
120
}
@@ -122,6 +126,7 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
122
126
if readOnly {
123
127
options = append (options , "ro" )
124
128
}
129
+ var err error
125
130
126
131
if mnt := volumeCapability .GetMount (); mnt != nil {
127
132
if mnt .FsType != "" {
@@ -169,12 +174,16 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
169
174
170
175
err = ns .Mounter .Interface .Mount (sourcePath , targetPath , fstype , options )
171
176
if err != nil {
177
+ klog .Errorf ("Mount of disk %s failed: %v" , targetPath , err )
172
178
notMnt , mntErr := ns .Mounter .Interface .IsLikelyNotMountPoint (targetPath )
173
179
if mntErr != nil {
174
180
klog .Errorf ("IsLikelyNotMountPoint check failed: %v" , mntErr )
175
181
return nil , status .Error (codes .Internal , fmt .Sprintf ("NodePublishVolume failed to check whether target path is a mount point: %v" , err ))
176
182
}
177
183
if ! notMnt {
184
+ // TODO: check the logic here again. If mntErr == nil & notMnt == false, it means volume is actually mounted.
185
+ // Why need to unmount?
186
+ klog .Warning ("Although volume mount failed, but IsLikelyNotMountPoint returns volume %s is mounted already at %s" , volumeID , targetPath )
178
187
if mntErr = ns .Mounter .Interface .Unmount (targetPath ); mntErr != nil {
179
188
klog .Errorf ("Failed to unmount: %v" , mntErr )
180
189
return nil , status .Error (codes .Internal , fmt .Sprintf ("NodePublishVolume failed to unmount target path: %v" , err ))
@@ -190,8 +199,9 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
190
199
return nil , status .Error (codes .Internal , fmt .Sprintf ("NodePublishVolume something is wrong with mounting: %v" , err ))
191
200
}
192
201
}
193
- os .Remove (targetPath )
194
- klog .Errorf ("Mount of disk %s failed: %v" , targetPath , err )
202
+ if err := os .Remove (targetPath ); err != nil {
203
+ klog .Errorf ("failed to remove targetPath %s: %v" , targetPath , err )
204
+ }
195
205
return nil , status .Error (codes .Internal , fmt .Sprintf ("NodePublishVolume mount of disk failed: %v" , err ))
196
206
}
197
207
@@ -280,22 +290,11 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
280
290
klog .V (4 ).Infof ("Successfully found attached GCE PD %q at device path %s." , volumeKey .Name , devicePath )
281
291
282
292
// Part 2: Check if mount already exists at stagingTargetPath
283
- notMnt , err := ns .Mounter .Interface .IsLikelyNotMountPoint (stagingTargetPath )
284
- if err != nil && ! os .IsNotExist (err ) {
285
- return nil , status .Error (codes .Internal , fmt .Sprintf ("cannot validate mount point: %s %v" , stagingTargetPath , err ))
286
- }
287
- if ! notMnt {
288
- // TODO(#95): Check who is mounted here. No error if its us
289
- /*
290
- 1) Target Path MUST be the vol referenced by vol ID
291
- 2) VolumeCapability MUST match
292
- 3) Readonly MUST match
293
-
294
- */
295
- klog .V (4 ).Infof ("NodeStageVolume succeeded on %v to %s, mount already exists." , volumeID , stagingTargetPath )
293
+ if ns .isVolumePathMounted (stagingTargetPath ) {
294
+ klog .V (4 ).Infof ("NodeStageVolume succeeded on volume %v to %s, mount already exists." , volumeID , stagingTargetPath )
296
295
return & csi.NodeStageVolumeResponse {}, nil
297
-
298
296
}
297
+
299
298
if err := prepareStagePath (stagingTargetPath , ns .Mounter ); err != nil {
300
299
return nil , status .Error (codes .Internal , fmt .Sprintf ("mkdir failed on disk %s (%v)" , stagingTargetPath , err ))
301
300
}
0 commit comments