Skip to content

Commit 60f684c

Browse files
authored
fix: update unpublish volume methods to handle mounted state (#15)
Signed-off-by: caoyifan.cyf <[email protected]>
1 parent 4ef026b commit 60f684c

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

pkg/service/node.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,23 @@ func (s *Service) nodeUnpublishVolume(
172172
}
173173

174174
if !isMounted {
175-
logger.WithContext(ctx).Infof("target path is already umounted")
176-
return &csi.NodeUnpublishVolumeResponse{}, isStaticVolume, nil
175+
logger.WithContext(ctx).Warnf("target path is already umounted")
177176
}
178177

179178
if isStaticVolume {
180-
resp, err := s.nodeUnPublishVolumeStatic(ctx, volumeID, targetPath)
179+
resp, err := s.nodeUnPublishVolumeStatic(ctx, volumeID, targetPath, isMounted)
181180
return resp, isStaticVolume, err
182181
}
183182

184183
statusPath := filepath.Join(s.cfg.Get().GetVolumeDir(volumeID), "status.json")
185184
volumeStatus, err := s.sm.Get(statusPath)
186185
if err == nil && volumeStatus != nil && volumeStatus.Inline {
187186
logger.WithContext(ctx).Infof("unpublishing static inline volume: %s", volumeStatus.Reference)
188-
resp, err := s.nodeUnPublishVolumeStaticInlineVolume(ctx, volumeID, targetPath)
187+
resp, err := s.nodeUnPublishVolumeStaticInlineVolume(ctx, volumeID, targetPath, isMounted)
189188
return resp, isStaticVolume, err
190189
}
191190

192-
resp, err := s.nodeUnPublishVolumeDynamic(ctx, volumeID, targetPath)
191+
resp, err := s.nodeUnPublishVolumeDynamic(ctx, volumeID, targetPath, isMounted)
193192
return resp, isStaticVolume, err
194193
}
195194

pkg/service/node_dynamic.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (s *Service) nodePublishVolumeDynamicForRootMount(ctx context.Context, volu
4949
return &csi.NodePublishVolumeResponse{}, nil
5050
}
5151

52-
func (s *Service) nodeUnPublishVolumeDynamic(ctx context.Context, volumeName, targetPath string) (*csi.NodeUnpublishVolumeResponse, error) {
52+
func (s *Service) nodeUnPublishVolumeDynamic(ctx context.Context, volumeName, targetPath string, isMounted bool) (*csi.NodeUnpublishVolumeResponse, error) {
5353
sourceCSIDir := s.cfg.Get().GetCSISockDirForDynamic(volumeName)
5454
volumeDir := s.cfg.Get().GetVolumeDirForDynamic(volumeName)
5555

@@ -71,8 +71,10 @@ func (s *Service) nodeUnPublishVolumeDynamic(ctx context.Context, volumeName, ta
7171
}
7272
}
7373

74-
if err := mounter.UMount(ctx, targetPath, true); err != nil {
75-
logger.WithContext(ctx).WithError(err).Errorf("unmount target path")
74+
if isMounted {
75+
if err := mounter.UMount(ctx, targetPath, true); err != nil {
76+
return nil, status.Error(codes.Internal, errors.Wrapf(err, "unmount target path").Error())
77+
}
7678
}
7779

7880
sourceVolumeDir := s.cfg.Get().GetVolumeDirForDynamic(volumeName)

pkg/service/node_static.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ func (s *Service) nodePublishVolumeStatic(ctx context.Context, volumeName, targe
3939
return &csi.NodePublishVolumeResponse{}, nil
4040
}
4141

42-
func (s *Service) nodeUnPublishVolumeStatic(ctx context.Context, volumeName, targetPath string) (*csi.NodeUnpublishVolumeResponse, error) {
43-
if err := mounter.UMount(ctx, targetPath, true); err != nil {
44-
return nil, status.Error(codes.Internal, errors.Wrapf(err, "unmount target path").Error())
42+
func (s *Service) nodeUnPublishVolumeStatic(ctx context.Context, volumeName, targetPath string, isMounted bool) (*csi.NodeUnpublishVolumeResponse, error) {
43+
if isMounted {
44+
if err := mounter.UMount(ctx, targetPath, true); err != nil {
45+
return nil, status.Error(codes.Internal, errors.Wrapf(err, "unmount target path").Error())
46+
}
4547
}
4648

4749
statusPath := filepath.Join(s.cfg.Get().GetVolumeDir(volumeName), "status.json")

pkg/service/node_static_inline.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ func (s *Service) nodePublishVolumeStaticInlineVolume(ctx context.Context, volum
5151
return &csi.NodePublishVolumeResponse{}, nil
5252
}
5353

54-
func (s *Service) nodeUnPublishVolumeStaticInlineVolume(ctx context.Context, volumeName, targetPath string) (*csi.NodeUnpublishVolumeResponse, error) {
55-
if err := mounter.UMount(ctx, targetPath, true); err != nil {
56-
logger.WithContext(ctx).WithError(err).Errorf("unmount target path")
57-
// return nil, status.Error(codes.Internal, errors.Wrapf(err, "unmount target path").Error())
54+
func (s *Service) nodeUnPublishVolumeStaticInlineVolume(ctx context.Context, volumeName, targetPath string, isMounted bool) (*csi.NodeUnpublishVolumeResponse, error) {
55+
if isMounted {
56+
if err := mounter.UMount(ctx, targetPath, true); err != nil {
57+
return nil, status.Error(codes.Internal, errors.Wrapf(err, "unmount target path").Error())
58+
}
5859
}
5960

6061
sourceVolumeDir := s.cfg.Get().GetVolumeDir(volumeName)

0 commit comments

Comments
 (0)