Skip to content

Commit

Permalink
rename func + introduce consts
Browse files Browse the repository at this point in the history
  • Loading branch information
kon-angelo committed Aug 1, 2024
1 parent e56f0d3 commit 9392ace
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
22 changes: 10 additions & 12 deletions pkg/azure/client/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,21 @@ func BlobStorageDomainFromCloudConfiguration(cloudConfiguration *azureapi.CloudC
// Furthermore, it seems there is still no unified way of specifying the cloud instance to connect to as the domain remains part of the storage account URL while
// the new options _also_ allow configuring the cloud instance.
switch {
case cloudConfiguration == nil:
return "blob.core.windows.net", nil
case strings.EqualFold(cloudConfiguration.Name, "AzurePublic"):
return "blob.core.windows.net", nil
case cloudConfiguration == nil || strings.EqualFold(cloudConfiguration.Name, "AzurePublic"):
return azure.AzureBlobStorageDomain, nil
case strings.EqualFold(cloudConfiguration.Name, "AzureGovernment"):
// Note: This differs from the one mentioned in the docs ("blob.core.govcloudapi.net") but should be the right one.
// ref.: https://github.com/google/go-cloud/blob/be1b4aee38955e1b8cd1c46f8f47fb6f9d820a9b/blob/azureblob/azureblob.go#L162
return "blob.core.usgovcloudapi.net", nil
return azure.AzureUSGovBlobStorageDomain, nil
case strings.EqualFold(cloudConfiguration.Name, "AzureChina"):
// source: https://learn.microsoft.com/en-us/azure/china/resources-developer-guide#check-endpoints-in-azure
return "blob.core.chinacloudapi.cn", nil
return azure.AzureChinaBlobStorageDomain, nil
}
return "", fmt.Errorf("unknown cloud configuration name '%s'", cloudConfiguration.Name)
}

// NewStorageClient creates a blob storage client.
func NewStorageClient(_ context.Context, storageAccountName, storageAccountKey, storageDomain string) (*BlobStorageClient, error) {
// NewBlobStorageClient creates a blob storage client.
func NewBlobStorageClient(_ context.Context, storageAccountName, storageAccountKey, storageDomain string) (*BlobStorageClient, error) {
credentials, err := azblob.NewSharedKeyCredential(storageAccountName, storageAccountKey)
if err != nil {
return nil, fmt.Errorf("failed to create shared key credentials: %v", err)
Expand All @@ -67,9 +65,9 @@ func NewStorageClient(_ context.Context, storageAccountName, storageAccountKey,
return &BlobStorageClient{blobclient}, err
}

// NewStorageClientFromSecretRef creates a client for an Azure Blob storage by reading auth information from secret reference. Requires passing the storage domain (formerly
// NewBlobStorageClientFromSecretRef creates a client for an Azure Blob storage by reading auth information from secret reference. Requires passing the storage domain (formerly
// blobstorage host name) to determine the endpoint to build the service url for.
func NewStorageClientFromSecretRef(ctx context.Context, client client.Client, secretRef *corev1.SecretReference) (*BlobStorageClient, error) {
func NewBlobStorageClientFromSecretRef(ctx context.Context, client client.Client, secretRef *corev1.SecretReference) (*BlobStorageClient, error) {
secret, err := extensionscontroller.GetSecretByReference(ctx, client, secretRef)
if err != nil {
return nil, err
Expand All @@ -84,12 +82,12 @@ func NewStorageClientFromSecretRef(ctx context.Context, client client.Client, se
return nil, fmt.Errorf("secret %s/%s doesn't have a storage key", secret.Namespace, secret.Name)
}

storageDomain := azure.AzureBlobStorageHostName
storageDomain := azure.AzureBlobStorageDomain
if v, ok := secret.Data[azure.StorageDomain]; ok {
storageDomain = string(v)
}

return NewStorageClient(ctx, string(storageAccountName), string(storageAccountKey), storageDomain)
return NewBlobStorageClient(ctx, string(storageAccountName), string(storageAccountKey), storageDomain)
}

// DeleteObjectsWithPrefix deletes the blob objects with the specific <prefix> from <container>.
Expand Down
8 changes: 6 additions & 2 deletions pkg/azure/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ const (
// StorageDomain is a constant for the key in a backup secret that holds the domain for the Azure blob storage service.
StorageDomain = "domain"

// AzureBlobStorageHostName is the host name for azure blob storage service.
AzureBlobStorageHostName = "blob.core.windows.net"
// AzureBlobStorageDomain is the host name for azure blob storage service.
AzureBlobStorageDomain = "blob.core.windows.net"
// AzureChinaBlobStorageDomain is the host name for azure blob storage service for the Chinese regions.
AzureChinaBlobStorageDomain = "blob.core.chinacloudapi.cn"
// AzureUSGovBlobStorageDomain is the host name for azure blob storage service for the US Government regions.
AzureUSGovBlobStorageDomain = "blob.core.usgovcloudapi.net"

// MachineSetTagKey is the name of the infrastructure resource tag for machine sets.
MachineSetTagKey = "machineset.azure.extensions.gardener.cloud"
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/backupbucket/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

var (
// DefaultBlobStorageClient is the default function to get a backupbucket client. Can be overridden for tests.
DefaultBlobStorageClient = azureclient.NewStorageClientFromSecretRef
DefaultBlobStorageClient = azureclient.NewBlobStorageClientFromSecretRef
)

type actuator struct {
Expand Down Expand Up @@ -76,11 +76,11 @@ func (a *actuator) Reconcile(ctx context.Context, _ logr.Logger, backupBucket *e
}
}

storageClient, err := DefaultBlobStorageClient(ctx, a.client, backupBucket.Status.GeneratedSecretRef)
blobStorageClient, err := DefaultBlobStorageClient(ctx, a.client, backupBucket.Status.GeneratedSecretRef)
if err != nil {
return util.DetermineError(err, helper.KnownCodes)
}
return util.DetermineError(storageClient.CreateContainerIfNotExists(ctx, backupBucket.Name), helper.KnownCodes)
return util.DetermineError(blobStorageClient.CreateContainerIfNotExists(ctx, backupBucket.Name), helper.KnownCodes)
}

func (a *actuator) Delete(ctx context.Context, logger logr.Logger, backupBucket *extensionsv1alpha1.BackupBucket) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/backupentry/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var (
// DefaultBlobStorageClient is the default function to get a backupbucket client. Can be overridden for tests.
DefaultBlobStorageClient = azureclient.NewStorageClientFromSecretRef
DefaultBlobStorageClient = azureclient.NewBlobStorageClientFromSecretRef
)

type actuator struct {
Expand Down

0 comments on commit 9392ace

Please sign in to comment.