Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Cloud Instance by Region as Fallback #875

Merged
merged 7 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions charts/internal/machineclass/templates/machineclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ nodeTemplate:
zone: {{ $machineClass.nodeTemplate.zone }}
{{- end }}
providerSpec:
{{- if hasKey $machineClass "cloudConfiguration" }}
cloudConfiguration:
{{ toYaml $machineClass.cloudConfiguration | indent 4 }}
{{- end }}
location: {{ $machineClass.region }}
properties:
{{- if $machineClass.securityProfile }}
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/azure/types_cloudprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ type MachineType struct {
// AcceleratedNetworking is an indicator if the machine type supports Azure accelerated networking.
AcceleratedNetworking *bool
}

// The (currently) supported values for the names of clouds to use in the CloudConfiguration.
const (
AzureChinaCloudName string = "AzureChina"
AzureGovCloudName string = "AzureGovernment"
AzurePublicCloudName string = "AzurePublic"
)

// The known prefixes in of region names for the various instances.
var (
AzureGovRegionPrefixes = []string{"usgov", "usdod", "ussec"}
AzureChinaRegionPrefixes = []string{"china"}
)
43 changes: 0 additions & 43 deletions pkg/azure/client/client_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ package client

import (
"crypto/tls"
"fmt"
"math"
"net"
"net/http"
"strings"
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"

"github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure"
)

const (
Expand Down Expand Up @@ -76,45 +72,6 @@ func getAzureClientOpts() *arm.ClientOptions {
}
}

// AzureCloudConfigurationFromCloudConfiguration returns the cloud.Configuration corresponding to the given cloud configuration name (as part of our CloudConfiguration)
func AzureCloudConfigurationFromCloudConfiguration(cloudConfiguration *azure.CloudConfiguration) (cloud.Configuration, error) {
if cloudConfiguration == nil {
return cloud.AzurePublic, nil
}

cloudConfigurationName := cloudConfiguration.Name
switch {
case strings.EqualFold(cloudConfigurationName, "AzurePublic"):
return cloud.AzurePublic, nil
case strings.EqualFold(cloudConfigurationName, "AzureGovernment"):
return cloud.AzureGovernment, nil
case strings.EqualFold(cloudConfigurationName, "AzureChina"):
return cloud.AzureChina, nil

default:
return cloud.Configuration{}, fmt.Errorf("unknown cloud configuration name '%s'", cloudConfigurationName)
}
}

// CloudEnvVarNameFromCloudConfiguration returns the names of env-vars used by the upstream-controllers corresponding to the given cloud configuration name (as part of our CloudConfiguration)
func CloudEnvVarNameFromCloudConfiguration(cloudConfiguration *azure.CloudConfiguration) (string, error) {
if cloudConfiguration == nil {
return "AZUREPUBLICCLOUD", nil
}

cloudConfigurationName := cloudConfiguration.Name
switch {
case strings.EqualFold(cloudConfigurationName, "AzurePublic"):
return "AZUREPUBLICCLOUD", nil
case strings.EqualFold(cloudConfigurationName, "AzureGovernment"):
return "AZUREUSGOVERNMENT", nil
case strings.EqualFold(cloudConfigurationName, "AzureChina"):
return "AZURECHINACLOUD", nil
default:
return "", fmt.Errorf("unknown cloud configuration name '%s'", cloudConfigurationName)
}
}

func getRetriableStatusCode() []int {
return []int{
http.StatusRequestTimeout, // 408
Expand Down
68 changes: 68 additions & 0 deletions pkg/azure/client/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ package client

import (
"errors"
"fmt"
"net/http"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/go-autorest/autorest"
azerrors "github.com/AzureAD/microsoft-authentication-library-for-go/apps/errors"

"github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure"
)

// FilterNotFoundError returns nil for NotFound errors.
Expand Down Expand Up @@ -62,3 +67,66 @@ func IsAzureAPIUnauthorized(err error) bool {
inErr := &azidentity.AuthenticationFailedError{}
return errors.As(err, &inErr)
}

// CloudConfiguration returns a CloudConfiguration for the given input, prioritising the given CloudConfiguration if both inputs are not nil. In essence
// this function unifies both ways to configure the instance to connect to into a single type - our CloudConfiguration.
func CloudConfiguration(cloudConfiguration *azure.CloudConfiguration, region *string) (*azure.CloudConfiguration, error) {
if cloudConfiguration != nil {
return cloudConfiguration, nil
} else if region != nil {
return cloudConfigurationFromRegion(*region), nil
}
return nil, fmt.Errorf("either CloudConfiguration or region must not be nil to determine Azure Cloud configuration")
}

// AzureCloudConfiguration is a convenience function to get the corresponding Azure Cloud configuration (from the Azure SDK) to the given input,
// preferring the cloudConfiguration if both values are not nil.
func AzureCloudConfiguration(cloudConfiguration *azure.CloudConfiguration, region *string) (cloud.Configuration, error) {
cloudConf, err := CloudConfiguration(cloudConfiguration, region)
if err != nil {
return cloud.Configuration{}, err
}
return AzureCloudConfigurationFromCloudConfiguration(cloudConf)
}

// cloudConfigurationFromRegion returns a matching cloudConfiguration corresponding to a well known cloud instance for the given region
func cloudConfigurationFromRegion(region string) *azure.CloudConfiguration {
switch {
case hasAnyPrefix(region, azure.AzureGovRegionPrefixes...):
return &azure.CloudConfiguration{Name: azure.AzureGovCloudName}
case hasAnyPrefix(region, azure.AzureChinaRegionPrefixes...):
return &azure.CloudConfiguration{Name: azure.AzureChinaCloudName}
default:
return &azure.CloudConfiguration{Name: azure.AzurePublicCloudName}
}
}

// AzureCloudConfigurationFromCloudConfiguration returns the cloud.Configuration corresponding to the given cloud configuration name (as part of our CloudConfiguration).
func AzureCloudConfigurationFromCloudConfiguration(cloudConfiguration *azure.CloudConfiguration) (cloud.Configuration, error) {
if cloudConfiguration == nil {
return cloud.AzurePublic, nil
}

cloudConfigurationName := cloudConfiguration.Name
switch {
case strings.EqualFold(cloudConfigurationName, azure.AzurePublicCloudName):
return cloud.AzurePublic, nil
case strings.EqualFold(cloudConfigurationName, azure.AzureGovCloudName):
return cloud.AzureGovernment, nil
case strings.EqualFold(cloudConfigurationName, azure.AzureChinaCloudName):
return cloud.AzureChina, nil

default:
return cloud.Configuration{}, fmt.Errorf("unknown cloud configuration name '%s'", cloudConfigurationName)
}
}

func hasAnyPrefix(s string, prefixes ...string) bool {
lString := strings.ToLower(s)
for _, p := range prefixes {
if strings.HasPrefix(lString, strings.ToLower(p)) {
return true
}
}
return false
}
14 changes: 12 additions & 2 deletions pkg/controller/backupbucket/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (a *actuator) Reconcile(ctx context.Context, _ logr.Logger, backupBucket *e
return err
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(backupConfig.CloudConfiguration)
azCloudConfiguration, err := azureclient.AzureCloudConfiguration(backupConfig.CloudConfiguration, &backupBucket.Spec.Region)
if err != nil {
return err
}
Expand Down Expand Up @@ -98,9 +98,19 @@ func (a *actuator) delete(ctx context.Context, _ logr.Logger, backupBucket *exte
return err
}

var cloudConfiguration *azure.CloudConfiguration
var (
cloudConfiguration *azure.CloudConfiguration
region *string
)

if backupBucket != nil {
cloudConfiguration = backupBucketConfig.CloudConfiguration
region = &backupBucket.Spec.Region
}

cloudConfiguration, err = azureclient.CloudConfiguration(cloudConfiguration, region)
if err != nil {
return err
}

if secret != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/bastion/actuator_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func (a *actuator) Delete(ctx context.Context, log logr.Logger, bastion *extensi
cloudConfiguration = cloudProfile.CloudConfiguration
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(cloudConfiguration)
azCloudConfiguration, err := azureclient.AzureCloudConfiguration(cloudConfiguration, &cluster.Shoot.Spec.Region)

if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/bastion/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, bastion *exte
cloudConfiguration = cloudProfile.CloudConfiguration
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(cloudConfiguration)
azCloudConfiguration, err := azureclient.AzureCloudConfiguration(cloudConfiguration, &opt.Location)
if err != nil {
return err
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func getInfrastructureStatus(ctx context.Context, a *actuator, cluster *extensio
return nil, err
}

if worker == nil || worker.Spec.InfrastructureProviderStatus == nil {
if worker.Spec.InfrastructureProviderStatus == nil {
return nil, errors.New("infrastructure provider status must be not empty for worker")
}

Expand Down
18 changes: 0 additions & 18 deletions pkg/controller/controlplane/valuesprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ import (

"github.com/gardener/gardener-extension-provider-azure/charts"
apisazure "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure"
"github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure/helper"
azureapihelper "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure/helper"
"github.com/gardener/gardener-extension-provider-azure/pkg/azure"
azureclient "github.com/gardener/gardener-extension-provider-azure/pkg/azure/client"
"github.com/gardener/gardener-extension-provider-azure/pkg/features"
"github.com/gardener/gardener-extension-provider-azure/pkg/internal"
)
Expand Down Expand Up @@ -462,24 +460,8 @@ func getConfigChartValues(infraStatus *apisazure.InfrastructureStatus, cp *exten
maxNodes = maxNodes + worker.Maximum
}

cloudProfile, err := helper.CloudProfileConfigFromCluster(cluster)
if err != nil {
return nil, err
}

var cloudConfiguration *apisazure.CloudConfiguration
if cloudProfile != nil {
cloudConfiguration = cloudProfile.CloudConfiguration
}

azureCloudEnvVarName, err := azureclient.CloudEnvVarNameFromCloudConfiguration(cloudConfiguration)
if err != nil {
return nil, err
}

// Collect config chart values.
values := map[string]interface{}{
"cloud": azureCloudEnvVarName,
"tenantId": ca.TenantID,
"subscriptionId": ca.SubscriptionID,
"aadClientId": ca.ClientID,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controlplane/valuesprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ var _ = Describe("ValuesProvider", func() {
cluster = generateCluster(cidr, k8sVersion, false, nil, nil, nil)

ControlPlaneChartValues = map[string]interface{}{
"cloud": "AZUREPUBLICCLOUD",
"tenantId": "TenantID",
"subscriptionId": "SubscriptionID",
"aadClientId": "ClientID",
Expand Down Expand Up @@ -774,6 +773,7 @@ func generateCluster(cidr, k8sVersion string, vpaEnabled bool, shootAnnotations
},
},
},
Region: "eu-west-1a",
Networking: &gardencorev1beta1.Networking{
Pods: &cidr,
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/dnsrecord/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, dns *extensio
return err
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(dnsRecordConfig.CloudConfiguration)
azCloudConfiguration, err := azureclient.AzureCloudConfiguration(dnsRecordConfig.CloudConfiguration, dns.Spec.Region)
if err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +106,8 @@ func (a *actuator) Delete(ctx context.Context, log logr.Logger, dns *extensionsv
return err
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(dnsRecordConfig.CloudConfiguration)
azCloudConfiguration, err := azureclient.AzureCloudConfiguration(dnsRecordConfig.CloudConfiguration, dns.Spec.Region)

if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/dnsrecord/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var _ = Describe("Actuator", func() {
Name: domainName,
RecordType: extensionsv1alpha1.DNSRecordTypeA,
Values: []string{address},
Region: ptr.To("Foobar"),
},
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/controller/infrastructure/flow_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func (f *FlowReconciler) Reconcile(ctx context.Context, infra *extensionsv1alpha
return err
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(cloudProfile.CloudConfiguration)
var cloudConfiguration *azure.CloudConfiguration
if cloudProfile != nil {
cloudConfiguration = cloudProfile.CloudConfiguration
}

azCloudConfiguration, err := azureclient.AzureCloudConfiguration(cloudConfiguration, &cluster.Shoot.Spec.Region)
if err != nil {
return err
}
Expand Down Expand Up @@ -113,7 +118,12 @@ func (f *FlowReconciler) Delete(ctx context.Context, infra *extensionsv1alpha1.I
return err
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(cloudProfile.CloudConfiguration)
var cloudConfiguration *azure.CloudConfiguration
if cloudProfile != nil {
cloudConfiguration = cloudProfile.CloudConfiguration
}

azCloudConfiguration, err := azureclient.AzureCloudConfiguration(cloudConfiguration, &cluster.Shoot.Spec.Region)
if err != nil {
return err
}
Expand Down
18 changes: 1 addition & 17 deletions pkg/controller/infrastructure/terraform_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,12 @@ func NoOpStateInitializer(_ context.Context, _ client.Client, _, _ string, _ *me
return nil
}

func (r *TerraformReconciler) getClientFactory(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, cluster *controller.Cluster) (azureclient.Factory, error) {
cloudProfile, err := helper.CloudProfileConfigFromCluster(cluster)
if err != nil {
return nil, err
}

var cloudConfiguration *azure.CloudConfiguration
if cloudProfile != nil {
cloudConfiguration = cloudProfile.CloudConfiguration
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(cloudConfiguration)
if err != nil {
return nil, err
}

func (r *TerraformReconciler) getClientFactory(ctx context.Context, infra *extensionsv1alpha1.Infrastructure, _ *controller.Cluster) (azureclient.Factory, error) {
return DefaultAzureClientFactoryFunc(
ctx,
r.Client,
infra.Spec.SecretRef,
false,
azureclient.WithCloudConfiguration(azCloudConfiguration),
)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/worker/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (d *delegateFactory) WorkerDelegate(ctx context.Context, worker *extensions
cloudConfiguration = cloudProfile.CloudConfiguration
}

azCloudConfiguration, err := azureclient.AzureCloudConfigurationFromCloudConfiguration(cloudConfiguration)
azCloudConfiguration, err := azureclient.AzureCloudConfiguration(cloudConfiguration, &cluster.Shoot.Spec.Region)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading