sigs.k8s.io/cluster-api-provider-azure@v1.17.0/api/v1beta1/azuremanagedcontrolplanetemplate_default.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1beta1
    18  
    19  import (
    20  	"strings"
    21  
    22  	"k8s.io/utils/ptr"
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  )
    25  
    26  func (mcp *AzureManagedControlPlaneTemplate) setDefaults() {
    27  	setDefault[*string](&mcp.Spec.Template.Spec.NetworkPlugin, ptr.To(AzureNetworkPluginName))
    28  	setDefault[*string](&mcp.Spec.Template.Spec.LoadBalancerSKU, ptr.To("Standard"))
    29  	setDefault[*bool](&mcp.Spec.Template.Spec.EnablePreviewFeatures, ptr.To(false))
    30  
    31  	if mcp.Spec.Template.Spec.Version != "" && !strings.HasPrefix(mcp.Spec.Template.Spec.Version, "v") {
    32  		mcp.Spec.Template.Spec.Version = setDefaultVersion(mcp.Spec.Template.Spec.Version)
    33  	}
    34  
    35  	mcp.setDefaultVirtualNetwork()
    36  	mcp.setDefaultSubnet()
    37  	mcp.Spec.Template.Spec.SKU = setDefaultSku(mcp.Spec.Template.Spec.SKU)
    38  	mcp.Spec.Template.Spec.AutoScalerProfile = setDefaultAutoScalerProfile(mcp.Spec.Template.Spec.AutoScalerProfile)
    39  }
    40  
    41  // setDefaultVirtualNetwork sets the default VirtualNetwork for an AzureManagedControlPlaneTemplate.
    42  func (mcp *AzureManagedControlPlaneTemplate) setDefaultVirtualNetwork() {
    43  	if mcp.Spec.Template.Spec.VirtualNetwork.Name != "" {
    44  		// Being able to set the vnet name in the template type is a bug, as vnet names cannot be reused across clusters.
    45  		// To avoid a breaking API change, a warning is logged.
    46  		ctrl.Log.WithName("AzureManagedControlPlaneTemplateWebHookLogger").Info("WARNING: VirtualNetwork.Name should not be set in the template. Virtual Network names cannot be shared across clusters.")
    47  	}
    48  	if mcp.Spec.Template.Spec.VirtualNetwork.CIDRBlock == "" {
    49  		mcp.Spec.Template.Spec.VirtualNetwork.CIDRBlock = defaultAKSVnetCIDR
    50  	}
    51  }
    52  
    53  // setDefaultSubnet sets the default Subnet for an AzureManagedControlPlaneTemplate.
    54  func (mcp *AzureManagedControlPlaneTemplate) setDefaultSubnet() {
    55  	if mcp.Spec.Template.Spec.VirtualNetwork.Subnet.Name == "" {
    56  		mcp.Spec.Template.Spec.VirtualNetwork.Subnet.Name = mcp.Name
    57  	}
    58  	if mcp.Spec.Template.Spec.VirtualNetwork.Subnet.CIDRBlock == "" {
    59  		mcp.Spec.Template.Spec.VirtualNetwork.Subnet.CIDRBlock = defaultAKSNodeSubnetCIDR
    60  	}
    61  }
    62  
    63  // setDefault sets the default value for a pointer to a value for any comparable type.
    64  func setDefault[T comparable](field *T, value T) {
    65  	if field == nil {
    66  		// shouldn't happen with proper use
    67  		return
    68  	}
    69  	var zero T
    70  	if *field == zero {
    71  		*field = value
    72  	}
    73  }