From e307b69dcad491a024f83cc3f2c5baf083007aad Mon Sep 17 00:00:00 2001 From: markrexwinkel Date: Tue, 2 Mar 2021 18:30:03 +0100 Subject: [PATCH] [Fix] Allow absolute paths for volumes on Windows (#510, @markrexwinkel) --- cmd/util/volumes.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/util/volumes.go b/cmd/util/volumes.go index 9749081f..46fa2a56 100644 --- a/cmd/util/volumes.go +++ b/cmd/util/volumes.go @@ -25,12 +25,16 @@ import ( "fmt" "os" "strings" - - "github.com/rancher/k3d/v4/pkg/runtimes" + rt "runtime" + + "github.com/rancher/k3d/v3/pkg/runtimes" log "github.com/sirupsen/logrus" ) +// ValidateVolumeMount checks, if the source of volume mounts exists and if the destination is an absolute path +// - SRC: source directory/file -> tests: must exist +// - DEST: source directory/file -> tests: must be absolute path // ValidateVolumeMount checks, if the source of volume mounts exists and if the destination is an absolute path // - SRC: source directory/file -> tests: must exist // - DEST: source directory/file -> tests: must be absolute path @@ -40,17 +44,29 @@ func ValidateVolumeMount(runtime runtimes.Runtime, volumeMount string) (string, // validate 'SRC[:DEST]' substring split := strings.Split(volumeMount, ":") + // a volume mapping can have 3 parts seperated by a ':' followed by a node filter + // [SOURCE:]DEST[:OPT[,OPT]][@NODEFILTER[;NODEFILTER...]] + // On Windows the source path needs to be an absolute path which means the path starts with + // a drive designator and will also have a ':' in it. So for Windows the maxParts is increased by one. + maxParts := 3 + if rt.GOOS == "windows" { + maxParts++ + } if len(split) < 1 { return "", fmt.Errorf("No volume/path specified") } - if len(split) > 3 { - return "", fmt.Errorf("Invalid volume mount '%s': maximal 2 ':' allowed", volumeMount) + if len(split) > maxParts { + return "", fmt.Errorf("Invalid volume mount '%s': maximal %d ':' allowed", volumeMount, maxParts - 1) } // we only have SRC specified -> DEST = SRC + // On windows the first part of the SRC is the drive letter, so we need to concat the first and second parts to get the path. if len(split) == 1 { src = split[0] dest = src + } else if rt.GOOS == "windows" { + src = split[0] + ":" + split[1] + dest = split[2] } else { src = split[0] dest = split[1]