From 64d9c9558042cca12365046185204fac94c695a8 Mon Sep 17 00:00:00 2001 From: Andy Zhang Date: Thu, 26 Oct 2017 03:05:24 +0800 Subject: [PATCH] merge #52469: add feature: azurefile mount on windows node (#19) --- pkg/util/mount/mount_windows.go | 38 +++++++++++++++++++++++++++-- pkg/volume/azure_file/azure_file.go | 2 +- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/util/mount/mount_windows.go b/pkg/util/mount/mount_windows.go index 63294df9f0daa..2cb890f1b03a8 100644 --- a/pkg/util/mount/mount_windows.go +++ b/pkg/util/mount/mount_windows.go @@ -74,8 +74,28 @@ func (mounter *Mounter) Mount(source string, target string, fstype string, optio return nil } - // empty implementation for mounting azure file - return os.MkdirAll(target, 0755) + // currently only cifs mount is supported + if strings.ToLower(fstype) != "cifs" { + return fmt.Errorf("azureMount: only cifs mount is supported now, fstype: %q, mounting source (%q), target (%q), with options (%q)", fstype, source, target, options) + } + + cmdLine := fmt.Sprintf(`$User = "%s";$PWord = ConvertTo-SecureString -String "%s" -AsPlainText -Force;`+ + `$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord`, + options[0], options[1]) + + driverLetter, err := getAvailableDriveLetter() + if err != nil { + return err + } + bindSource = driverLetter + ":" + cmdLine += fmt.Sprintf(";New-SmbGlobalMapping -LocalPath %s -RemotePath %s -Credential $Credential", bindSource, source) + + if output, err := exec.Command("powershell", "/c", cmdLine).CombinedOutput(); err != nil { + // we don't return error here, even though New-SmbGlobalMapping failed, we still make it successful, + // will return error when Windows 2016 RS3 is ready on azure + glog.Errorf("azureMount: SmbGlobalMapping failed: %v, only SMB mount is supported now, output: %q", err, string(output)) + return os.MkdirAll(target, 0755) + } } if output, err := exec.Command("cmd", "/c", "mklink", "/D", target, bindSource).CombinedOutput(); err != nil { @@ -178,6 +198,20 @@ func normalizeWindowsPath(path string) string { return normalizedPath } +func getAvailableDriveLetter() (string, error) { + cmd := "$used = Get-PSDrive | Select-Object -Expand Name | Where-Object { $_.Length -eq 1 }" + cmd += ";$drive = 67..90 | ForEach-Object { [string][char]$_ } | Where-Object { $used -notcontains $_ } | Select-Object -First 1;$drive" + output, err := exec.Command("powershell", "/c", cmd).CombinedOutput() + if err != nil { + return "", fmt.Errorf("getAvailableDriveLetter failed: %v, output: %q", err, string(output)) + } + + if len(output) == 0 { + return "", fmt.Errorf("azureMount: there is no available drive letter now") + } + return string(output)[:1], nil +} + // ValidateDiskNumber : disk number should be a number in [0, 99] func ValidateDiskNumber(disk string) error { diskNum, err := strconv.Atoi(disk) diff --git a/pkg/volume/azure_file/azure_file.go b/pkg/volume/azure_file/azure_file.go index e418877c0797d..4b9242599827a 100644 --- a/pkg/volume/azure_file/azure_file.go +++ b/pkg/volume/azure_file/azure_file.go @@ -218,7 +218,7 @@ func (b *azureFileMounter) SetUpAt(dir string, fsGroup *int64) error { source = fmt.Sprintf("%s%s%s.file.%s%s%s", osSeparator, osSeparator, accountName, getStorageEndpointSuffix(b.plugin.host.GetCloudProvider()), osSeparator, b.shareName) if runtime.GOOS == "windows" { - mountOptions = []string{accountName, accountKey} + mountOptions = []string{fmt.Sprintf("AZURE\\%s", accountName), accountKey} } else { os.MkdirAll(dir, 0700) // parameters suggested by https://azure.microsoft.com/en-us/documentation/articles/storage-how-to-use-files-linux/