Skip to content

Commit 66f401e

Browse files
authored
Merge pull request #1 from vladimirvivien/hostpath-ephemeral
Hostpath driver fixes for ephemeral support
2 parents f414a15 + 02cb105 commit 66f401e

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

pkg/hostpath/controllerserver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737

3838
const (
3939
deviceID = "deviceID"
40-
provisionRoot = "/tmp/"
4140
snapshotRoot = "/tmp/"
4241
maxStorageCapacity = tib
4342
)
@@ -46,7 +45,10 @@ type controllerServer struct {
4645
caps []*csi.ControllerServiceCapability
4746
}
4847

49-
func NewControllerServer() *controllerServer {
48+
func NewControllerServer(ephemeral bool) *controllerServer {
49+
if ephemeral {
50+
return &controllerServer{caps: getControllerServiceCapabilities(nil)}
51+
}
5052
return &controllerServer{
5153
caps: getControllerServiceCapabilities(
5254
[]csi.ControllerServiceCapability_RPC_Type{
@@ -95,8 +97,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
9597
return nil, status.Errorf(codes.OutOfRange, "Requested capacity %d exceeds maximum allowed %d", capacity, maxStorageCapacity)
9698
}
9799
volumeID := uuid.NewUUID().String()
98-
path := provisionRoot + volumeID
99-
err := os.MkdirAll(path, 0777)
100+
path, err := createVolumeDir(volumeID)
100101
if err != nil {
101102
glog.V(3).Infof("failed to create volume: %v", err)
102103
return nil, err
@@ -150,8 +151,7 @@ func (cs *controllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
150151
}
151152
volumeID := req.VolumeId
152153
glog.V(4).Infof("deleting volume %s", volumeID)
153-
path := provisionRoot + volumeID
154-
os.RemoveAll(path)
154+
deleteVolumeDir(volumeID)
155155
delete(hostPathVolumes, volumeID)
156156
return &csi.DeleteVolumeResponse{}, nil
157157
}

pkg/hostpath/hostpath.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package hostpath
1818

1919
import (
2020
"fmt"
21+
"os"
2122

2223
"github.com/golang/glog"
2324

@@ -33,6 +34,10 @@ const (
3334
tib100 int64 = tib * 100
3435
)
3536

37+
const (
38+
volumeRoot = "/tmp"
39+
)
40+
3641
type hostPath struct {
3742
name string
3843
nodeID string
@@ -102,13 +107,10 @@ func NewHostPathDriver(driverName, nodeID, endpoint string, ephemeral bool) (*ho
102107
func (hp *hostPath) Run() {
103108
s := NewNonBlockingGRPCServer()
104109

105-
hp.ids = nil
106-
hp.cs = nil
110+
hp.ids = NewIdentityServer(hp.name, hp.version)
107111
hp.ns = NewNodeServer(hp.nodeID, hp.ephemeral)
108-
if !hp.ephemeral {
109-
hp.ids = NewIdentityServer(hp.name, hp.version)
110-
hp.cs = NewControllerServer()
111-
}
112+
hp.cs = NewControllerServer(hp.ephemeral)
113+
112114
s.Start(hp.endpoint, hp.ids, hp.cs, hp.ns)
113115
s.Wait()
114116
}
@@ -137,3 +139,29 @@ func getSnapshotByName(name string) (hostPathSnapshot, error) {
137139
}
138140
return hostPathSnapshot{}, fmt.Errorf("snapshot name %s does not exit in the snapshots list", name)
139141
}
142+
143+
// getVolumePath returs the canonical path for hostpath volume
144+
func getVolumePath(volID string) string {
145+
return fmt.Sprintf("%s/%s", volumeRoot, volID)
146+
}
147+
148+
// createVolume create the directory for the hostpath volume.
149+
// It returns the volume path or err if one occurs.
150+
func createVolumeDir(volID string) (string, error) {
151+
path := getVolumePath(volID)
152+
err := os.MkdirAll(path, 0777)
153+
if err != nil {
154+
return "", err
155+
}
156+
157+
return path, nil
158+
}
159+
160+
// deleteVolume deletes the directory for the hostpath volume.
161+
func deleteVolumeDir(volID string) error {
162+
path := getVolumePath(volID)
163+
if err := os.RemoveAll(path); err != nil {
164+
return err
165+
}
166+
return nil
167+
}

pkg/hostpath/nodeserver.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package hostpath
1818

1919
import (
20+
"fmt"
2021
"os"
22+
"strings"
2123

2224
"github.com/golang/glog"
2325
"golang.org/x/net/context"
@@ -90,19 +92,25 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
9092
options = append(options, "ro")
9193
}
9294
mounter := mount.New("")
93-
path := provisionRoot + volumeId
94-
newEphemeral := false
95+
96+
path := getVolumePath(volumeId)
9597
if ns.ephemeral {
96-
if err = os.MkdirAll(path, 0777); err != nil && !os.IsNotExist(err) {
98+
volPath, err := createVolumeDir(volumeId)
99+
if err != nil && !os.IsExist(err) {
97100
return nil, status.Error(codes.Internal, err.Error())
98101
}
99-
newEphemeral = true
102+
glog.V(4).Infof("ephemeral mode: created volume: %s", volPath)
100103
}
104+
101105
if err := mounter.Mount(path, targetPath, "", options); err != nil {
102-
if newEphemeral {
103-
os.RemoveAll(path)
106+
var errList strings.Builder
107+
errList.WriteString(err.Error())
108+
if ns.ephemeral {
109+
if rmErr := os.RemoveAll(path); rmErr != nil && !os.IsNotExist(rmErr) {
110+
errList.WriteString(fmt.Sprintf(" :%s", rmErr.Error()))
111+
}
104112
}
105-
return nil, err
113+
return nil, status.Error(codes.Internal, errList.String())
106114
}
107115

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

130138
if ns.ephemeral {
131139
glog.V(4).Infof("deleting volume %s", volumeID)
132-
path := provisionRoot + volumeID
133-
os.RemoveAll(path)
140+
deleteVolumeDir(volumeID)
134141
}
135142

136143
return &csi.NodeUnpublishVolumeResponse{}, nil

0 commit comments

Comments
 (0)