mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #48460 from cosmincojocar/azure_file_cloud_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 #46081 cc @rootfs @brendandburns
This commit is contained in:
commit
db1956b876
@ -40,6 +40,8 @@ go_test(
|
|||||||
library = ":go_default_library",
|
library = ":go_default_library",
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
deps = [
|
deps = [
|
||||||
|
"//pkg/cloudprovider/providers/azure:go_default_library",
|
||||||
|
"//pkg/cloudprovider/providers/fake:go_default_library",
|
||||||
"//pkg/util/mount:go_default_library",
|
"//pkg/util/mount:go_default_library",
|
||||||
"//pkg/volume:go_default_library",
|
"//pkg/volume:go_default_library",
|
||||||
"//pkg/volume/testing:go_default_library",
|
"//pkg/volume/testing:go_default_library",
|
||||||
|
@ -28,10 +28,12 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/volume"
|
"k8s.io/kubernetes/pkg/volume"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||||
|
"k8s.io/kubernetes/pkg/cloudprovider/providers/azure"
|
||||||
"k8s.io/kubernetes/pkg/volume/util"
|
"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 {
|
func ProbeVolumePlugins() []volume.VolumePlugin {
|
||||||
return []volume.VolumePlugin{&azureFilePlugin{nil}}
|
return []volume.VolumePlugin{&azureFilePlugin{nil}}
|
||||||
}
|
}
|
||||||
@ -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 {
|
if accountName, accountKey, err = b.util.GetAzureCredentials(b.plugin.host, b.pod.Namespace, b.secretName); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
os.MkdirAll(dir, 0750)
|
os.MkdirAll(dir, 0700)
|
||||||
source := fmt.Sprintf("//%s.file.core.windows.net/%s", accountName, b.shareName)
|
|
||||||
|
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/
|
// 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 {
|
if b.readOnly {
|
||||||
options = append(options, "ro")
|
options = append(options, "ro")
|
||||||
}
|
}
|
||||||
@ -268,3 +271,22 @@ func getVolumeSource(
|
|||||||
|
|
||||||
return nil, false, fmt.Errorf("Spec does not reference an AzureFile volume type")
|
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
|
||||||
|
}
|
||||||
|
@ -20,12 +20,15 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/client-go/kubernetes/fake"
|
"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/util/mount"
|
||||||
"k8s.io/kubernetes/pkg/volume"
|
"k8s.io/kubernetes/pkg/volume"
|
||||||
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
||||||
@ -82,14 +85,53 @@ func contains(modes []v1.PersistentVolumeAccessMode, mode v1.PersistentVolumeAcc
|
|||||||
return false
|
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")
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "azurefileTest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't make a temp dir: %v", err)
|
t.Fatalf("can't make a temp dir: %v", err)
|
||||||
}
|
}
|
||||||
|
return tmpDir
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPluginAzureCloudProvider(t *testing.T) {
|
||||||
|
tmpDir := getTestTempDir(t)
|
||||||
defer os.RemoveAll(tmpDir)
|
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 := volume.VolumePluginMgr{}
|
||||||
plugMgr.InitPlugins(ProbeVolumePlugins(), volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
|
plugMgr.InitPlugins(ProbeVolumePlugins(), volumeHost)
|
||||||
|
|
||||||
plug, err := plugMgr.FindPluginByName("kubernetes.io/azure-file")
|
plug, err := plugMgr.FindPluginByName("kubernetes.io/azure-file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -53,7 +53,15 @@ type fakeVolumeHost struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewFakeVolumeHost(rootDir string, kubeClient clientset.Interface, plugins []VolumePlugin) *fakeVolumeHost {
|
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.mounter = &mount.FakeMounter{}
|
||||||
host.writer = &io.StdWriter{}
|
host.writer = &io.StdWriter{}
|
||||||
host.pluginMgr.InitPlugins(plugins, host)
|
host.pluginMgr.InitPlugins(plugins, host)
|
||||||
|
Loading…
Reference in New Issue
Block a user