Skip to content

Commit 154d3f0

Browse files
committed
add AzureEnvironment to AzureManagedControlPlaneSpec
1 parent 4c0d8c7 commit 154d3f0

9 files changed

+68
-18
lines changed

api/v1beta1/azurecluster_default.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"k8s.io/utils/pointer"
23+
"sigs.k8s.io/cluster-api-provider-azure/azure"
2324
)
2425

2526
const (
@@ -41,8 +42,6 @@ const (
4142
DefaultInternalLBIPAddress = "10.0.0.100"
4243
// DefaultOutboundRuleIdleTimeoutInMinutes is the default for IdleTimeoutInMinutes for the load balancer.
4344
DefaultOutboundRuleIdleTimeoutInMinutes = 4
44-
// DefaultAzureCloud is the public cloud that will be used by most users.
45-
DefaultAzureCloud = "AzurePublicCloud"
4645
)
4746

4847
func (c *AzureCluster) setDefaults() {
@@ -69,7 +68,7 @@ func (c *AzureCluster) setResourceGroupDefault() {
6968

7069
func (c *AzureCluster) setAzureEnvironmentDefault() {
7170
if c.Spec.AzureEnvironment == "" {
72-
c.Spec.AzureEnvironment = DefaultAzureCloud
71+
c.Spec.AzureEnvironment = azure.PublicCloudName
7372
}
7473
}
7574

api/v1beta1/azurecluster_default_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"reflect"
2222
"testing"
2323

24+
"sigs.k8s.io/cluster-api-provider-azure/azure"
25+
2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2527
"k8s.io/utils/pointer"
2628
)
@@ -1037,7 +1039,7 @@ func TestAzureEnviromentDefault(t *testing.T) {
10371039
},
10381040
Spec: AzureClusterSpec{
10391041
AzureClusterClassSpec: AzureClusterClassSpec{
1040-
AzureEnvironment: DefaultAzureCloud,
1042+
AzureEnvironment: azure.PublicCloudName,
10411043
},
10421044
},
10431045
},
@@ -1049,7 +1051,7 @@ func TestAzureEnviromentDefault(t *testing.T) {
10491051
},
10501052
Spec: AzureClusterSpec{
10511053
AzureClusterClassSpec: AzureClusterClassSpec{
1052-
AzureEnvironment: DefaultAzureCloud,
1054+
AzureEnvironment: azure.PublicCloudName,
10531055
},
10541056
},
10551057
},
@@ -1059,7 +1061,7 @@ func TestAzureEnviromentDefault(t *testing.T) {
10591061
},
10601062
Spec: AzureClusterSpec{
10611063
AzureClusterClassSpec: AzureClusterClassSpec{
1062-
AzureEnvironment: DefaultAzureCloud,
1064+
AzureEnvironment: azure.PublicCloudName,
10631065
},
10641066
},
10651067
},

api/v1beta1/azuremanagedcontrolplane_default.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"golang.org/x/crypto/ssh"
2424
"k8s.io/utils/pointer"
25+
"sigs.k8s.io/cluster-api-provider-azure/azure"
2526
utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
2627
)
2728

@@ -144,3 +145,9 @@ func (m *AzureManagedControlPlane) setDefaultAutoScalerProfile() {
144145
m.Spec.AutoScalerProfile.SkipNodesWithSystemPods = (*SkipNodesWithSystemPods)(pointer.String(string(SkipNodesWithSystemPodsTrue)))
145146
}
146147
}
148+
149+
func (m *AzureManagedControlPlane) setAzureEnvironmentDefault() {
150+
if m.Spec.AzureEnvironment == "" {
151+
m.Spec.AzureEnvironment = azure.PublicCloudName
152+
}
153+
}

api/v1beta1/azuremanagedcontrolplane_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ type AzureManagedControlPlaneSpec struct {
138138
// AutoscalerProfile is the parameters to be applied to the cluster-autoscaler when enabled
139139
// +optional
140140
AutoScalerProfile *AutoScalerProfile `json:"autoscalerProfile,omitempty"`
141+
142+
// AzureEnvironment is the name of the AzureCloud to be used.
143+
// The default value that would be used by most users is "AzurePublicCloud", other values are:
144+
// - ChinaCloud: "AzureChinaCloud"
145+
// - GermanCloud: "AzureGermanCloud"
146+
// - PublicCloud: "AzurePublicCloud"
147+
// - USGovernmentCloud: "AzureUSGovernmentCloud"
148+
// +optional
149+
AzureEnvironment string `json:"azureEnvironment,omitempty"`
141150
}
142151

143152
// AADProfile - AAD integration managed by AKS.

api/v1beta1/azuremanagedcontrolplane_webhook.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ func (mw *azureManagedControlPlaneWebhook) ValidateUpdate(ctx context.Context, o
193193
allErrs = append(allErrs, err)
194194
}
195195

196+
if !reflect.DeepEqual(m.Spec.AzureEnvironment, old.Spec.AzureEnvironment) {
197+
old.setAzureEnvironmentDefault()
198+
199+
// if it's still not equal, return error.
200+
if !reflect.DeepEqual(m.Spec.AzureEnvironment, old.Spec.AzureEnvironment) {
201+
allErrs = append(allErrs,
202+
field.Invalid(field.NewPath("spec", "AzureEnvironment"),
203+
m.Spec.AzureEnvironment, "field is immutable"),
204+
)
205+
}
206+
}
207+
196208
if old.Spec.AADProfile != nil {
197209
if m.Spec.AADProfile == nil {
198210
allErrs = append(allErrs,

api/v1beta1/types_class.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ limitations under the License.
1616

1717
package v1beta1
1818

19-
import corev1 "k8s.io/api/core/v1"
19+
import (
20+
corev1 "k8s.io/api/core/v1"
21+
"sigs.k8s.io/cluster-api-provider-azure/azure"
22+
)
2023

2124
// AzureClusterClassSpec defines the AzureCluster properties that may be shared across several Azure clusters.
2225
type AzureClusterClassSpec struct {
@@ -134,7 +137,7 @@ type FrontendIPClass struct {
134137
// setDefaults sets default values for AzureClusterClassSpec.
135138
func (acc *AzureClusterClassSpec) setDefaults() {
136139
if acc.AzureEnvironment == "" {
137-
acc.AzureEnvironment = DefaultAzureCloud
140+
acc.AzureEnvironment = azure.PublicCloudName
138141
}
139142
}
140143

azure/scope/managedcontrolplane.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func NewManagedControlPlaneScope(ctx context.Context, params ManagedControlPlane
7373
}
7474

7575
if params.ControlPlane.Spec.IdentityRef == nil {
76-
if err := params.AzureClients.setCredentials(params.ControlPlane.Spec.SubscriptionID, ""); err != nil {
76+
if err := params.AzureClients.setCredentials(params.ControlPlane.Spec.SubscriptionID, params.ControlPlane.Spec.AzureEnvironment); err != nil {
7777
return nil, errors.Wrap(err, "failed to create Azure session")
7878
}
7979
} else {
@@ -82,7 +82,7 @@ func NewManagedControlPlaneScope(ctx context.Context, params ManagedControlPlane
8282
return nil, errors.Wrap(err, "failed to init credentials provider")
8383
}
8484

85-
if err := params.AzureClients.setCredentialsWithProvider(ctx, params.ControlPlane.Spec.SubscriptionID, "", credentialsProvider); err != nil {
85+
if err := params.AzureClients.setCredentialsWithProvider(ctx, params.ControlPlane.Spec.SubscriptionID, params.ControlPlane.Spec.AzureEnvironment, credentialsProvider); err != nil {
8686
return nil, errors.Wrap(err, "failed to configure azure settings and credentials for Identity")
8787
}
8888
}

config/crd/bases/infrastructure.cluster.x-k8s.io_azuremanagedcontrolplanes.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ spec:
217217
- "false"
218218
type: string
219219
type: object
220+
azureEnvironment:
221+
description: 'AzureEnvironment is the name of the AzureCloud to be
222+
used. The default value that would be used by most users is "AzurePublicCloud",
223+
other values are: - ChinaCloud: "AzureChinaCloud" - GermanCloud:
224+
"AzureGermanCloud" - PublicCloud: "AzurePublicCloud" - USGovernmentCloud:
225+
"AzureUSGovernmentCloud"'
226+
type: string
220227
controlPlaneEndpoint:
221228
description: ControlPlaneEndpoint represents the endpoint used to
222229
communicate with the control plane.

controllers/azuremanagedmachinepool_reconciler.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
25+
azureautorest "github.com/Azure/go-autorest/autorest/azure"
2526
"github.com/pkg/errors"
2627
azprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider"
2728
"sigs.k8s.io/cluster-api-provider-azure/azure"
@@ -74,22 +75,32 @@ func (a *AgentPoolVMSSNotFoundError) Is(target error) bool {
7475

7576
// newAzureManagedMachinePoolService populates all the services based on input scope.
7677
func newAzureManagedMachinePoolService(scope *scope.ManagedMachinePoolScope) (*azureManagedMachinePoolService, error) {
77-
var authorizer azure.Authorizer = scope
78-
if scope.Location() != "" {
79-
regionalAuthorizer, err := azure.WithRegionalBaseURI(scope, scope.Location())
80-
if err != nil {
81-
return nil, errors.Wrap(err, "failed to create a regional authorizer")
82-
}
83-
authorizer = regionalAuthorizer
78+
scaleSetAuthorizer, err := scaleSetAuthorizer(scope)
79+
if err != nil {
80+
return nil, err
8481
}
8582

8683
return &azureManagedMachinePoolService{
8784
scope: scope,
8885
agentPoolsSvc: agentpools.New(scope),
89-
scaleSetsSvc: scalesets.NewClient(authorizer),
86+
scaleSetsSvc: scalesets.NewClient(scaleSetAuthorizer),
9087
}, nil
9188
}
9289

90+
// scaleSetAuthorizer takes a scope and determines if a regional authorizer is needed for scale sets
91+
// see https://github.com/kubernetes-sigs/cluster-api-provider-azure/pull/1850 for context on region based authorizer.
92+
func scaleSetAuthorizer(scope *scope.ManagedMachinePoolScope) (azure.Authorizer, error) {
93+
if scope.Location() == "" {
94+
return scope, nil // no location so use default
95+
}
96+
97+
if scope.ControlPlane.Spec.AzureEnvironment == azureautorest.USGovernmentCloud.Name {
98+
return scope, nil // no region support in usgovcloud
99+
}
100+
101+
return azure.WithRegionalBaseURI(scope, scope.Location())
102+
}
103+
93104
// Reconcile reconciles all the services in a predetermined order.
94105
func (s *azureManagedMachinePoolService) Reconcile(ctx context.Context) error {
95106
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.azureManagedMachinePoolService.Reconcile")

0 commit comments

Comments
 (0)