Skip to content

Commit f73a068

Browse files
Cherrypicked upstream azure env changes for managed cluster aks on gov cloud (#105)
* added AzureEnvironment to AzureManagedControlPlaneSpec * PCP-1159: added default function and minor lint fixes ---------
1 parent 55cf967 commit f73a068

File tree

11 files changed

+74
-21
lines changed

11 files changed

+74
-21
lines changed

api/v1beta1/types_class.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ 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+
)
2022

2123
// AzureClusterClassSpec defines the AzureCluster properties that may be shared across several Azure clusters.
2224
type AzureClusterClassSpec struct {

azure/defaults.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ const (
3131
DefaultUserName = "capi"
3232
// DefaultAKSUserName is the default username for a created AKS VM.
3333
DefaultAKSUserName = "azureuser"
34+
// PublicCloudName is the name of the Azure public cloud.
35+
PublicCloudName = "AzurePublicCloud"
36+
// ChinaCloudName is the name of the Azure China cloud.
37+
ChinaCloudName = "AzureChinaCloud"
38+
// USGovernmentCloudName is the name of the Azure US Government cloud.
39+
USGovernmentCloudName = "AzureUSGovernmentCloud"
3440
)
3541

3642
const (

azure/scope/managedcontrolplane.go

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

7272
if params.ControlPlane.Spec.IdentityRef == nil {
73-
if err := params.AzureClients.setCredentials(params.ControlPlane.Spec.SubscriptionID, ""); err != nil {
73+
if err := params.AzureClients.setCredentials(params.ControlPlane.Spec.SubscriptionID, params.ControlPlane.Spec.AzureEnvironment); err != nil {
7474
return nil, errors.Wrap(err, "failed to create Azure session")
7575
}
7676
} else {
@@ -79,7 +79,7 @@ func NewManagedControlPlaneScope(ctx context.Context, params ManagedControlPlane
7979
return nil, errors.Wrap(err, "failed to init credentials provider")
8080
}
8181

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,12 @@ spec:
593593
required:
594594
- upgradeChannel
595595
type: object
596+
azureEnvironment:
597+
description: 'AzureEnvironment is the name of the AzureCloud to be
598+
used. The default value that would be used by most users is "AzurePublicCloud",
599+
other values are: - ChinaCloud: "AzureChinaCloud" - PublicCloud:
600+
"AzurePublicCloud" - USGovernmentCloud: "AzureUSGovernmentCloud"'
601+
type: string
596602
controlPlaneEndpoint:
597603
description: ControlPlaneEndpoint represents the endpoint used to
598604
communicate with the control plane.

config/default/manager_image_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ spec:
88
spec:
99
containers:
1010
# Change the value of image field below to your controller image URL
11-
- image: gcr.io/spectro-dev-public/ubuntu/release/cluster-api-azure-controller:v1.3.2-spectro-4.0.0-dev
11+
- image: gcr.io/spectro-dev-public/shubham/release/cluster-api-azure-controller:v1.3.2-spectro-4.0.0-dev
1212
name: manager

exp/api/v1alpha3/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/api/v1alpha4/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/api/v1beta1/azuremanagedcontrolplane_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ type AzureManagedControlPlaneSpec struct {
168168
// +optional
169169
AutoUpgradeProfile *ManagedClusterAutoUpgradeProfile `json:"autoUpgradeProfile,omitempty"`
170170

171+
// AzureEnvironment is the name of the AzureCloud to be used.
172+
// The default value that would be used by most users is "AzurePublicCloud", other values are:
173+
// - ChinaCloud: "AzureChinaCloud"
174+
// - PublicCloud: "AzurePublicCloud"
175+
// - USGovernmentCloud: "AzureUSGovernmentCloud"
176+
// +optional
177+
AzureEnvironment string `json:"azureEnvironment,omitempty"`
178+
171179
// DisableLocalAccounts - If set to true, getting static credential will be disabled for this cluster. Expected to only be used for AAD clusters.
172180
// +optional
173181
DisableLocalAccounts *bool `json:"disableLocalAccounts,omitempty"`

exp/api/v1beta1/azuremanagedcontrolplane_webhook.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/apimachinery/pkg/util/validation/field"
3333
"k8s.io/utils/ptr"
3434
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
35+
"sigs.k8s.io/cluster-api-provider-azure/azure"
3536
"sigs.k8s.io/cluster-api-provider-azure/util/versions"
3637
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3738
ctrl "sigs.k8s.io/controller-runtime"
@@ -77,6 +78,9 @@ func (m *AzureManagedControlPlane) Default(_ client.Client) {
7778
m.setDefaultVirtualNetwork()
7879
m.setDefaultSubnet()
7980
m.setDefaultSku()
81+
if m.Spec.AzureEnvironment == "" {
82+
m.Spec.AzureEnvironment = azure.PublicCloudName
83+
}
8084
}
8185

8286
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcontrolplane,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedcontrolplanes,versions=v1beta1,name=validation.azuremanagedcontrolplanes.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
@@ -253,6 +257,25 @@ func (m *AzureManagedControlPlane) ValidateUpdate(oldRaw runtime.Object, client
253257
}
254258
}
255259

260+
if old.Spec.AzureEnvironment != "" {
261+
// Prevent AzureEnvironment modification if it was already set to some value
262+
if m.Spec.AzureEnvironment == "" {
263+
// unsetting the field is not allowed
264+
allErrs = append(allErrs,
265+
field.Invalid(
266+
field.NewPath("Spec", "AzureEnvironment"),
267+
m.Spec.AzureEnvironment,
268+
"field is immutable, unsetting is not allowed"))
269+
} else if m.Spec.AzureEnvironment != old.Spec.AzureEnvironment {
270+
// changing the field is not allowed
271+
allErrs = append(allErrs,
272+
field.Invalid(
273+
field.NewPath("Spec", "AzureEnvironment"),
274+
*m.Spec.LoadBalancerSKU,
275+
"field is immutable"))
276+
}
277+
}
278+
256279
if m.Spec.DisableLocalAccounts != nil &&
257280
m.Spec.AADProfile == nil {
258281
allErrs = append(allErrs,

exp/controllers/azuremanagedmachinepool_reconciler.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,28 @@ func (a *AgentPoolVMSSNotFoundError) Is(target error) bool {
7474

7575
// newAzureManagedMachinePoolService populates all the services based on input scope.
7676
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
77+
scaleSetAuthorizer, err := scaleSetAuthorizer(scope)
78+
if err != nil {
79+
return nil, err
8480
}
8581

8682
return &azureManagedMachinePoolService{
8783
scope: scope,
8884
agentPoolsSvc: agentpools.New(scope),
89-
scaleSetsSvc: scalesets.NewClient(authorizer),
85+
scaleSetsSvc: scalesets.NewClient(scaleSetAuthorizer),
9086
}, nil
9187
}
9288

89+
// scaleSetAuthorizer takes a scope and determines if a regional authorizer is needed for scale sets
90+
// see https://github.com/kubernetes-sigs/cluster-api-provider-azure/pull/1850 for context on region based authorizer.
91+
func scaleSetAuthorizer(scope *scope.ManagedMachinePoolScope) (azure.Authorizer, error) {
92+
if scope.ControlPlane.Spec.AzureEnvironment == azure.PublicCloudName {
93+
return azure.WithRegionalBaseURI(scope, scope.Location()) // public cloud supports regional end points
94+
}
95+
96+
return scope, nil
97+
}
98+
9399
// Reconcile reconciles all the services in a predetermined order.
94100
func (s *azureManagedMachinePoolService) Reconcile(ctx context.Context) error {
95101
ctx, log, done := tele.StartSpanWithLogger(ctx, "controllers.azureManagedMachinePoolService.Reconcile")

0 commit comments

Comments
 (0)