sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/azuremanagedcontrolplane_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  	"encoding/base64"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"golang.org/x/crypto/ssh"
    25  	"k8s.io/utils/ptr"
    26  	utilSSH "sigs.k8s.io/cluster-api-provider-azure/util/ssh"
    27  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    28  	ctrl "sigs.k8s.io/controller-runtime"
    29  )
    30  
    31  const (
    32  	// defaultAKSVnetCIDR is the default Vnet CIDR.
    33  	defaultAKSVnetCIDR = "10.0.0.0/8"
    34  	// defaultAKSNodeSubnetCIDR is the default Node Subnet CIDR.
    35  	defaultAKSNodeSubnetCIDR = "10.240.0.0/16"
    36  	// defaultAKSVnetCIDRForOverlay is the default Vnet CIDR when Azure CNI overlay is enabled.
    37  	defaultAKSVnetCIDRForOverlay = "10.224.0.0/12"
    38  	// defaultAKSNodeSubnetCIDRForOverlay is the default Node Subnet CIDR when Azure CNI overlay is enabled.
    39  	defaultAKSNodeSubnetCIDRForOverlay = "10.224.0.0/16"
    40  )
    41  
    42  // setDefaultResourceGroupName sets the default ResourceGroupName for an AzureManagedControlPlane.
    43  func (m *AzureManagedControlPlane) setDefaultResourceGroupName() {
    44  	if m.Spec.ResourceGroupName == "" {
    45  		if clusterName, ok := m.Labels[clusterv1.ClusterNameLabel]; ok {
    46  			m.Spec.ResourceGroupName = clusterName
    47  		}
    48  	}
    49  }
    50  
    51  // setDefaultSSHPublicKey sets the default SSHPublicKey for an AzureManagedControlPlane.
    52  func (m *AzureManagedControlPlane) setDefaultSSHPublicKey() error {
    53  	if sshKey := m.Spec.SSHPublicKey; sshKey != nil && *sshKey == "" {
    54  		_, publicRsaKey, err := utilSSH.GenerateSSHKey()
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  		m.Spec.SSHPublicKey = ptr.To(base64.StdEncoding.EncodeToString(ssh.MarshalAuthorizedKey(publicRsaKey)))
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  // setDefaultNodeResourceGroupName sets the default NodeResourceGroup for an AzureManagedControlPlane.
    66  func (m *AzureManagedControlPlane) setDefaultNodeResourceGroupName() {
    67  	if m.Spec.NodeResourceGroupName == "" {
    68  		m.Spec.NodeResourceGroupName = fmt.Sprintf("MC_%s_%s_%s", m.Spec.ResourceGroupName, m.Name, m.Spec.Location)
    69  	}
    70  }
    71  
    72  // setDefaultVirtualNetwork sets the default VirtualNetwork for an AzureManagedControlPlane.
    73  func (m *AzureManagedControlPlane) setDefaultVirtualNetwork() {
    74  	if m.Spec.VirtualNetwork.Name == "" {
    75  		m.Spec.VirtualNetwork.Name = m.Name
    76  	}
    77  	if m.Spec.VirtualNetwork.CIDRBlock == "" {
    78  		m.Spec.VirtualNetwork.CIDRBlock = defaultAKSVnetCIDR
    79  		if ptr.Deref(m.Spec.NetworkPluginMode, "") == NetworkPluginModeOverlay {
    80  			m.Spec.VirtualNetwork.CIDRBlock = defaultAKSVnetCIDRForOverlay
    81  		}
    82  	}
    83  	if m.Spec.VirtualNetwork.ResourceGroup == "" {
    84  		m.Spec.VirtualNetwork.ResourceGroup = m.Spec.ResourceGroupName
    85  	}
    86  }
    87  
    88  // setDefaultSubnet sets the default Subnet for an AzureManagedControlPlane.
    89  func (m *AzureManagedControlPlane) setDefaultSubnet() {
    90  	if m.Spec.VirtualNetwork.Subnet.Name == "" {
    91  		m.Spec.VirtualNetwork.Subnet.Name = m.Name
    92  	}
    93  	if m.Spec.VirtualNetwork.Subnet.CIDRBlock == "" {
    94  		m.Spec.VirtualNetwork.Subnet.CIDRBlock = defaultAKSNodeSubnetCIDR
    95  		if ptr.Deref(m.Spec.NetworkPluginMode, "") == NetworkPluginModeOverlay {
    96  			m.Spec.VirtualNetwork.Subnet.CIDRBlock = defaultAKSNodeSubnetCIDRForOverlay
    97  		}
    98  	}
    99  }
   100  
   101  // setDefaultFleetsMember sets the default FleetsMember for an AzureManagedControlPlane.
   102  func setDefaultFleetsMember(fleetsMember *FleetsMember, labels map[string]string) *FleetsMember {
   103  	result := fleetsMember.DeepCopy()
   104  	if clusterName, ok := labels[clusterv1.ClusterNameLabel]; ok && fleetsMember != nil && fleetsMember.Name == "" {
   105  		result.Name = clusterName
   106  	}
   107  	return result
   108  }
   109  
   110  func setDefaultSku(sku *AKSSku) *AKSSku {
   111  	result := sku.DeepCopy()
   112  	if sku == nil {
   113  		result = new(AKSSku)
   114  		result.Tier = FreeManagedControlPlaneTier
   115  	} else if sku.Tier == PaidManagedControlPlaneTier {
   116  		result.Tier = StandardManagedControlPlaneTier
   117  		ctrl.Log.WithName("AzureManagedControlPlaneWebHookLogger").Info("Paid SKU tier is deprecated and has been replaced by Standard")
   118  	}
   119  	return result
   120  }
   121  
   122  func setDefaultVersion(version string) string {
   123  	if version != "" && !strings.HasPrefix(version, "v") {
   124  		normalizedVersion := "v" + version
   125  		version = normalizedVersion
   126  	}
   127  	return version
   128  }
   129  
   130  func setDefaultAutoScalerProfile(autoScalerProfile *AutoScalerProfile) *AutoScalerProfile {
   131  	if autoScalerProfile == nil {
   132  		return nil
   133  	}
   134  
   135  	result := autoScalerProfile.DeepCopy()
   136  
   137  	// Default values are from https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler#using-the-autoscaler-profile
   138  	// If any values are set, they all need to be set.
   139  	if autoScalerProfile.BalanceSimilarNodeGroups == nil {
   140  		result.BalanceSimilarNodeGroups = (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse)))
   141  	}
   142  	if autoScalerProfile.Expander == nil {
   143  		result.Expander = (*Expander)(ptr.To(string(ExpanderRandom)))
   144  	}
   145  	if autoScalerProfile.MaxEmptyBulkDelete == nil {
   146  		result.MaxEmptyBulkDelete = ptr.To("10")
   147  	}
   148  	if autoScalerProfile.MaxGracefulTerminationSec == nil {
   149  		result.MaxGracefulTerminationSec = ptr.To("600")
   150  	}
   151  	if autoScalerProfile.MaxNodeProvisionTime == nil {
   152  		result.MaxNodeProvisionTime = ptr.To("15m")
   153  	}
   154  	if autoScalerProfile.MaxTotalUnreadyPercentage == nil {
   155  		result.MaxTotalUnreadyPercentage = ptr.To("45")
   156  	}
   157  	if autoScalerProfile.NewPodScaleUpDelay == nil {
   158  		result.NewPodScaleUpDelay = ptr.To("0s")
   159  	}
   160  	if autoScalerProfile.OkTotalUnreadyCount == nil {
   161  		result.OkTotalUnreadyCount = ptr.To("3")
   162  	}
   163  	if autoScalerProfile.ScanInterval == nil {
   164  		result.ScanInterval = ptr.To("10s")
   165  	}
   166  	if autoScalerProfile.ScaleDownDelayAfterAdd == nil {
   167  		result.ScaleDownDelayAfterAdd = ptr.To("10m")
   168  	}
   169  	if autoScalerProfile.ScaleDownDelayAfterDelete == nil {
   170  		// Default is the same as the ScanInterval so default to that same value if it isn't set
   171  		result.ScaleDownDelayAfterDelete = result.ScanInterval
   172  	}
   173  	if autoScalerProfile.ScaleDownDelayAfterFailure == nil {
   174  		result.ScaleDownDelayAfterFailure = ptr.To("3m")
   175  	}
   176  	if autoScalerProfile.ScaleDownUnneededTime == nil {
   177  		result.ScaleDownUnneededTime = ptr.To("10m")
   178  	}
   179  	if autoScalerProfile.ScaleDownUnreadyTime == nil {
   180  		result.ScaleDownUnreadyTime = ptr.To("20m")
   181  	}
   182  	if autoScalerProfile.ScaleDownUtilizationThreshold == nil {
   183  		result.ScaleDownUtilizationThreshold = ptr.To("0.5")
   184  	}
   185  	if autoScalerProfile.SkipNodesWithLocalStorage == nil {
   186  		result.SkipNodesWithLocalStorage = (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse)))
   187  	}
   188  	if autoScalerProfile.SkipNodesWithSystemPods == nil {
   189  		result.SkipNodesWithSystemPods = (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue)))
   190  	}
   191  
   192  	return result
   193  }
   194  
   195  func (m *AzureManagedControlPlane) setDefaultOIDCIssuerProfile() {
   196  	if m.Spec.OIDCIssuerProfile == nil {
   197  		m.Spec.OIDCIssuerProfile = &OIDCIssuerProfile{}
   198  	}
   199  
   200  	if m.Spec.OIDCIssuerProfile.Enabled == nil {
   201  		m.Spec.OIDCIssuerProfile.Enabled = ptr.To(false)
   202  	}
   203  }
   204  
   205  func (m *AzureManagedControlPlane) setDefaultDNSPrefix() {
   206  	if m.Spec.DNSPrefix == nil {
   207  		m.Spec.DNSPrefix = ptr.To(m.Name)
   208  	}
   209  }
   210  
   211  func (m *AzureManagedControlPlane) setDefaultAKSExtensions() {
   212  	for _, extension := range m.Spec.Extensions {
   213  		if extension.Plan != nil && extension.Plan.Name == "" {
   214  			extension.Plan.Name = fmt.Sprintf("%s-%s", m.Name, extension.Plan.Product)
   215  		}
   216  		if extension.AutoUpgradeMinorVersion == nil {
   217  			extension.AutoUpgradeMinorVersion = ptr.To(true)
   218  		}
   219  	}
   220  }