@@ -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 ,
0 commit comments