Skip to content

Commit

Permalink
Merge pull request kubernetes#48460 from cosmincojocar/azure_file_clo…
Browse files Browse the repository at this point in the history
…ud_environment

Automatic merge from submit-queue (batch tested with PRs 49218, 48253, 48967, 48460, 49230)

Fix the Azure file to work within different cloud environments

**What this PR does / why we need it**:
Fix the Azure file plugin to work within different cloud environments.

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

cc @rootfs @brendandburns
  • Loading branch information
Kubernetes Submit Queue authored Jul 20, 2017
2 parents ecadada + 95cf81f commit db1956b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/volume/azure_file/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ go_test(
library = ":go_default_library",
tags = ["automanaged"],
deps = [
"//pkg/cloudprovider/providers/azure:go_default_library",
"//pkg/cloudprovider/providers/fake:go_default_library",
"//pkg/util/mount:go_default_library",
"//pkg/volume:go_default_library",
"//pkg/volume/testing:go_default_library",
Expand Down
30 changes: 26 additions & 4 deletions pkg/volume/azure_file/azure_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import (
"k8s.io/kubernetes/pkg/volume"

"github.com/golang/glog"
"k8s.io/kubernetes/pkg/cloudprovider"
"k8s.io/kubernetes/pkg/cloudprovider/providers/azure"
"k8s.io/kubernetes/pkg/volume/util"
)

// This is the primary entrypoint for volume plugins.
// ProbeVolumePlugins is the primary endpoint for volume plugins
func ProbeVolumePlugins() []volume.VolumePlugin {
return []volume.VolumePlugin{&azureFilePlugin{nil}}
}
Expand Down Expand Up @@ -206,10 +208,11 @@ func (b *azureFileMounter) SetUpAt(dir string, fsGroup *int64) error {
if accountName, accountKey, err = b.util.GetAzureCredentials(b.plugin.host, b.pod.Namespace, b.secretName); err != nil {
return err
}
os.MkdirAll(dir, 0750)
source := fmt.Sprintf("//%s.file.core.windows.net/%s", accountName, b.shareName)
os.MkdirAll(dir, 0700)

source := fmt.Sprintf("//%s.file.%s/%s", accountName, getStorageEndpointSuffix(b.plugin.host.GetCloudProvider()), b.shareName)
// 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=0777,file_mode=0777", accountName, accountKey)}
options := []string{fmt.Sprintf("vers=3.0,username=%s,password=%s,dir_mode=0700,file_mode=0700", accountName, accountKey)}
if b.readOnly {
options = append(options, "ro")
}
Expand Down Expand Up @@ -268,3 +271,22 @@ func getVolumeSource(

return nil, false, fmt.Errorf("Spec does not reference an AzureFile volume type")
}

func getAzureCloud(cloudProvider cloudprovider.Interface) (*azure.Cloud, error) {
azure, ok := cloudProvider.(*azure.Cloud)
if !ok || azure == nil {
return nil, fmt.Errorf("Failed to get Azure Cloud Provider. GetCloudProvider returned %v instead", cloudProvider)
}

return azure, nil
}

func getStorageEndpointSuffix(cloudprovider cloudprovider.Interface) string {
const publicCloudStorageEndpointSuffix = "core.windows.net"
azure, err := getAzureCloud(cloudprovider)
if err != nil {
glog.Warningf("No Azure cloud provider found. Using the Azure public cloud endpoint: %s", publicCloudStorageEndpointSuffix)
return publicCloudStorageEndpointSuffix
}
return azure.Environment.StorageEndpointSuffix
}
46 changes: 44 additions & 2 deletions pkg/volume/azure_file/azure_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"testing"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/kubernetes/pkg/cloudprovider/providers/azure"
fakecloud "k8s.io/kubernetes/pkg/cloudprovider/providers/fake"
"k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/volume"
volumetest "k8s.io/kubernetes/pkg/volume/testing"
Expand Down Expand Up @@ -82,14 +85,53 @@ func contains(modes []v1.PersistentVolumeAccessMode, mode v1.PersistentVolumeAcc
return false
}

func TestPlugin(t *testing.T) {
func getAzureTestCloud(t *testing.T) *azure.Cloud {
config := `{
"aadClientId": "--aad-client-id--",
"aadClientSecret": "--aad-client-secret--"
}`
configReader := strings.NewReader(config)
cloud, err := azure.NewCloud(configReader)
if err != nil {
t.Error(err)
}
azureCloud, ok := cloud.(*azure.Cloud)
if !ok {
t.Error("NewCloud returned incorrect type")
}
return azureCloud
}

func getTestTempDir(t *testing.T) string {
tmpDir, err := ioutil.TempDir(os.TempDir(), "azurefileTest")
if err != nil {
t.Fatalf("can't make a temp dir: %v", err)
}
return tmpDir
}

func TestPluginAzureCloudProvider(t *testing.T) {
tmpDir := getTestTempDir(t)
defer os.RemoveAll(tmpDir)
testPlugin(t, tmpDir, volumetest.NewFakeVolumeHostWithCloudProvider(tmpDir, nil, nil, getAzureTestCloud(t)))
}

func TestPluginWithoutCloudProvider(t *testing.T) {
tmpDir := getTestTempDir(t)
defer os.RemoveAll(tmpDir)
testPlugin(t, tmpDir, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
}

func TestPluginWithOtherCloudProvider(t *testing.T) {
tmpDir := getTestTempDir(t)
defer os.RemoveAll(tmpDir)
cloud := &fakecloud.FakeCloud{}
testPlugin(t, tmpDir, volumetest.NewFakeVolumeHostWithCloudProvider(tmpDir, nil, nil, cloud))
}

func testPlugin(t *testing.T, tmpDir string, volumeHost volume.VolumeHost) {
plugMgr := volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
plugMgr.InitPlugins(ProbeVolumePlugins(), volumeHost)

plug, err := plugMgr.FindPluginByName("kubernetes.io/azure-file")
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion pkg/volume/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ type fakeVolumeHost struct {
}

func NewFakeVolumeHost(rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin) *fakeVolumeHost {
host := &fakeVolumeHost{rootDir: rootDir, kubeClient: kubeClient, cloud: nil}
return newFakeVolumeHost(rootDir, kubeClient, plugins, nil)
}

func NewFakeVolumeHostWithCloudProvider(rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin, cloud cloudprovider.Interface) *fakeVolumeHost {
return newFakeVolumeHost(rootDir, kubeClient, plugins, cloud)
}

func newFakeVolumeHost(rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin, cloud cloudprovider.Interface) *fakeVolumeHost {
host := &fakeVolumeHost{rootDir: rootDir, kubeClient: kubeClient, cloud: cloud}
host.mounter = &mount.FakeMounter{}
host.writer = &io.StdWriter{}
host.pluginMgr.InitPlugins(plugins, host)
Expand Down

0 comments on commit db1956b

Please sign in to comment.