From e56f0d3639f02f98fa811e7ac76f778143e3da8a Mon Sep 17 00:00:00 2001
From: Konstantinos Angelopoulos
-
BackupConfig is the provider-specific configuration for backup buckets/entries
+BackupBucketConfig is the provider-specific configuration for backup buckets/entries
BackupConfig |
+BackupBucketConfig |
@@ -605,7 +605,7 @@ string
(Appears on: -BackupConfig, +BackupBucketConfig, CloudProfileConfig, DNSRecordConfig) diff --git a/pkg/apis/azure/helper/scheme.go b/pkg/apis/azure/helper/scheme.go index 62b2865f2..025c5e274 100644 --- a/pkg/apis/azure/helper/scheme.go +++ b/pkg/apis/azure/helper/scheme.go @@ -83,8 +83,8 @@ func CloudProfileConfigFromCluster(cluster *controller.Cluster) (*api.CloudProfi } // BackupConfigFromBackupBucket decodes the provider specific config from a given BackupBucket object. -func BackupConfigFromBackupBucket(backupBucket *extensionsv1alpha1.BackupBucket) (api.BackupConfig, error) { - backupConfig := api.BackupConfig{} +func BackupConfigFromBackupBucket(backupBucket *extensionsv1alpha1.BackupBucket) (api.BackupBucketConfig, error) { + backupConfig := api.BackupBucketConfig{} if backupBucket != nil && backupBucket.Spec.ProviderConfig != nil { bucketJson, err := backupBucket.Spec.ProviderConfig.MarshalJSON() if err != nil { @@ -99,8 +99,8 @@ func BackupConfigFromBackupBucket(backupBucket *extensionsv1alpha1.BackupBucket) } // BackupConfigFromBackupEntry decodes the provider specific config from a given BackupEntry object. -func BackupConfigFromBackupEntry(backupEntry *extensionsv1alpha1.BackupEntry) (api.BackupConfig, error) { - backupConfig := api.BackupConfig{} +func BackupConfigFromBackupEntry(backupEntry *extensionsv1alpha1.BackupEntry) (api.BackupBucketConfig, error) { + backupConfig := api.BackupBucketConfig{} if backupEntry != nil && backupEntry.Spec.DefaultSpec.ProviderConfig != nil { entryJson, err := backupEntry.Spec.ProviderConfig.MarshalJSON() if err != nil { diff --git a/pkg/apis/azure/register.go b/pkg/apis/azure/register.go index faf91b146..5b763de1b 100644 --- a/pkg/apis/azure/register.go +++ b/pkg/apis/azure/register.go @@ -42,7 +42,7 @@ func addKnownTypes(scheme *runtime.Scheme) error { &ControlPlaneConfig{}, &WorkerStatus{}, &WorkerConfig{}, - &BackupConfig{}, + &BackupBucketConfig{}, &DNSRecordConfig{}, ) return nil diff --git a/pkg/apis/azure/types_backup.go b/pkg/apis/azure/types_backup.go index eb4f79764..586058af2 100644 --- a/pkg/apis/azure/types_backup.go +++ b/pkg/apis/azure/types_backup.go @@ -10,8 +10,8 @@ import ( // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -// BackupConfig is the provider-specific configuration for backup buckets/entries -type BackupConfig struct { +// BackupBucketConfig is the provider-specific configuration for backup buckets/entries +type BackupBucketConfig struct { metav1.TypeMeta // CloudConfiguration contains config that controls which cloud to connect to. CloudConfiguration *CloudConfiguration diff --git a/pkg/apis/azure/v1alpha1/register.go b/pkg/apis/azure/v1alpha1/register.go index d688ec420..987615f86 100644 --- a/pkg/apis/azure/v1alpha1/register.go +++ b/pkg/apis/azure/v1alpha1/register.go @@ -36,7 +36,7 @@ func addKnownTypes(scheme *runtime.Scheme) error { &ControlPlaneConfig{}, &WorkerConfig{}, &WorkerStatus{}, - &BackupConfig{}, + &BackupBucketConfig{}, &DNSRecordConfig{}, ) return nil diff --git a/pkg/apis/azure/v1alpha1/types_backup.go b/pkg/apis/azure/v1alpha1/types_backup.go index 387dfe766..b829bf437 100644 --- a/pkg/apis/azure/v1alpha1/types_backup.go +++ b/pkg/apis/azure/v1alpha1/types_backup.go @@ -11,8 +11,8 @@ import ( // +genclient // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object -// BackupConfig is the provider-specific configuration for backup buckets/entries -type BackupConfig struct { +// BackupBucketConfig is the provider-specific configuration for backup buckets/entries +type BackupBucketConfig struct { metav1.TypeMeta `json:",inline"` // CloudConfiguration contains config that controls which cloud to connect to. // +optional diff --git a/pkg/apis/azure/v1alpha1/zz_generated.conversion.go b/pkg/apis/azure/v1alpha1/zz_generated.conversion.go index b1bc9742d..d89a84b41 100644 --- a/pkg/apis/azure/v1alpha1/zz_generated.conversion.go +++ b/pkg/apis/azure/v1alpha1/zz_generated.conversion.go @@ -45,13 +45,13 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*BackupConfig)(nil), (*azure.BackupConfig)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1alpha1_BackupConfig_To_azure_BackupConfig(a.(*BackupConfig), b.(*azure.BackupConfig), scope) + if err := s.AddGeneratedConversionFunc((*BackupBucketConfig)(nil), (*azure.BackupBucketConfig)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1alpha1_BackupBucketConfig_To_azure_BackupBucketConfig(a.(*BackupBucketConfig), b.(*azure.BackupBucketConfig), scope) }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*azure.BackupConfig)(nil), (*BackupConfig)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_azure_BackupConfig_To_v1alpha1_BackupConfig(a.(*azure.BackupConfig), b.(*BackupConfig), scope) + if err := s.AddGeneratedConversionFunc((*azure.BackupBucketConfig)(nil), (*BackupBucketConfig)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_azure_BackupBucketConfig_To_v1alpha1_BackupBucketConfig(a.(*azure.BackupBucketConfig), b.(*BackupBucketConfig), scope) }); err != nil { return err } @@ -438,24 +438,24 @@ func Convert_azure_AzureResource_To_v1alpha1_AzureResource(in *azure.AzureResour return autoConvert_azure_AzureResource_To_v1alpha1_AzureResource(in, out, s) } -func autoConvert_v1alpha1_BackupConfig_To_azure_BackupConfig(in *BackupConfig, out *azure.BackupConfig, s conversion.Scope) error { +func autoConvert_v1alpha1_BackupBucketConfig_To_azure_BackupBucketConfig(in *BackupBucketConfig, out *azure.BackupBucketConfig, s conversion.Scope) error { out.CloudConfiguration = (*azure.CloudConfiguration)(unsafe.Pointer(in.CloudConfiguration)) return nil } -// Convert_v1alpha1_BackupConfig_To_azure_BackupConfig is an autogenerated conversion function. -func Convert_v1alpha1_BackupConfig_To_azure_BackupConfig(in *BackupConfig, out *azure.BackupConfig, s conversion.Scope) error { - return autoConvert_v1alpha1_BackupConfig_To_azure_BackupConfig(in, out, s) +// Convert_v1alpha1_BackupBucketConfig_To_azure_BackupBucketConfig is an autogenerated conversion function. +func Convert_v1alpha1_BackupBucketConfig_To_azure_BackupBucketConfig(in *BackupBucketConfig, out *azure.BackupBucketConfig, s conversion.Scope) error { + return autoConvert_v1alpha1_BackupBucketConfig_To_azure_BackupBucketConfig(in, out, s) } -func autoConvert_azure_BackupConfig_To_v1alpha1_BackupConfig(in *azure.BackupConfig, out *BackupConfig, s conversion.Scope) error { +func autoConvert_azure_BackupBucketConfig_To_v1alpha1_BackupBucketConfig(in *azure.BackupBucketConfig, out *BackupBucketConfig, s conversion.Scope) error { out.CloudConfiguration = (*CloudConfiguration)(unsafe.Pointer(in.CloudConfiguration)) return nil } -// Convert_azure_BackupConfig_To_v1alpha1_BackupConfig is an autogenerated conversion function. -func Convert_azure_BackupConfig_To_v1alpha1_BackupConfig(in *azure.BackupConfig, out *BackupConfig, s conversion.Scope) error { - return autoConvert_azure_BackupConfig_To_v1alpha1_BackupConfig(in, out, s) +// Convert_azure_BackupBucketConfig_To_v1alpha1_BackupBucketConfig is an autogenerated conversion function. +func Convert_azure_BackupBucketConfig_To_v1alpha1_BackupBucketConfig(in *azure.BackupBucketConfig, out *BackupBucketConfig, s conversion.Scope) error { + return autoConvert_azure_BackupBucketConfig_To_v1alpha1_BackupBucketConfig(in, out, s) } func autoConvert_v1alpha1_CloudConfiguration_To_azure_CloudConfiguration(in *CloudConfiguration, out *azure.CloudConfiguration, s conversion.Scope) error { diff --git a/pkg/apis/azure/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/azure/v1alpha1/zz_generated.deepcopy.go index 8b3449a78..79edfbd6b 100644 --- a/pkg/apis/azure/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/azure/v1alpha1/zz_generated.deepcopy.go @@ -57,7 +57,7 @@ func (in *AzureResource) DeepCopy() *AzureResource { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *BackupConfig) DeepCopyInto(out *BackupConfig) { +func (in *BackupBucketConfig) DeepCopyInto(out *BackupBucketConfig) { *out = *in out.TypeMeta = in.TypeMeta if in.CloudConfiguration != nil { @@ -68,18 +68,18 @@ func (in *BackupConfig) DeepCopyInto(out *BackupConfig) { return } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupConfig. -func (in *BackupConfig) DeepCopy() *BackupConfig { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupBucketConfig. +func (in *BackupBucketConfig) DeepCopy() *BackupBucketConfig { if in == nil { return nil } - out := new(BackupConfig) + out := new(BackupBucketConfig) in.DeepCopyInto(out) return out } // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *BackupConfig) DeepCopyObject() runtime.Object { +func (in *BackupBucketConfig) DeepCopyObject() runtime.Object { if c := in.DeepCopy(); c != nil { return c } diff --git a/pkg/apis/azure/zz_generated.deepcopy.go b/pkg/apis/azure/zz_generated.deepcopy.go index eb0bf6b75..752f00019 100644 --- a/pkg/apis/azure/zz_generated.deepcopy.go +++ b/pkg/apis/azure/zz_generated.deepcopy.go @@ -57,7 +57,7 @@ func (in *AzureResource) DeepCopy() *AzureResource { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *BackupConfig) DeepCopyInto(out *BackupConfig) { +func (in *BackupBucketConfig) DeepCopyInto(out *BackupBucketConfig) { *out = *in out.TypeMeta = in.TypeMeta if in.CloudConfiguration != nil { @@ -68,18 +68,18 @@ func (in *BackupConfig) DeepCopyInto(out *BackupConfig) { return } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupConfig. -func (in *BackupConfig) DeepCopy() *BackupConfig { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupBucketConfig. +func (in *BackupBucketConfig) DeepCopy() *BackupBucketConfig { if in == nil { return nil } - out := new(BackupConfig) + out := new(BackupBucketConfig) in.DeepCopyInto(out) return out } // DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *BackupConfig) DeepCopyObject() runtime.Object { +func (in *BackupBucketConfig) DeepCopyObject() runtime.Object { if c := in.DeepCopy(); c != nil { return c } diff --git a/pkg/azure/client/factory.go b/pkg/azure/client/factory.go index f7732edbc..dafa2c35f 100644 --- a/pkg/azure/client/factory.go +++ b/pkg/azure/client/factory.go @@ -6,8 +6,6 @@ package client import ( "context" - "fmt" - "strings" "github.com/Azure/azure-sdk-for-go/sdk/azcore" "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm/policy" @@ -15,7 +13,6 @@ import ( corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure" "github.com/gardener/gardener-extension-provider-azure/pkg/internal" ) @@ -166,41 +163,3 @@ func (f azureFactory) ManagedUserIdentity() (ManagedUserIdentity, error) { func (f azureFactory) VirtualMachineImages() (VirtualMachineImages, error) { return NewVirtualMachineImagesClient(f.auth, f.tokenCredential, f.clientOpts) } - -// NewBlobStorageClient reads the secret from the passed reference and return an Azure (blob) storage client. -func NewBlobStorageClient(ctx context.Context, c client.Client, secretRef corev1.SecretReference, cloudConfiguration *azure.CloudConfiguration) (BlobStorage, error) { - var storageDomain string - - // Unfortunately the valid values for storage domains run by Microsoft do not seem to be part of any sdk module. They might be queryable from the cloud configuration, - // but I also haven't been able to find a documented list of proper ServiceName values. - // 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. - // For the time being (until further testing can be done) I assume that the instance that is pointed at by the URL wins so let's keep the old logic for building the - // service URL. - if cloudConfiguration == nil { - storageDomain = "blob.core.windows.net" - } else { - cloudConfigurationName := cloudConfiguration.Name - switch { - case strings.EqualFold(cloudConfigurationName, "AzurePublic"): - storageDomain = "blob.core.windows.net" - case strings.EqualFold(cloudConfigurationName, "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 - storageDomain = "blob.core.usgovcloudapi.net" - case strings.EqualFold(cloudConfigurationName, "AzureChina"): - // This is an educated guess - storageDomain = "blob.core.chinacloudapi.cn" - - default: - return nil, fmt.Errorf("unknown cloud configuration name '%s'", cloudConfigurationName) - } - } - - blobStorageClient, err := newStorageClient(ctx, c, &secretRef, storageDomain) - if err != nil { - return nil, err - } - - return blobStorageClient, nil -} diff --git a/pkg/azure/client/storage.go b/pkg/azure/client/storage.go index 267a6ee99..9717e7129 100644 --- a/pkg/azure/client/storage.go +++ b/pkg/azure/client/storage.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "net/url" + "strings" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob" @@ -17,6 +18,7 @@ import ( "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" + azureapi "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure" "github.com/gardener/gardener-extension-provider-azure/pkg/azure" ) @@ -28,9 +30,46 @@ type BlobStorageClient struct { client *azblob.Client } -// newStorageClient creates a client for an Azure Blob storage by reading auth information from secret reference. Requires passing the storage domain (formerly +// BlobStorageDomainFromCloudConfiguration returns the storage service domain given a known cloudConfiguration. +func BlobStorageDomainFromCloudConfiguration(cloudConfiguration *azureapi.CloudConfiguration) (string, error) { + // Unfortunately the valid values for storage domains run by Microsoft do not seem to be part of any sdk module. They might be queryable from the cloud configuration, + // but I also haven't been able to find a documented list of proper ServiceName values. + // 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 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 + 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 "", 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) { + credentials, err := azblob.NewSharedKeyCredential(storageAccountName, storageAccountKey) + if err != nil { + return nil, fmt.Errorf("failed to create shared key credentials: %v", err) + } + + storageEndpointURL, err := url.Parse(fmt.Sprintf("https://%s.%s", storageAccountName, storageDomain)) + if err != nil { + return nil, fmt.Errorf("failed to parse service url: %v", err) + } + blobclient, err := azblob.NewClientWithSharedKeyCredential(storageEndpointURL.String(), credentials, nil) + 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 // blobstorage host name) to determine the endpoint to build the service url for. -func newStorageClient(ctx context.Context, client client.Client, secretRef *corev1.SecretReference, storageDomain string) (*BlobStorageClient, error) { +func NewStorageClientFromSecretRef(ctx context.Context, client client.Client, secretRef *corev1.SecretReference) (*BlobStorageClient, error) { secret, err := extensionscontroller.GetSecretByReference(ctx, client, secretRef) if err != nil { return nil, err @@ -45,19 +84,12 @@ func newStorageClient(ctx context.Context, client client.Client, secretRef *core return nil, fmt.Errorf("secret %s/%s doesn't have a storage key", secret.Namespace, secret.Name) } - credentials, err := azblob.NewSharedKeyCredential(string(storageAccountName), string(storageAccountKey)) - if err != nil { - return nil, fmt.Errorf("failed to create shared key credentials: %v", err) - } - - storageAccountURL, err := url.Parse(fmt.Sprintf("https://%s.%s", storageAccountName, storageDomain)) - if err != nil { - return nil, fmt.Errorf("failed to parse service url: %v", err) + storageDomain := azure.AzureBlobStorageHostName + if v, ok := secret.Data[azure.StorageDomain]; ok { + storageDomain = string(v) } - blobclient, err := azblob.NewClientWithSharedKeyCredential(storageAccountURL.String(), credentials, nil) - return &BlobStorageClient{blobclient}, err - + return NewStorageClient(ctx, string(storageAccountName), string(storageAccountKey), storageDomain) } // DeleteObjectsWithPrefix deletes the blob objects with the specific |