Skip to content

Commit

Permalink
Ephemeral support
Browse files Browse the repository at this point in the history
This adds support to the hostPath driver to be used
in an ephemeral mode.

Signed-off-by: Kevin Fox <[email protected]>
  • Loading branch information
kfox1111 committed Feb 28, 2019
1 parent c44a77d commit 0e84abc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/hostpathplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
driverName = flag.String("drivername", "csi-hostpath", "name of the driver")
nodeID = flag.String("nodeid", "", "node id")
ephemeral = flag.Bool("ephemeral", false, "deploy in ephemeral mode")
)

func main() {
Expand All @@ -42,7 +43,7 @@ func main() {
}

func handle() {
driver, err := hostpath.NewHostPathDriver(*driverName, *nodeID, *endpoint)
driver, err := hostpath.NewHostPathDriver(*driverName, *nodeID, *endpoint, *ephemeral)
if err != nil {
fmt.Printf("Failed to initialize driver: %s", err.Error())
os.Exit(1)
Expand Down
18 changes: 11 additions & 7 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type hostPath struct {
nodeID string
version string
endpoint string
ephemeral bool

ids *identityServer
ns *nodeServer
Expand Down Expand Up @@ -73,7 +74,7 @@ func init() {
hostPathVolumeSnapshots = map[string]hostPathSnapshot{}
}

func NewHostPathDriver(driverName, nodeID, endpoint string) (*hostPath, error) {
func NewHostPathDriver(driverName, nodeID, endpoint string, ephemeral bool) (*hostPath, error) {
if driverName == "" {
return nil, fmt.Errorf("No driver name provided")
}
Expand All @@ -94,17 +95,20 @@ func NewHostPathDriver(driverName, nodeID, endpoint string) (*hostPath, error) {
version: vendorVersion,
nodeID: nodeID,
endpoint: endpoint,
ephemeral: ephemeral,
}, nil
}

func (hp *hostPath) Run() {

// Create GRPC servers
hp.ids = NewIdentityServer(hp.name, hp.version)
hp.ns = NewNodeServer(hp.nodeID)
hp.cs = NewControllerServer()

s := NewNonBlockingGRPCServer()

hp.ids = nil
hp.cs = nil
hp.ns = NewNodeServer(hp.nodeID, hp.ephemeral)
if !hp.ephemeral {
hp.ids = NewIdentityServer(hp.name, hp.version)
hp.cs = NewControllerServer()
}
s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
s.Wait()
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/hostpath/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import (

type nodeServer struct {
nodeID string
ephemeral bool
}

func NewNodeServer(nodeId string) *nodeServer {
func NewNodeServer(nodeId string, ephemeral bool) *nodeServer {
return &nodeServer{
nodeID: nodeId,
ephemeral: ephemeral,
}
}

Expand Down Expand Up @@ -89,7 +91,17 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
}
mounter := mount.New("")
path := provisionRoot + volumeId
newEphemeral := false
if ns.ephemeral {
if err = os.MkdirAll(path, 0777); err != nil && ! os.IsNotExist(err) {
return nil, status.Error(codes.Internal, err.Error())
}
newEphemeral = true
}
if err := mounter.Mount(path, targetPath, "", options); err != nil {
if newEphemeral {
os.RemoveAll(path)
}
return nil, err
}

Expand All @@ -115,6 +127,12 @@ func (ns *nodeServer) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpu
}
glog.V(4).Infof("hostpath: volume %s/%s has been unmounted.", targetPath, volumeID)

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

return &csi.NodeUnpublishVolumeResponse{}, nil
}

Expand Down

0 comments on commit 0e84abc

Please sign in to comment.