Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#5512 from Lyndon-Li/issue-fix-5505
Browse files Browse the repository at this point in the history
Issue fix 5505
  • Loading branch information
blackpiglet authored Nov 1, 2022
2 parents ae1e42c + 3571339 commit 5db3da5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/5512-lyndon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue 5505: the pod volume backups/restores except the first one fail under the kopia path if "AZURE_CLOUD_NAME" is specified
24 changes: 18 additions & 6 deletions pkg/repository/config/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"context"
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -211,13 +212,15 @@ func getRequiredValues(getValue func(string) string, keys ...string) error {
}

// GetAzureStorageDomain gets the Azure storage domain required by a Azure blob connection,
// if the provided config doean't have the value, get it from system's environment variables
func GetAzureStorageDomain(config map[string]string) string {
if domain, exist := config[storageDomainConfigKey]; exist {
return domain
} else {
return os.Getenv(cloudNameEnvVar)
// if the provided credential file doesn't have the value, get it from system's environment variables
func GetAzureStorageDomain(config map[string]string) (string, error) {
credentialsFile := selectCredentialsFile(config)

if err := loadCredentialsIntoEnv(credentialsFile); err != nil {
return "", err
}

return getStorageDomainFromCloudName(os.Getenv(cloudNameEnvVar))
}

func GetAzureCredentials(config map[string]string) (string, string, error) {
Expand All @@ -228,3 +231,12 @@ func GetAzureCredentials(config map[string]string) (string, string, error) {

return config[storageAccountConfigKey], storageAccountKey, nil
}

func getStorageDomainFromCloudName(cloudName string) (string, error) {
env, err := parseAzureEnvironment(cloudName)
if err != nil {
return "", errors.Wrapf(err, "unable to parse azure env from cloud name %s", cloudName)
}

return fmt.Sprintf("blob.%s", env.StorageEndpointSuffix), nil
}
46 changes: 46 additions & 0 deletions pkg/repository/config/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -86,3 +87,48 @@ func TestSelectCredentialsFile(t *testing.T) {
})
}
}

func TestGetStorageDomainFromCloudName(t *testing.T) {
testCases := []struct {
name string
cloudName string
expected string
expectedErr string
}{
{
name: "get azure env fail",
cloudName: "fake-cloud",
expectedErr: "unable to parse azure env from cloud name fake-cloud: autorest/azure: There is no cloud environment matching the name \"FAKE-CLOUD\"",
},
{
name: "cloud name is empty",
cloudName: "",
expected: "blob.core.windows.net",
},
{
name: "azure public cloud",
cloudName: "AzurePublicCloud",
expected: "blob.core.windows.net",
},
{

name: "azure China cloud",
cloudName: "AzureChinaCloud",
expected: "blob.core.chinacloudapi.cn",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
domain, err := getStorageDomainFromCloudName(tc.cloudName)

require.Equal(t, tc.expected, domain)

if tc.expectedErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedErr)
assert.Empty(t, domain)
}
})
}
}
7 changes: 6 additions & 1 deletion pkg/repository/provider/unified_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,12 @@ func getStorageVariables(backupLocation *velerov1api.BackupStorageLocation, repo
result[udmrepo.StoreOptionS3DisableTlsVerify] = config["insecureSkipTLSVerify"]
result[udmrepo.StoreOptionS3DisableTls] = strconv.FormatBool(disableTls)
} else if backendType == repoconfig.AzureBackend {
result[udmrepo.StoreOptionAzureDomain] = getAzureStorageDomain(config)
domain, err := getAzureStorageDomain(config)
if err != nil {
return map[string]string{}, errors.Wrapf(err, "error to get azure storage domain")
}

result[udmrepo.StoreOptionAzureDomain] = domain
}

result[udmrepo.StoreOptionOssBucket] = bucket
Expand Down
54 changes: 39 additions & 15 deletions pkg/repository/provider/unified_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestGetStorageVariables(t *testing.T) {
repoName string
repoBackend string
getS3BucketRegion func(string) (string, error)
getAzureStorageDomain func(map[string]string) string
getAzureStorageDomain func(map[string]string) (string, error)
expected map[string]string
expectedErr string
}{
Expand Down Expand Up @@ -366,17 +366,42 @@ func TestGetStorageVariables(t *testing.T) {
"skipTLSVerify": "false",
},
},
{
name: "azure, getAzureStorageDomain fail",
backupLocation: velerov1api.BackupStorageLocation{
Spec: velerov1api.BackupStorageLocationSpec{
Provider: "velero.io/azure",
Config: map[string]string{
"bucket": "fake-bucket-config",
"prefix": "fake-prefix-config",
"region": "fake-region",
"fspath": "",
},
StorageType: velerov1api.StorageType{
ObjectStorage: &velerov1api.ObjectStorageLocation{
Bucket: "fake-bucket-object-store",
Prefix: "fake-prefix-object-store",
},
},
},
},
getAzureStorageDomain: func(config map[string]string) (string, error) {
return "", errors.New("fake error")
},
repoBackend: "fake-repo-type",
expected: map[string]string{},
expectedErr: "error to get azure storage domain: fake error",
},
{
name: "azure, ObjectStorage section exists in BSL",
backupLocation: velerov1api.BackupStorageLocation{
Spec: velerov1api.BackupStorageLocationSpec{
Provider: "velero.io/azure",
Config: map[string]string{
"bucket": "fake-bucket-config",
"prefix": "fake-prefix-config",
"region": "fake-region",
"fspath": "",
"storageDomain": "fake-domain",
"bucket": "fake-bucket-config",
"prefix": "fake-prefix-config",
"region": "fake-region",
"fspath": "",
},
StorageType: velerov1api.StorageType{
ObjectStorage: &velerov1api.ObjectStorageLocation{
Expand All @@ -386,8 +411,8 @@ func TestGetStorageVariables(t *testing.T) {
},
},
},
getAzureStorageDomain: func(config map[string]string) string {
return config["storageDomain"]
getAzureStorageDomain: func(config map[string]string) (string, error) {
return "fake-domain", nil
},
repoBackend: "fake-repo-type",
expected: map[string]string{
Expand All @@ -404,18 +429,17 @@ func TestGetStorageVariables(t *testing.T) {
Spec: velerov1api.BackupStorageLocationSpec{
Provider: "velero.io/azure",
Config: map[string]string{
"bucket": "fake-bucket",
"prefix": "fake-prefix",
"region": "fake-region",
"fspath": "",
"storageDomain": "fake-domain",
"bucket": "fake-bucket",
"prefix": "fake-prefix",
"region": "fake-region",
"fspath": "",
},
},
},
repoName: "//fake-name//",
repoBackend: "fake-repo-type",
getAzureStorageDomain: func(config map[string]string) string {
return config["storageDomain"]
getAzureStorageDomain: func(config map[string]string) (string, error) {
return "fake-domain", nil
},
expected: map[string]string{
"bucket": "fake-bucket",
Expand Down

0 comments on commit 5db3da5

Please sign in to comment.