Skip to content

Commit

Permalink
Create dataRoot if it doesn't exist, consolidate provisionRoot and sn…
Browse files Browse the repository at this point in the history
…apshotRoot
  • Loading branch information
msau42 committed Jul 15, 2019
1 parent 85e803e commit 5d59d11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
17 changes: 9 additions & 8 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"fmt"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

"github.com/golang/protobuf/ptypes"

Expand All @@ -38,8 +38,6 @@ import (

const (
deviceID = "deviceID"
provisionRoot = "/csi-data-dir"
snapshotRoot = "/csi-data-dir"
maxStorageCapacity = tib
)

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

// getSnapshotPath returns the full path to where the snapshot is stored
func getSnapshotPath(snapshotId string) string {
return filepath.Join(dataRoot, fmt.Sprintf("%s.tgz", snapshotId))
}

// CreateSnapshot uses tar command to create snapshot for hostpath volume. The tar command can quickly create
// archives of entire directories. The host image must have "tar" binaries in /bin, /usr/sbin, or /usr/bin.
func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
Expand Down Expand Up @@ -296,8 +299,7 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
snapshotID := uuid.NewUUID().String()
creationTime := ptypes.TimestampNow()
volPath := hostPathVolume.VolPath
filePath := []string{snapshotRoot, "/", snapshotID, ".tgz"}
file := strings.Join(filePath, "")
file := getSnapshotPath(snapshotID)
args := []string{}
if hostPathVolume.VolAccessType == blockAccess {
glog.V(4).Infof("Creating snapshot of Raw Block Mode Volume")
Expand Down Expand Up @@ -346,9 +348,8 @@ func (cs *controllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteS
return nil, err
}
snapshotID := req.GetSnapshotId()
glog.V(4).Infof("deleting volume %s", snapshotID)
pathSlice := []string{snapshotRoot, "/", snapshotID, ".tgz"}
path := strings.Join(pathSlice, "")
glog.V(4).Infof("deleting snapshot %s", snapshotID)
path := getSnapshotPath(snapshotID)
os.RemoveAll(path)
delete(hostPathVolumeSnapshots, snapshotID)
return &csi.DeleteSnapshotResponse{}, nil
Expand Down
20 changes: 16 additions & 4 deletions pkg/hostpath/hostpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/golang/glog"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -69,11 +70,18 @@ type hostPathSnapshot struct {
ReadyToUse bool `json:"readyToUse"`
}

var hostPathVolumes map[string]hostPathVolume
var hostPathVolumeSnapshots map[string]hostPathSnapshot

var (
vendorVersion = "dev"

hostPathVolumes map[string]hostPathVolume
hostPathVolumeSnapshots map[string]hostPathSnapshot
)

const (
// Directory where data for volumes and snapshots are persisted.
// This can be ephemeral within the container or persisted if
// backed by a Pod volume.
dataRoot = "/csi-data-dir"
)

func init() {
Expand All @@ -97,6 +105,10 @@ func NewHostPathDriver(driverName, nodeID, endpoint, version string, ephemeral b
vendorVersion = version
}

if err := os.MkdirAll(dataRoot, 0750); err != nil {
return nil, fmt.Errorf("failed to create dataRoot: %v", err)
}

glog.Infof("Driver: %v ", driverName)
glog.Infof("Version: %s", vendorVersion)

Expand Down Expand Up @@ -147,7 +159,7 @@ func getSnapshotByName(name string) (hostPathSnapshot, error) {

// getVolumePath returs the canonical path for hostpath volume
func getVolumePath(volID string) string {
return fmt.Sprintf("%s/%s", provisionRoot, volID)
return filepath.Join(dataRoot, volID)
}

// createVolume create the directory for the hostpath volume.
Expand Down

0 comments on commit 5d59d11

Please sign in to comment.