From badefda861d180b670943f4d19a421f320ed7350 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Fri, 27 Oct 2017 05:32:46 +0000 Subject: [PATCH] support mount options in azure file use JoinMountOptions func add a new test case move const vars fix fmt issue --- pkg/volume/azure_file/azure_file.go | 3 ++- pkg/volume/azure_file/azure_file_test.go | 33 +++++++++++++++++++++++ pkg/volume/azure_file/azure_util.go | 34 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/pkg/volume/azure_file/azure_file.go b/pkg/volume/azure_file/azure_file.go index 4b924259982..21075784bde 100644 --- a/pkg/volume/azure_file/azure_file.go +++ b/pkg/volume/azure_file/azure_file.go @@ -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("vers=3.0,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) diff --git a/pkg/volume/azure_file/azure_file_test.go b/pkg/volume/azure_file/azure_file_test.go index c9e17788305..57ff2537e0c 100644 --- a/pkg/volume/azure_file/azure_file_test.go +++ b/pkg/volume/azure_file/azure_file_test.go @@ -17,9 +17,11 @@ limitations under the License. package azure_file import ( + "fmt" "io/ioutil" "os" "path" + "reflect" "strings" "testing" @@ -358,3 +360,34 @@ 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", fileModeName, defaultFileMode)}, + }, + { + options: []string{"file_mode=0777"}, + expected: []string{"file_mode=0777", fmt.Sprintf("%s=%s", dirModeName, defaultDirMode)}, + }, + { + options: []string{""}, + expected: []string{"", fmt.Sprintf("%s=%s", fileModeName, defaultFileMode), fmt.Sprintf("%s=%s", dirModeName, defaultDirMode)}, + }, + { + options: []string{"file_mode=0777", "dir_mode=0777"}, + expected: []string{"file_mode=0777", "dir_mode=0777"}, + }, + } + + 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) + } + } +} diff --git a/pkg/volume/azure_file/azure_util.go b/pkg/volume/azure_file/azure_util.go index 41caf86deff..b414b1ca965 100644 --- a/pkg/volume/azure_file/azure_util.go +++ b/pkg/volume/azure_file/azure_util.go @@ -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,13 @@ import ( "k8s.io/kubernetes/pkg/volume" ) +const ( + fileModeName = "file_mode" + dirModeName = "dir_mode" + defaultFileMode = "700" + defaultDirMode = "700" +) + // Abstract interface to azure file operations. type azureUtil interface { GetAzureCredentials(host volume.VolumeHost, nameSpace, secretName string) (string, string, error) @@ -84,3 +92,29 @@ func (s *azureSvc) SetAzureCredentials(host volume.VolumeHost, nameSpace, accoun } return secretName, err } + +// check whether mountOptions contains file_mode and dir_mode, if not, append default mode +func appendDefaultMountOptions(mountOptions []string) []string { + fileModeFlag := false + dirModeFlag := false + + for _, mountOption := range mountOptions { + if strings.HasPrefix(mountOption, fileModeName) { + fileModeFlag = true + } + if strings.HasPrefix(mountOption, dirModeName) { + dirModeFlag = true + } + } + + allMountOptions := mountOptions + if !fileModeFlag { + allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", fileModeName, defaultFileMode)) + } + + if !dirModeFlag { + allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", dirModeName, defaultDirMode)) + } + + return allMountOptions +}