Skip to content

Commit

Permalink
Merge pull request #1 from vladimirvivien/hostpath-ephemeral
Browse files Browse the repository at this point in the history
Hostpath driver fixes for ephemeral support
  • Loading branch information
kfox1111 authored Mar 15, 2019
2 parents f414a15 + 02cb105 commit 66f401e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
12 changes: 6 additions & 6 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (

const (
deviceID = "deviceID"
provisionRoot = "/tmp/"
snapshotRoot = "/tmp/"
maxStorageCapacity = tib
)
Expand All @@ -46,7 +45,10 @@ type controllerServer struct {
caps []*csi.ControllerServiceCapability
}

func NewControllerServer() *controllerServer {
func NewControllerServer(ephemeral bool) *controllerServer {
if ephemeral {
return &controllerServer{caps: getControllerServiceCapabilities(nil)}
}
return &controllerServer{
caps: getControllerServiceCapabilities(
[]csi.ControllerServiceCapability_RPC_Type{
Expand Down Expand Up @@ -95,8 +97,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, maxStorageCapacity)
}
volumeID := uuid.NewUUID().String()
path := provisionRoot + volumeID
err := os.MkdirAll(path, 0777)
path, err := createVolumeDir(volumeID)
if err != nil {
glog.V(3).Infof("failed to create volume: %v", err)
return nil, err
Expand Down Expand Up @@ -150,8 +151,7 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
}
volumeID := req.VolumeId
glog.V(4).Infof("deleting volume %s", volumeID)
path := provisionRoot + volumeID
os.RemoveAll(path)
deleteVolumeDir(volumeID)
delete(hostPathVolumes, volumeID)
return &csi.DeleteVolumeResponse{}, nil
}
Expand Down
40 changes: 34 additions & 6 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package hostpath

import (
"fmt"
"os"

"github.com/golang/glog"

Expand All @@ -33,6 +34,10 @@ const (
tib100 int64 = tib * 100
)

const (
volumeRoot = "/tmp"
)

type hostPath struct {
name string
nodeID string
Expand Down Expand Up @@ -102,13 +107,10 @@ func NewHostPathDriver(driverName, nodeID, endpoint string, ephemeral bool) (*ho
func (hp *hostPath) Run() {
s := NewNonBlockingGRPCServer()

hp.ids = nil
hp.cs = nil
hp.ids = NewIdentityServer(hp.name, hp.version)
hp.ns = NewNodeServer(hp.nodeID, hp.ephemeral)
if !hp.ephemeral {
hp.ids = NewIdentityServer(hp.name, hp.version)
hp.cs = NewControllerServer()
}
hp.cs = NewControllerServer(hp.ephemeral)

s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
s.Wait()
}
Expand Down Expand Up @@ -137,3 +139,29 @@ func getSnapshotByName(name string) (hostPathSnapshot, error) {
}
return hostPathSnapshot{}, fmt.Errorf("snapshot name %s does not exit in the snapshots list", name)
}

// getVolumePath returs the canonical path for hostpath volume
func getVolumePath(volID string) string {
return fmt.Sprintf("%s/%s", volumeRoot, volID)
}

// createVolume create the directory for the hostpath volume.
// It returns the volume path or err if one occurs.
func createVolumeDir(volID string) (string, error) {
path := getVolumePath(volID)
err := os.MkdirAll(path, 0777)
if err != nil {
return "", err
}

return path, nil
}

// deleteVolume deletes the directory for the hostpath volume.
func deleteVolumeDir(volID string) error {
path := getVolumePath(volID)
if err := os.RemoveAll(path); err != nil {
return err
}
return nil
}
25 changes: 16 additions & 9 deletions pkg/hostpath/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package hostpath

import (
"fmt"
"os"
"strings"

"github.com/golang/glog"
"golang.org/x/net/context"
Expand Down Expand Up @@ -90,19 +92,25 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
options = append(options, "ro")
}
mounter := mount.New("")
path := provisionRoot + volumeId
newEphemeral := false

path := getVolumePath(volumeId)
if ns.ephemeral {
if err = os.MkdirAll(path, 0777); err != nil && !os.IsNotExist(err) {
volPath, err := createVolumeDir(volumeId)
if err != nil && !os.IsExist(err) {
return nil, status.Error(codes.Internal, err.Error())
}
newEphemeral = true
glog.V(4).Infof("ephemeral mode: created volume: %s", volPath)
}

if err := mounter.Mount(path, targetPath, "", options); err != nil {
if newEphemeral {
os.RemoveAll(path)
var errList strings.Builder
errList.WriteString(err.Error())
if ns.ephemeral {
if rmErr := os.RemoveAll(path); rmErr != nil && !os.IsNotExist(rmErr) {
errList.WriteString(fmt.Sprintf(" :%s", rmErr.Error()))
}
}
return nil, err
return nil, status.Error(codes.Internal, errList.String())
}

return &csi.NodePublishVolumeResponse{}, nil
Expand All @@ -129,8 +137,7 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu

if ns.ephemeral {
glog.V(4).Infof("deleting volume %s", volumeID)
path := provisionRoot + volumeID
os.RemoveAll(path)
deleteVolumeDir(volumeID)
}

return &csi.NodeUnpublishVolumeResponse{}, nil
Expand Down

0 comments on commit 66f401e

Please sign in to comment.