diff --git a/pkg/kubelet/volume_host.go b/pkg/kubelet/volume_host.go index ae7847bc6f..de71e3c4b0 100644 --- a/pkg/kubelet/volume_host.go +++ b/pkg/kubelet/volume_host.go @@ -19,6 +19,7 @@ package kubelet import ( "fmt" "net" + "runtime" "github.com/golang/glog" @@ -91,7 +92,11 @@ func (kvh *kubeletVolumeHost) GetVolumeDevicePluginDir(pluginName string) string } func (kvh *kubeletVolumeHost) GetPodVolumeDir(podUID types.UID, pluginName string, volumeName string) string { - return kvh.kubelet.getPodVolumeDir(podUID, pluginName, volumeName) + dir := kvh.kubelet.getPodVolumeDir(podUID, pluginName, volumeName) + if runtime.GOOS == "windows" { + dir = volume.GetWindowsPath(dir) + } + return dir } func (kvh *kubeletVolumeHost) GetPodVolumeDeviceDir(podUID types.UID, pluginName string) string { diff --git a/pkg/volume/flexvolume/plugin.go b/pkg/volume/flexvolume/plugin.go index 4c201f3a6a..e402e20e70 100644 --- a/pkg/volume/flexvolume/plugin.go +++ b/pkg/volume/flexvolume/plugin.go @@ -19,6 +19,7 @@ package flexvolume import ( "fmt" "path" + "runtime" "strings" "sync" @@ -100,7 +101,11 @@ func (plugin *flexVolumePlugin) Init(host volume.VolumeHost) error { func (plugin *flexVolumePlugin) getExecutable() string { parts := strings.Split(plugin.driverName, "/") execName := parts[len(parts)-1] - return path.Join(plugin.execPath, execName) + execPath := path.Join(plugin.execPath, execName) + if runtime.GOOS == "windows" { + execPath = volume.GetWindowsPath(execPath) + } + return execPath } // Name is part of the volume.VolumePlugin interface. diff --git a/pkg/volume/util.go b/pkg/volume/util.go index 3f71da1b86..98acec8dfe 100644 --- a/pkg/volume/util.go +++ b/pkg/volume/util.go @@ -514,3 +514,12 @@ func AccessModesContainedInAll(indexedModes []v1.PersistentVolumeAccessMode, req } return true } + +// GetWindowsPath get a windows path +func GetWindowsPath(path string) string { + windowsPath := strings.Replace(path, "/", "\\", -1) + if strings.HasPrefix(windowsPath, "\\") { + windowsPath = "c:" + windowsPath + } + return windowsPath +} diff --git a/pkg/volume/util_test.go b/pkg/volume/util_test.go index c902fde2a7..273722a0c3 100644 --- a/pkg/volume/util_test.go +++ b/pkg/volume/util_test.go @@ -869,3 +869,34 @@ func TestValidateZone(t *testing.T) { } } } + +func TestGetWindowsPath(t *testing.T) { + tests := []struct { + path string + expectedPath string + }{ + { + path: `/var/lib/kubelet/pods/146f8428-83e7-11e7-8dd4-000d3a31dac4/volumes/kubernetes.io~disk`, + expectedPath: `c:\var\lib\kubelet\pods\146f8428-83e7-11e7-8dd4-000d3a31dac4\volumes\kubernetes.io~disk`, + }, + { + path: `\var/lib/kubelet/pods/146f8428-83e7-11e7-8dd4-000d3a31dac4\volumes\kubernetes.io~disk`, + expectedPath: `c:\var\lib\kubelet\pods\146f8428-83e7-11e7-8dd4-000d3a31dac4\volumes\kubernetes.io~disk`, + }, + { + path: `/`, + expectedPath: `c:\`, + }, + { + path: ``, + expectedPath: ``, + }, + } + + for _, test := range tests { + result := GetWindowsPath(test.path) + if result != test.expectedPath { + t.Errorf("GetWindowsPath(%v) returned (%v), want (%v)", test.path, result, test.expectedPath) + } + } +}