Skip to content

Commit

Permalink
fix: panic when controller does not have cloud config
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Feb 19, 2024
1 parent a0e07e7 commit d210700
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 13 deletions.
21 changes: 20 additions & 1 deletion pkg/azurefile/azurefile.go
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage"
"github.com/Azure/azure-storage-file-go/azfile"
azure2 "github.com/Azure/go-autorest/autorest/azure"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/pborman/uuid"
"github.com/rubiojr/go-vhd/vhd"
Expand Down Expand Up @@ -376,7 +377,8 @@ func (d *Driver) Run(endpoint, kubeconfig string, testBool bool) {
klog.V(2).Infof("cloud: %s, location: %s, rg: %s, VnetName: %s, VnetResourceGroup: %s, SubnetName: %s", d.cloud.Cloud, d.cloud.Location, d.cloud.ResourceGroup, d.cloud.VnetName, d.cloud.VnetResourceGroup, d.cloud.SubnetName)

// todo: set backoff from cloud provider config
d.fileClient = newAzureFileClient(&d.cloud.Environment, &retry.Backoff{Steps: 1})
env := d.getCloudEnvironment()
d.fileClient = newAzureFileClient(&env, &retry.Backoff{Steps: 1})

d.mounter, err = mounter.NewSafeMounter(d.enableWindowsHostProcess)
if err != nil {
Expand Down Expand Up @@ -1053,6 +1055,9 @@ func (d *Driver) GetTotalAccountQuota(ctx context.Context, subsID, resourceGroup

// RemoveStorageAccountTag remove tag from storage account
func (d *Driver) RemoveStorageAccountTag(ctx context.Context, subsID, resourceGroup, account, key string) error {
if d.cloud == nil {
return fmt.Errorf("cloud is nil")
}
// search in cache first
cache, err := d.skipMatchingTagCache.Get(account, azcache.CacheReadTypeDefault)
if err != nil {
Expand Down Expand Up @@ -1195,3 +1200,17 @@ func (d *Driver) SetAzureCredentials(ctx context.Context, accountName, accountKe
}
return secretName, err
}

func (d *Driver) getStorageEndPointSuffix() string {
if d.cloud == nil || d.cloud.Environment.StorageEndpointSuffix == "" {
return defaultStorageEndPointSuffix
}
return d.cloud.Environment.StorageEndpointSuffix
}

func (d *Driver) getCloudEnvironment() azure2.Environment {
if d.cloud == nil {
return azure2.PublicCloud
}
return d.cloud.Environment
}
85 changes: 85 additions & 0 deletions pkg/azurefile/azurefile_test.go
Expand Up @@ -1395,3 +1395,88 @@ func TestGetTotalAccountQuota(t *testing.T) {
assert.Equal(t, test.expectedShareNum, fileShareNum, test.name)
}
}

func TestGetStorageEndPointSuffix(t *testing.T) {
d := NewFakeDriver()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []struct {
name string
cloud *azure.Cloud
expectedSuffix string
}{
{
name: "nil cloud",
cloud: nil,
expectedSuffix: "core.windows.net",
},
{
name: "empty cloud",
cloud: &azure.Cloud{},
expectedSuffix: "core.windows.net",
},
{
name: "cloud with storage endpoint suffix",
cloud: &azure.Cloud{
Environment: azure2.Environment{
StorageEndpointSuffix: "suffix",
},
},
expectedSuffix: "suffix",
},
{
name: "public cloud",
cloud: &azure.Cloud{
Environment: azure2.PublicCloud,
},
expectedSuffix: "core.windows.net",
},
{
name: "china cloud",
cloud: &azure.Cloud{
Environment: azure2.ChinaCloud,
},
expectedSuffix: "core.chinacloudapi.cn",
},
}

for _, test := range tests {
d.cloud = test.cloud
suffix := d.getStorageEndPointSuffix()
assert.Equal(t, test.expectedSuffix, suffix, test.name)
}
}

func TestGetCloudEnvironment(t *testing.T) {
d := NewFakeDriver()

ctrl := gomock.NewController(t)
defer ctrl.Finish()

tests := []struct {
name string
cloud *azure.Cloud
expectedEnv azure2.Environment
}{
{
name: "nil cloud",
cloud: nil,
expectedEnv: azure2.PublicCloud,
},
{
name: "cloud with environment",
cloud: &azure.Cloud{
Environment: azure2.ChinaCloud,
},
expectedEnv: azure2.ChinaCloud,
},
}

for _, test := range tests {
d.cloud = test.cloud
env := d.getCloudEnvironment()
assert.Equal(t, test.expectedEnv, env, test.name)
}
}
10 changes: 3 additions & 7 deletions pkg/azurefile/controllerserver.go
Expand Up @@ -407,11 +407,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}

if strings.TrimSpace(storageEndpointSuffix) == "" {
if d.cloud.Environment.StorageEndpointSuffix != "" {
storageEndpointSuffix = d.cloud.Environment.StorageEndpointSuffix
} else {
storageEndpointSuffix = defaultStorageEndPointSuffix
}
storageEndpointSuffix = d.getStorageEndPointSuffix()
}
if d.fileClient != nil {
d.fileClient.StorageEndpointSuffix = storageEndpointSuffix
Expand Down Expand Up @@ -593,7 +589,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
diskSizeBytes := volumehelper.GiBToBytes(requestGiB)
klog.V(2).Infof("begin to create vhd file(%s) size(%d) on share(%s) on account(%s) type(%s) rg(%s) location(%s)",
diskName, diskSizeBytes, validFileShareName, account, sku, resourceGroup, location)
if err := createDisk(ctx, accountName, accountKey, d.cloud.Environment.StorageEndpointSuffix, validFileShareName, diskName, diskSizeBytes); err != nil {
if err := createDisk(ctx, accountName, accountKey, d.getStorageEndPointSuffix(), validFileShareName, diskName, diskSizeBytes); err != nil {
return nil, status.Errorf(codes.Internal, "failed to create VHD disk: %v", err)
}
klog.V(2).Infof("create vhd file(%s) size(%d) on share(%s) on account(%s) type(%s) rg(%s) location(%s) successfully",
Expand Down Expand Up @@ -1076,7 +1072,7 @@ func (d *Driver) getServiceURL(ctx context.Context, sourceVolumeID string, secre
return azfile.ServiceURL{}, "", err
}

u, err := url.Parse(fmt.Sprintf(serviceURLTemplate, accountName, d.cloud.Environment.StorageEndpointSuffix))
u, err := url.Parse(fmt.Sprintf(serviceURLTemplate, accountName, d.getStorageEndPointSuffix()))
if err != nil {
klog.Errorf("parse serviceURLTemplate error: %v", err)
return azfile.ServiceURL{}, "", err
Expand Down
6 changes: 1 addition & 5 deletions pkg/azurefile/nodeserver.go
Expand Up @@ -266,11 +266,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
defer d.volumeLocks.Release(lockKey)

if strings.TrimSpace(storageEndpointSuffix) == "" {
if d.cloud.Environment.StorageEndpointSuffix != "" {
storageEndpointSuffix = d.cloud.Environment.StorageEndpointSuffix
} else {
storageEndpointSuffix = defaultStorageEndPointSuffix
}
storageEndpointSuffix = d.getStorageEndPointSuffix()
}

// replace pv/pvc name namespace metadata in fileShareName
Expand Down

0 comments on commit d210700

Please sign in to comment.