sigs.k8s.io/cluster-api-provider-azure@v1.14.3/api/v1beta1/azureclustertemplate_default.go (about) 1 /* 2 Copyright 2022 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 "fmt" 21 ) 22 23 func (c *AzureClusterTemplate) setDefaults() { 24 c.Spec.Template.Spec.AzureClusterClassSpec.setDefaults() 25 c.setNetworkTemplateSpecDefaults() 26 } 27 28 func (c *AzureClusterTemplate) setNetworkTemplateSpecDefaults() { 29 c.setVnetTemplateDefaults() 30 c.setBastionTemplateDefaults() 31 c.setSubnetsTemplateDefaults() 32 33 apiServerLB := &c.Spec.Template.Spec.NetworkSpec.APIServerLB 34 apiServerLB.setAPIServerLBDefaults() 35 c.setNodeOutboundLBDefaults() 36 c.setControlPlaneOutboundLBDefaults() 37 } 38 39 func (c *AzureClusterTemplate) setVnetTemplateDefaults() { 40 c.Spec.Template.Spec.NetworkSpec.Vnet.VnetClassSpec.setDefaults() 41 } 42 43 func (c *AzureClusterTemplate) setBastionTemplateDefaults() { 44 if c.Spec.Template.Spec.BastionSpec.AzureBastion != nil { 45 // Ensure defaults for Subnet settings. 46 if len(c.Spec.Template.Spec.BastionSpec.AzureBastion.Subnet.CIDRBlocks) == 0 { 47 c.Spec.Template.Spec.BastionSpec.AzureBastion.Subnet.CIDRBlocks = []string{DefaultAzureBastionSubnetCIDR} 48 } 49 if c.Spec.Template.Spec.BastionSpec.AzureBastion.Subnet.Role == "" { 50 c.Spec.Template.Spec.BastionSpec.AzureBastion.Subnet.Role = DefaultAzureBastionSubnetRole 51 } 52 } 53 } 54 55 func (c *AzureClusterTemplate) setSubnetsTemplateDefaults() { 56 clusterSubnet, err := c.Spec.Template.Spec.NetworkSpec.GetSubnetTemplate(SubnetCluster) 57 clusterSubnetExists := err == nil 58 if clusterSubnetExists { 59 clusterSubnet.SubnetClassSpec.setDefaults(DefaultClusterSubnetCIDR) 60 clusterSubnet.SecurityGroup.setDefaults() 61 c.Spec.Template.Spec.NetworkSpec.UpdateSubnetTemplate(clusterSubnet, SubnetCluster) 62 } 63 64 cpSubnet, errcp := c.Spec.Template.Spec.NetworkSpec.GetSubnetTemplate(SubnetControlPlane) 65 if errcp == nil { 66 cpSubnet.SubnetClassSpec.setDefaults(DefaultControlPlaneSubnetCIDR) 67 cpSubnet.SecurityGroup.setDefaults() 68 c.Spec.Template.Spec.NetworkSpec.UpdateSubnetTemplate(cpSubnet, SubnetControlPlane) 69 } else if errcp != nil && !clusterSubnetExists { 70 cpSubnet = SubnetTemplateSpec{SubnetClassSpec: SubnetClassSpec{Role: SubnetControlPlane}} 71 cpSubnet.SubnetClassSpec.setDefaults(DefaultControlPlaneSubnetCIDR) 72 cpSubnet.SecurityGroup.setDefaults() 73 c.Spec.Template.Spec.NetworkSpec.Subnets = append(c.Spec.Template.Spec.NetworkSpec.Subnets, cpSubnet) 74 } 75 76 var nodeSubnetFound bool 77 var nodeSubnetCounter int 78 for i, subnet := range c.Spec.Template.Spec.NetworkSpec.Subnets { 79 if subnet.Role != SubnetNode { 80 continue 81 } 82 nodeSubnetCounter++ 83 nodeSubnetFound = true 84 subnet.SubnetClassSpec.setDefaults(fmt.Sprintf(DefaultNodeSubnetCIDRPattern, nodeSubnetCounter)) 85 subnet.SecurityGroup.setDefaults() 86 c.Spec.Template.Spec.NetworkSpec.Subnets[i] = subnet 87 } 88 89 if !nodeSubnetFound && !clusterSubnetExists { 90 nodeSubnet := SubnetTemplateSpec{ 91 SubnetClassSpec: SubnetClassSpec{ 92 Role: SubnetNode, 93 CIDRBlocks: []string{DefaultNodeSubnetCIDR}, 94 }, 95 } 96 c.Spec.Template.Spec.NetworkSpec.Subnets = append(c.Spec.Template.Spec.NetworkSpec.Subnets, nodeSubnet) 97 } 98 } 99 100 func (c *AzureClusterTemplate) setNodeOutboundLBDefaults() { 101 if c.Spec.Template.Spec.NetworkSpec.NodeOutboundLB == nil { 102 if c.Spec.Template.Spec.NetworkSpec.APIServerLB.Type == Internal { 103 return 104 } 105 106 var needsOutboundLB bool 107 for _, subnet := range c.Spec.Template.Spec.NetworkSpec.Subnets { 108 if (subnet.Role == SubnetNode || subnet.Role == SubnetCluster) && subnet.IsIPv6Enabled() { 109 needsOutboundLB = true 110 break 111 } 112 } 113 114 // If we don't default the outbound LB when there are some subnets with NAT gateway, 115 // and some without, those without wouldn't have outbound traffic. So taking the 116 // safer route, we configure the outbound LB in that scenario. 117 if !needsOutboundLB { 118 return 119 } 120 121 c.Spec.Template.Spec.NetworkSpec.NodeOutboundLB = &LoadBalancerClassSpec{} 122 } 123 124 c.Spec.Template.Spec.NetworkSpec.NodeOutboundLB.setNodeOutboundLBDefaults() 125 } 126 127 func (c *AzureClusterTemplate) setControlPlaneOutboundLBDefaults() { 128 lb := c.Spec.Template.Spec.NetworkSpec.ControlPlaneOutboundLB 129 if lb == nil { 130 return 131 } 132 lb.setControlPlaneOutboundLBDefaults() 133 }