sigs.k8s.io/cluster-api-provider-azure@v1.17.0/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 fleetsMember != nil { 105 if clusterName, ok := labels[clusterv1.ClusterNameLabel]; ok && fleetsMember.Name == "" { 106 result.Name = clusterName 107 } 108 if fleetsMember.Group == "" { 109 result.Group = "default" 110 } 111 } 112 return result 113 } 114 115 func setDefaultSku(sku *AKSSku) *AKSSku { 116 result := sku.DeepCopy() 117 if sku == nil { 118 result = new(AKSSku) 119 result.Tier = FreeManagedControlPlaneTier 120 } else if sku.Tier == PaidManagedControlPlaneTier { 121 result.Tier = StandardManagedControlPlaneTier 122 ctrl.Log.WithName("AzureManagedControlPlaneWebHookLogger").Info("Paid SKU tier is deprecated and has been replaced by Standard") 123 } 124 return result 125 } 126 127 func setDefaultVersion(version string) string { 128 if version != "" && !strings.HasPrefix(version, "v") { 129 normalizedVersion := "v" + version 130 version = normalizedVersion 131 } 132 return version 133 } 134 135 func setDefaultAutoScalerProfile(autoScalerProfile *AutoScalerProfile) *AutoScalerProfile { 136 if autoScalerProfile == nil { 137 return nil 138 } 139 140 result := autoScalerProfile.DeepCopy() 141 142 // Default values are from https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler#using-the-autoscaler-profile 143 // If any values are set, they all need to be set. 144 if autoScalerProfile.BalanceSimilarNodeGroups == nil { 145 result.BalanceSimilarNodeGroups = (*BalanceSimilarNodeGroups)(ptr.To(string(BalanceSimilarNodeGroupsFalse))) 146 } 147 if autoScalerProfile.Expander == nil { 148 result.Expander = (*Expander)(ptr.To(string(ExpanderRandom))) 149 } 150 if autoScalerProfile.MaxEmptyBulkDelete == nil { 151 result.MaxEmptyBulkDelete = ptr.To("10") 152 } 153 if autoScalerProfile.MaxGracefulTerminationSec == nil { 154 result.MaxGracefulTerminationSec = ptr.To("600") 155 } 156 if autoScalerProfile.MaxNodeProvisionTime == nil { 157 result.MaxNodeProvisionTime = ptr.To("15m") 158 } 159 if autoScalerProfile.MaxTotalUnreadyPercentage == nil { 160 result.MaxTotalUnreadyPercentage = ptr.To("45") 161 } 162 if autoScalerProfile.NewPodScaleUpDelay == nil { 163 result.NewPodScaleUpDelay = ptr.To("0s") 164 } 165 if autoScalerProfile.OkTotalUnreadyCount == nil { 166 result.OkTotalUnreadyCount = ptr.To("3") 167 } 168 if autoScalerProfile.ScanInterval == nil { 169 result.ScanInterval = ptr.To("10s") 170 } 171 if autoScalerProfile.ScaleDownDelayAfterAdd == nil { 172 result.ScaleDownDelayAfterAdd = ptr.To("10m") 173 } 174 if autoScalerProfile.ScaleDownDelayAfterDelete == nil { 175 // Default is the same as the ScanInterval so default to that same value if it isn't set 176 result.ScaleDownDelayAfterDelete = result.ScanInterval 177 } 178 if autoScalerProfile.ScaleDownDelayAfterFailure == nil { 179 result.ScaleDownDelayAfterFailure = ptr.To("3m") 180 } 181 if autoScalerProfile.ScaleDownUnneededTime == nil { 182 result.ScaleDownUnneededTime = ptr.To("10m") 183 } 184 if autoScalerProfile.ScaleDownUnreadyTime == nil { 185 result.ScaleDownUnreadyTime = ptr.To("20m") 186 } 187 if autoScalerProfile.ScaleDownUtilizationThreshold == nil { 188 result.ScaleDownUtilizationThreshold = ptr.To("0.5") 189 } 190 if autoScalerProfile.SkipNodesWithLocalStorage == nil { 191 result.SkipNodesWithLocalStorage = (*SkipNodesWithLocalStorage)(ptr.To(string(SkipNodesWithLocalStorageFalse))) 192 } 193 if autoScalerProfile.SkipNodesWithSystemPods == nil { 194 result.SkipNodesWithSystemPods = (*SkipNodesWithSystemPods)(ptr.To(string(SkipNodesWithSystemPodsTrue))) 195 } 196 197 return result 198 } 199 200 func (m *AzureManagedControlPlane) setDefaultOIDCIssuerProfile() { 201 if m.Spec.OIDCIssuerProfile == nil { 202 m.Spec.OIDCIssuerProfile = &OIDCIssuerProfile{} 203 } 204 205 if m.Spec.OIDCIssuerProfile.Enabled == nil { 206 m.Spec.OIDCIssuerProfile.Enabled = ptr.To(false) 207 } 208 } 209 210 func (m *AzureManagedControlPlane) setDefaultDNSPrefix() { 211 if m.Spec.DNSPrefix == nil { 212 m.Spec.DNSPrefix = ptr.To(m.Name) 213 } 214 } 215 216 func (m *AzureManagedControlPlane) setDefaultAKSExtensions() { 217 for _, extension := range m.Spec.Extensions { 218 if extension.Plan != nil && extension.Plan.Name == "" { 219 extension.Plan.Name = fmt.Sprintf("%s-%s", m.Name, extension.Plan.Product) 220 } 221 if extension.AutoUpgradeMinorVersion == nil { 222 extension.AutoUpgradeMinorVersion = ptr.To(true) 223 } 224 } 225 }