Skip to content

Commit

Permalink
per-volume ephemeral mode
Browse files Browse the repository at this point in the history
This removes the limitation that the driver can only be deployed for
one mode or the other. Instead the driver depends on the new
csi.storage.k8s.io/ephemeral
field (kubernetes/kubernetes#79624) in the
volume context to determine how it should behave in NodePublishVolume.

The --ephemeral parameter is deprecated and will trigger a warning
when still used.

Supporting both modes in the same deployment makes it possible to test
ephemeral mode without having to change how the Prow jobs deploy the
driver, which is once as part of the cluster setup.
  • Loading branch information
pohly committed Jul 10, 2019
1 parent 29543c8 commit 38d715f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
5 changes: 3 additions & 2 deletions cmd/hostpathplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path"

"github.com/kubernetes-csi/csi-driver-host-path/pkg/hostpath"
"github.com/kubernetes-csi/csi-lib-utils/deprecatedflags"
)

func init() {
Expand All @@ -33,7 +34,7 @@ var (
endpoint = flag.String("endpoint", "unix://tmp/csi.sock", "CSI endpoint")
driverName = flag.String("drivername", "hostpath.csi.k8s.io", "name of the driver")
nodeID = flag.String("nodeid", "", "node id")
ephemeral = flag.Bool("ephemeral", false, "deploy in ephemeral mode")
_ = deprecatedflags.Add("ephemeral")
showVersion = flag.Bool("version", false, "Show version.")
// Set by the build process
version = ""
Expand All @@ -53,7 +54,7 @@ func main() {
}

func handle() {
driver, err := hostpath.NewHostPathDriver(*driverName, *nodeID, *endpoint, version, *ephemeral)
driver, err := hostpath.NewHostPathDriver(*driverName, *nodeID, *endpoint, version)
if err != nil {
fmt.Printf("Failed to initialize driver: %s", err.Error())
os.Exit(1)
Expand Down
7 changes: 7 additions & 0 deletions deploy/kubernetes-1.14/hostpath/csi-hostpath-plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ spec:
path: /var/lib/csi-hostpath-data/
type: DirectoryOrCreate
name: csi-data-dir
---
apiVersion: storage.k8s.io/v1beta1
kind: CSIDriver
metadata:
name: hostpath.csi.k8s.io
spec:
podInfoOnMount: true
7 changes: 2 additions & 5 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ type controllerServer struct {
caps []*csi.ControllerServiceCapability
}

func NewControllerServer(ephemeral bool) *controllerServer {
if ephemeral {
return &controllerServer{caps: getControllerServiceCapabilities(nil)}
}
func NewControllerServer() *controllerServer {
return &controllerServer{
caps: getControllerServiceCapabilities(
[]csi.ControllerServiceCapability_RPC_Type{
Expand Down Expand Up @@ -167,7 +164,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
}
}

vol, err := createHostpathVolume(volumeID, req.GetName(), capacity, requestedAccessType)
vol, err := createHostpathVolume(volumeID, req.GetName(), capacity, requestedAccessType, false /* ephemeral */)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to create volume: %s", err))
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ const (
)

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

ids *identityServer
ns *nodeServer
Expand All @@ -53,6 +52,7 @@ type hostPathVolume struct {
VolSize int64 `json:"volSize"`
VolPath string `json:"volPath"`
VolAccessType accessType `json:"volAccessType"`
Ephemeral bool `json:"ephemeral"`
}

type hostPathSnapshot struct {
Expand All @@ -77,7 +77,7 @@ func init() {
hostPathVolumeSnapshots = map[string]hostPathSnapshot{}
}

func NewHostPathDriver(driverName, nodeID, endpoint, version string, ephemeral bool) (*hostPath, error) {
func NewHostPathDriver(driverName, nodeID, endpoint, version string) (*hostPath, error) {
if driverName == "" {
return nil, fmt.Errorf("No driver name provided")
}
Expand All @@ -97,19 +97,18 @@ func NewHostPathDriver(driverName, nodeID, endpoint, version string, ephemeral b
glog.Infof("Version: %s", vendorVersion)

return &hostPath{
name: driverName,
version: vendorVersion,
nodeID: nodeID,
endpoint: endpoint,
ephemeral: ephemeral,
name: driverName,
version: vendorVersion,
nodeID: nodeID,
endpoint: endpoint,
}, nil
}

func (hp *hostPath) Run() {
// Create GRPC servers
hp.ids = NewIdentityServer(hp.name, hp.version)
hp.ns = NewNodeServer(hp.nodeID, hp.ephemeral)
hp.cs = NewControllerServer(hp.ephemeral)
hp.ns = NewNodeServer(hp.nodeID)
hp.cs = NewControllerServer()

s := NewNonBlockingGRPCServer()
s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
Expand Down Expand Up @@ -148,7 +147,7 @@ func getVolumePath(volID string) string {

// createVolume create the directory for the hostpath volume.
// It returns the volume path or err if one occurs.
func createHostpathVolume(volID, name string, cap int64, volAccessType accessType) (*hostPathVolume, error) {
func createHostpathVolume(volID, name string, cap int64, volAccessType accessType, ephemeral bool) (*hostPathVolume, error) {
path := getVolumePath(volID)
if volAccessType == mountAccess {
err := os.MkdirAll(path, 0777)
Expand All @@ -163,6 +162,7 @@ func createHostpathVolume(volID, name string, cap int64, volAccessType accessTyp
VolSize: cap,
VolPath: path,
VolAccessType: volAccessType,
Ephemeral: ephemeral,
}
hostPathVolumes[volID] = hostpathVol
return &hostpathVol, nil
Expand Down
17 changes: 8 additions & 9 deletions pkg/hostpath/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ import (
)

type nodeServer struct {
nodeID string
ephemeral bool
nodeID string
}

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

Expand All @@ -57,17 +55,18 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
}

targetPath := req.GetTargetPath()
ephemeralVolume := req.GetVolumeContext()["csi.storage.k8s.io/ephemeral"] == "true"

if req.GetVolumeCapability().GetBlock() != nil &&
req.GetVolumeCapability().GetMount() != nil {
return nil, status.Error(codes.InvalidArgument, "cannot have both block and mount access type")
}

// if ephemeral is specified, create volume here to avoid errors
if ns.ephemeral {
if ephemeralVolume {
volID := req.GetVolumeId()
volName := fmt.Sprintf("ephemeral-%s", volID)
vol, err := createHostpathVolume(req.GetVolumeId(), volName, maxStorageCapacity, mountAccess)
vol, err := createHostpathVolume(req.GetVolumeId(), volName, maxStorageCapacity, mountAccess, ephemeralVolume)
if err != nil && !os.IsExist(err) {
glog.Error("ephemeral mode failed to create volume: ", err)
return nil, status.Error(codes.Internal, err.Error())
Expand Down Expand Up @@ -170,7 +169,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
if err := mounter.Mount(path, targetPath, "", options); err != nil {
var errList strings.Builder
errList.WriteString(err.Error())
if ns.ephemeral {
if vol.Ephemeral {
if rmErr := os.RemoveAll(path); rmErr != nil && !os.IsNotExist(rmErr) {
errList.WriteString(fmt.Sprintf(" :%s", rmErr.Error()))
}
Expand Down Expand Up @@ -218,7 +217,7 @@ 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 {
if vol.Ephemeral {
glog.V(4).Infof("deleting volume %s", volumeID)
if err := deleteHostpathVolume(volumeID); err != nil && !os.IsNotExist(err) {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to delete volume: %s", err))
Expand Down

0 comments on commit 38d715f

Please sign in to comment.