Skip to content

Commit 558e8b5

Browse files
authored
Merge pull request #1250 from andyzhangx/fix-cloud-panic
fix: panic when controller does not have cloud config
2 parents 5f8f01c + d71250a commit 558e8b5

File tree

4 files changed

+106
-15
lines changed

4 files changed

+106
-15
lines changed

pkg/blob/azure.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
2626
"github.com/Azure/azure-sdk-for-go/storage"
2727
"github.com/Azure/go-autorest/autorest"
28+
azure2 "github.com/Azure/go-autorest/autorest/azure"
2829
"golang.org/x/net/context"
2930
"k8s.io/client-go/kubernetes"
3031
"k8s.io/klog/v2"
@@ -159,7 +160,7 @@ func (d *Driver) initializeKvClient() (*kv.BaseClient, error) {
159160

160161
// getKeyvaultToken retrieves a new service principal token to access keyvault
161162
func (d *Driver) getKeyvaultToken() (authorizer autorest.Authorizer, err error) {
162-
env := d.cloud.Environment
163+
env := d.getCloudEnvironment()
163164
kvEndPoint := strings.TrimSuffix(env.KeyVaultEndpoint, "/")
164165
servicePrincipalToken, err := providerconfig.GetServicePrincipalToken(&d.cloud.AzureAuthConfig, &env, kvEndPoint)
165166
if err != nil {
@@ -236,3 +237,17 @@ func (d *Driver) updateSubnetServiceEndpoints(ctx context.Context, vnetResourceG
236237

237238
return nil
238239
}
240+
241+
func (d *Driver) getStorageEndPointSuffix() string {
242+
if d.cloud == nil || d.cloud.Environment.StorageEndpointSuffix == "" {
243+
return defaultStorageEndPointSuffix
244+
}
245+
return d.cloud.Environment.StorageEndpointSuffix
246+
}
247+
248+
func (d *Driver) getCloudEnvironment() azure2.Environment {
249+
if d.cloud == nil {
250+
return azure2.PublicCloud
251+
}
252+
return d.cloud.Environment
253+
}

pkg/blob/azure_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,88 @@ func TestUpdateSubnetServiceEndpoints(t *testing.T) {
375375
t.Run(tc.name, tc.testFunc)
376376
}
377377
}
378+
379+
func TestGetStorageEndPointSuffix(t *testing.T) {
380+
d := NewFakeDriver()
381+
382+
ctrl := gomock.NewController(t)
383+
defer ctrl.Finish()
384+
385+
tests := []struct {
386+
name string
387+
cloud *azureprovider.Cloud
388+
expectedSuffix string
389+
}{
390+
{
391+
name: "nil cloud",
392+
cloud: nil,
393+
expectedSuffix: "core.windows.net",
394+
},
395+
{
396+
name: "empty cloud",
397+
cloud: &azureprovider.Cloud{},
398+
expectedSuffix: "core.windows.net",
399+
},
400+
{
401+
name: "cloud with storage endpoint suffix",
402+
cloud: &azureprovider.Cloud{
403+
Environment: azure.Environment{
404+
StorageEndpointSuffix: "suffix",
405+
},
406+
},
407+
expectedSuffix: "suffix",
408+
},
409+
{
410+
name: "public cloud",
411+
cloud: &azureprovider.Cloud{
412+
Environment: azure.PublicCloud,
413+
},
414+
expectedSuffix: "core.windows.net",
415+
},
416+
{
417+
name: "china cloud",
418+
cloud: &azureprovider.Cloud{
419+
Environment: azure.ChinaCloud,
420+
},
421+
expectedSuffix: "core.chinacloudapi.cn",
422+
},
423+
}
424+
425+
for _, test := range tests {
426+
d.cloud = test.cloud
427+
suffix := d.getStorageEndPointSuffix()
428+
assert.Equal(t, test.expectedSuffix, suffix, test.name)
429+
}
430+
}
431+
432+
func TestGetCloudEnvironment(t *testing.T) {
433+
d := NewFakeDriver()
434+
435+
ctrl := gomock.NewController(t)
436+
defer ctrl.Finish()
437+
438+
tests := []struct {
439+
name string
440+
cloud *azureprovider.Cloud
441+
expectedEnv azure.Environment
442+
}{
443+
{
444+
name: "nil cloud",
445+
cloud: nil,
446+
expectedEnv: azure.PublicCloud,
447+
},
448+
{
449+
name: "cloud with environment",
450+
cloud: &azureprovider.Cloud{
451+
Environment: azure.ChinaCloud,
452+
},
453+
expectedEnv: azure.ChinaCloud,
454+
},
455+
}
456+
457+
for _, test := range tests {
458+
d.cloud = test.cloud
459+
env := d.getCloudEnvironment()
460+
assert.Equal(t, test.expectedEnv, env, test.name)
461+
}
462+
}

pkg/blob/controllerserver.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
309309
}
310310

311311
if strings.TrimSpace(storageEndpointSuffix) == "" {
312-
if d.cloud.Environment.StorageEndpointSuffix != "" {
313-
storageEndpointSuffix = d.cloud.Environment.StorageEndpointSuffix
314-
} else {
315-
storageEndpointSuffix = defaultStorageEndPointSuffix
316-
}
312+
storageEndpointSuffix = d.getStorageEndPointSuffix()
317313
}
318314

319315
accountOptions := &azure.AccountOptions{
@@ -558,7 +554,7 @@ func (d *Driver) ValidateVolumeCapabilities(ctx context.Context, req *csi.Valida
558554
var exist bool
559555
secrets := req.GetSecrets()
560556
if len(secrets) > 0 {
561-
container, err := getContainerReference(containerName, secrets, d.cloud.Environment)
557+
container, err := getContainerReference(containerName, secrets, d.getCloudEnvironment())
562558
if err != nil {
563559
return nil, status.Error(codes.Internal, err.Error())
564560
}
@@ -677,7 +673,7 @@ func (d *Driver) CreateBlobContainer(ctx context.Context, subsID, resourceGroupN
677673
return wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
678674
var err error
679675
if len(secrets) > 0 {
680-
container, getErr := getContainerReference(containerName, secrets, d.cloud.Environment)
676+
container, getErr := getContainerReference(containerName, secrets, d.getCloudEnvironment())
681677
if getErr != nil {
682678
return true, getErr
683679
}
@@ -714,7 +710,7 @@ func (d *Driver) DeleteBlobContainer(ctx context.Context, subsID, resourceGroupN
714710
return wait.ExponentialBackoff(d.cloud.RequestBackoff(), func() (bool, error) {
715711
var err error
716712
if len(secrets) > 0 {
717-
container, getErr := getContainerReference(containerName, secrets, d.cloud.Environment)
713+
container, getErr := getContainerReference(containerName, secrets, d.getCloudEnvironment())
718714
if getErr != nil {
719715
return true, getErr
720716
}

pkg/blob/nodeserver.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
3131
"sigs.k8s.io/cloud-provider-azure/pkg/metrics"
3232

33-
"github.com/Azure/azure-sdk-for-go/storage"
3433
"github.com/container-storage-interface/spec/lib/go/csi"
3534

3635
"k8s.io/apimachinery/pkg/util/wait"
@@ -334,11 +333,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
334333
containerName = replaceWithMap(containerName, containerNameReplaceMap)
335334

336335
if strings.TrimSpace(storageEndpointSuffix) == "" {
337-
if d.cloud.Environment.StorageEndpointSuffix != "" {
338-
storageEndpointSuffix = d.cloud.Environment.StorageEndpointSuffix
339-
} else {
340-
storageEndpointSuffix = storage.DefaultBaseURL
341-
}
336+
storageEndpointSuffix = d.getStorageEndPointSuffix()
342337
}
343338

344339
if strings.TrimSpace(serverAddress) == "" {

0 commit comments

Comments
 (0)