Skip to content

Commit

Permalink
Create target dir if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
huffmanca committed Nov 11, 2020
1 parent 5deb83d commit d684ca9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions mock/service/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (s *service) NodePublishVolume(
// nodeMntPathKey is the key in the volume's attributes that is set to a
// mock mount path if the volume has been published by the node
nodeMntPathKey := path.Join(s.nodeID, req.TargetPath)
nodeMntPathCreated := nodeMntPathKey + "Created"

// Check to see if the volume has already been published.
if v.VolumeContext[nodeMntPathKey] != "" {
Expand All @@ -203,15 +204,20 @@ func (s *service) NodePublishVolume(
ISEphemeral: true,
}
} else {
if req.GetStagingTargetPath() != "" {
exists, err := checkTargetExists(req.GetStagingTargetPath())
if req.GetTargetPath() != "" {
exists, err := checkTargetExists(req.GetTargetPath())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if !exists {
status.Errorf(codes.Internal, "staging target path %s does not exist", req.GetStagingTargetPath())
// If target path does not exist we need to create the directory where volume will be staged
if err = os.MkdirAll(req.TargetPath, os.FileMode(0755)); err != nil {
msg := fmt.Sprintf("NodePublishVolume: could not create target dir %q: %v", req.TargetPath, err)
return nil, status.Error(codes.Internal, msg)
}
v.VolumeContext[nodeMntPathCreated] = "true"
}
v.VolumeContext[nodeMntPathKey] = req.GetStagingTargetPath()
v.VolumeContext[nodeMntPathKey] = req.GetTargetPath()
} else {
v.VolumeContext[nodeMntPathKey] = device
}
Expand Down Expand Up @@ -254,12 +260,22 @@ func (s *service) NodeUnpublishVolume(
// nodeMntPathKey is the key in the volume's attributes that is set to a
// mock mount path if the volume has been published by the node
nodeMntPathKey := path.Join(s.nodeID, req.TargetPath)
nodeMntPathCreated := nodeMntPathKey + "Created"

// Check to see if the volume has already been unpublished.
if v.VolumeContext[nodeMntPathKey] == "" {
return &csi.NodeUnpublishVolumeResponse{}, nil
}

// Check to see if we created the volume, and if so delete it
if v.VolumeContext[nodeMntPathCreated] == "true" {
delete(v.VolumeContext, nodeMntPathCreated)
err := os.RemoveAll(v.VolumeContext[nodeMntPathKey])
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to delete previously created target directory")
}
}

// Unpublish the volume.
delete(v.VolumeContext, nodeMntPathKey)
s.vols[i] = v
Expand Down

0 comments on commit d684ca9

Please sign in to comment.