Merge pull request #54674 from andyzhangx/azurefile-mountoptions

Automatic merge from submit-queue (batch tested with PRs 55254, 55525, 50108, 54674, 55263). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

support mount options in azure file

**What this PR does / why we need it**:
support mount options in azure file

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #37005, #54610

**Special notes for your reviewer**:
@rootfs @karataliu @feiskyer 
By default, the dir_mode and file_mode would be 0700, vers would be 3.0, while if user specify `dir_mode`, `file_mode`, `vers` in storage class in `mountOptions` field(see below), then azure file should use user specified mount options.
```
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: azurefile
provisioner: kubernetes.io/azure-file
mountOptions:
  - dir_mode=0377
  - file_mode=0350
  - vers=2.1
parameters:
  skuName: Standard_LRS
  location: westus2
```

**Release note**:

```
support mount options in azure file
```
/sig azure
This commit is contained in:
Kubernetes Submit Queue
2017-11-17 13:34:14 -08:00
committed by GitHub
3 changed files with 82 additions and 1 deletions

View File

@@ -222,11 +222,12 @@ func (b *azureFileMounter) SetUpAt(dir string, fsGroup *int64) error {
} else {
os.MkdirAll(dir, 0700)
// parameters suggested by https://azure.microsoft.com/en-us/documentation/articles/storage-how-to-use-files-linux/
options := []string{fmt.Sprintf("vers=3.0,username=%s,password=%s,dir_mode=0700,file_mode=0700", accountName, accountKey)}
options := []string{fmt.Sprintf("username=%s,password=%s", accountName, accountKey)}
if b.readOnly {
options = append(options, "ro")
}
mountOptions = volume.JoinMountOptions(b.mountOptions, options)
mountOptions = appendDefaultMountOptions(mountOptions)
}
err = b.mounter.Mount(source, dir, "cifs", mountOptions)

View File

@@ -17,9 +17,11 @@ limitations under the License.
package azure_file
import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"strings"
"testing"
@@ -358,3 +360,38 @@ func TestGetSecretNameAndNamespaceForPV(t *testing.T) {
}
}
func TestAppendDefaultMountOptions(t *testing.T) {
tests := []struct {
options []string
expected []string
}{
{
options: []string{"dir_mode=0777"},
expected: []string{"dir_mode=0777", fmt.Sprintf("%s=%s", fileMode, defaultFileMode), fmt.Sprintf("%s=%s", vers, defaultVers)},
},
{
options: []string{"file_mode=0777"},
expected: []string{"file_mode=0777", fmt.Sprintf("%s=%s", dirMode, defaultDirMode), fmt.Sprintf("%s=%s", vers, defaultVers)},
},
{
options: []string{"vers=2.1"},
expected: []string{"vers=2.1", fmt.Sprintf("%s=%s", fileMode, defaultFileMode), fmt.Sprintf("%s=%s", dirMode, defaultDirMode)},
},
{
options: []string{""},
expected: []string{"", fmt.Sprintf("%s=%s", fileMode, defaultFileMode), fmt.Sprintf("%s=%s", dirMode, defaultDirMode), fmt.Sprintf("%s=%s", vers, defaultVers)},
},
{
options: []string{"file_mode=0777", "dir_mode=0777"},
expected: []string{"file_mode=0777", "dir_mode=0777", fmt.Sprintf("%s=%s", vers, defaultVers)},
},
}
for _, test := range tests {
result := appendDefaultMountOptions(test.options)
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("input: %q, appendDefaultMountOptions result: %q, expected: %q", test.options, result, test.expected)
}
}
}

View File

@@ -18,6 +18,7 @@ package azure_file
import (
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
@@ -25,6 +26,15 @@ import (
"k8s.io/kubernetes/pkg/volume"
)
const (
fileMode = "file_mode"
dirMode = "dir_mode"
vers = "vers"
defaultFileMode = "0700"
defaultDirMode = "0700"
defaultVers = "3.0"
)
// Abstract interface to azure file operations.
type azureUtil interface {
GetAzureCredentials(host volume.VolumeHost, nameSpace, secretName string) (string, string, error)
@@ -84,3 +94,36 @@ func (s *azureSvc) SetAzureCredentials(host volume.VolumeHost, nameSpace, accoun
}
return secretName, err
}
// check whether mountOptions contain file_mode and dir_mode, if not, append default mode
func appendDefaultMountOptions(mountOptions []string) []string {
fileModeFlag := false
dirModeFlag := false
versFlag := false
for _, mountOption := range mountOptions {
if strings.HasPrefix(mountOption, fileMode) {
fileModeFlag = true
}
if strings.HasPrefix(mountOption, dirMode) {
dirModeFlag = true
}
if strings.HasPrefix(mountOption, vers) {
versFlag = true
}
}
allMountOptions := mountOptions
if !fileModeFlag {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", fileMode, defaultFileMode))
}
if !dirModeFlag {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", dirMode, defaultDirMode))
}
if !versFlag {
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", vers, defaultVers))
}
return allMountOptions
}