diff --git a/cmd/hostpathplugin/main.go b/cmd/hostpathplugin/main.go index 983b35769..d98e6be30 100644 --- a/cmd/hostpathplugin/main.go +++ b/cmd/hostpathplugin/main.go @@ -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() { @@ -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 = "" @@ -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) diff --git a/deploy/kubernetes-1.14/hostpath/csi-hostpath-plugin.yaml b/deploy/kubernetes-1.14/hostpath/csi-hostpath-plugin.yaml index 29324d793..5dd555c40 100644 --- a/deploy/kubernetes-1.14/hostpath/csi-hostpath-plugin.yaml +++ b/deploy/kubernetes-1.14/hostpath/csi-hostpath-plugin.yaml @@ -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 diff --git a/pkg/hostpath/controllerserver.go b/pkg/hostpath/controllerserver.go index 5fbe65526..e97ae49c9 100644 --- a/pkg/hostpath/controllerserver.go +++ b/pkg/hostpath/controllerserver.go @@ -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{ @@ -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)) } diff --git a/pkg/hostpath/hostpath.go b/pkg/hostpath/hostpath.go index 725e00129..d994fb73b 100644 --- a/pkg/hostpath/hostpath.go +++ b/pkg/hostpath/hostpath.go @@ -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 @@ -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 { @@ -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") } @@ -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) @@ -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) @@ -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 diff --git a/pkg/hostpath/nodeserver.go b/pkg/hostpath/nodeserver.go index 077692452..6a280250b 100644 --- a/pkg/hostpath/nodeserver.go +++ b/pkg/hostpath/nodeserver.go @@ -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, } } @@ -57,6 +55,7 @@ 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 { @@ -64,10 +63,10 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis } // 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()) @@ -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())) } @@ -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))