Skip to content

Commit

Permalink
Fix merge conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Fox <Kevin.Fox@pnnl.gov>
  • Loading branch information
kfox1111 committed Mar 16, 2019
2 parents 136a97a + 46d99b0 commit fb328e6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
3 changes: 2 additions & 1 deletion cmd/hostpathplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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")
showVersion = flag.Bool("version", false, "Show version.")
// Set by the build process
version = ""
Expand All @@ -52,7 +53,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
7 changes: 5 additions & 2 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,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 @@ -136,7 +139,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
}

volumeID := uuid.NewUUID().String()
path := provisionRoot + volumeID
path := getVolumePath(volumeID)

switch requestedAccessType {
case blockAccess:
Expand Down
52 changes: 40 additions & 12 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 @@ -34,10 +35,11 @@ const (
)

type hostPath struct {
name string
nodeID string
version string
endpoint string
name string
nodeID string
version string
endpoint string
ephemeral bool

ids *identityServer
ns *nodeServer
Expand Down Expand Up @@ -74,7 +76,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 @@ -91,19 +93,19 @@ func NewHostPathDriver(driverName, nodeID, endpoint string) (*hostPath, error) {
glog.Infof("Version: %s", vendorVersion)

return &hostPath{
name: driverName,
version: vendorVersion,
nodeID: nodeID,
endpoint: endpoint,
name: driverName,
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()
hp.ns = NewNodeServer(hp.nodeID, hp.ephemeral)
hp.cs = NewControllerServer(hp.ephemeral)

s := NewNonBlockingGRPCServer()
s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
Expand Down Expand Up @@ -134,3 +136,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", provisionRoot, 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
}
30 changes: 26 additions & 4 deletions pkg/hostpath/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package hostpath
import (
"fmt"
"os"
"strings"

"github.com/golang/glog"
"golang.org/x/net/context"
Expand All @@ -31,12 +32,14 @@ import (
)

type nodeServer struct {
nodeID string
nodeID string
ephemeral bool
}

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

Expand Down Expand Up @@ -151,8 +154,22 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
}
mounter := mount.New("")
path := provisionRoot + volumeId
if ns.ephemeral {
volPath, err := createVolumeDir(volumeId)
if err != nil && !os.IsExist(err) {
return nil, status.Error(codes.Internal, err.Error())
}
glog.V(4).Infof("ephemeral mode: created volume: %s", volPath)
}

if err := mounter.Mount(path, targetPath, "", options); err != nil {
return nil, err
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()))
}
}
}
}

Expand Down Expand Up @@ -196,6 +213,11 @@ 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)
deleteVolumeDir(volumeID)
}

return &csi.NodeUnpublishVolumeResponse{}, nil
}

Expand Down

0 comments on commit fb328e6

Please sign in to comment.