Skip to content

Commit 08dfcb7

Browse files
committed
Create dataRoot if it doesn't exist, consolidate provisionRoot and snapshotRoot
1 parent 0bb7116 commit 08dfcb7

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

pkg/hostpath/controllerserver.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"fmt"
2121
"math"
2222
"os"
23+
"path/filepath"
2324
"sort"
2425
"strconv"
25-
"strings"
2626

2727
"github.com/golang/protobuf/ptypes"
2828

@@ -38,8 +38,6 @@ import (
3838

3939
const (
4040
deviceID = "deviceID"
41-
provisionRoot = "/csi-data-dir"
42-
snapshotRoot = "/csi-data-dir"
4341
maxStorageCapacity = tib
4442
)
4543

@@ -251,6 +249,11 @@ func (cs *controllerServer) ListVolumes(ctx context.Context, req *csi.ListVolume
251249
return nil, status.Error(codes.Unimplemented, "")
252250
}
253251

252+
// getSnapshotPath returns the full path to where the snapshot is stored
253+
func getSnapshotPath(snapshotId string) string {
254+
return filepath.Join(dataRoot, fmt.Sprintf("%s.tgz", snapshotId))
255+
}
256+
254257
// CreateSnapshot uses tar command to create snapshot for hostpath volume. The tar command can quickly create
255258
// archives of entire directories. The host image must have "tar" binaries in /bin, /usr/sbin, or /usr/bin.
256259
func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
@@ -296,8 +299,7 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
296299
snapshotID := uuid.NewUUID().String()
297300
creationTime := ptypes.TimestampNow()
298301
volPath := hostPathVolume.VolPath
299-
filePath := []string{snapshotRoot, "/", snapshotID, ".tgz"}
300-
file := strings.Join(filePath, "")
302+
file := getSnapshotPath(snapshotID)
301303
args := []string{}
302304
if hostPathVolume.VolAccessType == blockAccess {
303305
glog.V(4).Infof("Creating snapshot of Raw Block Mode Volume")
@@ -346,9 +348,8 @@ func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
346348
return nil, err
347349
}
348350
snapshotID := req.GetSnapshotId()
349-
glog.V(4).Infof("deleting volume %s", snapshotID)
350-
pathSlice := []string{snapshotRoot, "/", snapshotID, ".tgz"}
351-
path := strings.Join(pathSlice, "")
351+
glog.V(4).Infof("deleting snapshot %s", snapshotID)
352+
path := getSnapshotPath(snapshotID)
352353
os.RemoveAll(path)
353354
delete(hostPathVolumeSnapshots, snapshotID)
354355
return &csi.DeleteSnapshotResponse{}, nil

pkg/hostpath/hostpath.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"io"
2222
"os"
23+
"path/filepath"
2324

2425
"github.com/golang/glog"
2526
"google.golang.org/grpc/codes"
@@ -70,11 +71,18 @@ type hostPathSnapshot struct {
7071
ReadyToUse bool `json:"readyToUse"`
7172
}
7273

73-
var hostPathVolumes map[string]hostPathVolume
74-
var hostPathVolumeSnapshots map[string]hostPathSnapshot
75-
7674
var (
7775
vendorVersion = "dev"
76+
77+
hostPathVolumes map[string]hostPathVolume
78+
hostPathVolumeSnapshots map[string]hostPathSnapshot
79+
)
80+
81+
const (
82+
// Directory where data for volumes and snapshots are persisted.
83+
// This can be ephemeral within the container or persisted if
84+
// backed by a Pod volume.
85+
dataRoot = "/csi-data-dir"
7886
)
7987

8088
func init() {
@@ -98,6 +106,10 @@ func NewHostPathDriver(driverName, nodeID, endpoint, version string, ephemeral b
98106
vendorVersion = version
99107
}
100108

109+
if err := os.MkdirAll(dataRoot, 0750); err != nil {
110+
return nil, fmt.Errorf("failed to create dataRoot: %v", err)
111+
}
112+
101113
glog.Infof("Driver: %v ", driverName)
102114
glog.Infof("Version: %s", vendorVersion)
103115

@@ -148,7 +160,7 @@ func getSnapshotByName(name string) (hostPathSnapshot, error) {
148160

149161
// getVolumePath returs the canonical path for hostpath volume
150162
func getVolumePath(volID string) string {
151-
return fmt.Sprintf("%s/%s", provisionRoot, volID)
163+
return filepath.Join(dataRoot, volID)
152164
}
153165

154166
// createVolume create the directory for the hostpath volume.

0 commit comments

Comments
 (0)