diff --git a/api/v1beta1/azurecluster_default.go b/api/v1beta1/azurecluster_default.go index 27792ab7380..731932bab04 100644 --- a/api/v1beta1/azurecluster_default.go +++ b/api/v1beta1/azurecluster_default.go @@ -94,21 +94,27 @@ func (c *AzureCluster) setVnetDefaults() { c.Spec.NetworkSpec.Vnet.VnetClassSpec.setDefaults() } +// setSubnetDefaults ensures a fully populated, default subnet configuration +// and in certain scenarios creates new, default subnet configurations. func (c *AzureCluster) setSubnetDefaults() { clusterSubnet, err := c.Spec.NetworkSpec.GetSubnet(SubnetCluster) clusterSubnetExists := err == nil + // If we already have a cluster subnet defined, ensure it has sensible defaults + // for all properties. if clusterSubnetExists { clusterSubnet.setClusterSubnetDefaults(c.ObjectMeta.Name) c.Spec.NetworkSpec.UpdateSubnet(clusterSubnet, SubnetCluster) } if c.Spec.ControlPlaneEnabled { - /* if there is a cp subnet set defaults - if no cp subnet and cluster subnet create a default cp subnet */ cpSubnet, errcp := c.Spec.NetworkSpec.GetSubnet(SubnetControlPlane) + // If we already have a control plane subnet defined, ensure it has sensible defaults + // for all properties. if errcp == nil { cpSubnet.setControlPlaneSubnetDefaults(c.ObjectMeta.Name) c.Spec.NetworkSpec.UpdateSubnet(cpSubnet, SubnetControlPlane) + // If we don't have either a control plane subnet or a cluster subnet, + // create a new control plane subnet from scratch and populate with sensible defaults. } else if !clusterSubnetExists { cpSubnet = SubnetSpec{SubnetClassSpec: SubnetClassSpec{Role: SubnetControlPlane}} cpSubnet.setControlPlaneSubnetDefaults(c.ObjectMeta.Name) @@ -116,19 +122,28 @@ func (c *AzureCluster) setSubnetDefaults() { } } - var nodeSubnetFound bool + // anyNodeSubnetFound tracks whether or not we have one or more node subnets defined. + var anyNodeSubnetFound bool + // nodeSubnetCounter tracks all node subnets to aid automatic CIDR configuration. var nodeSubnetCounter int for i, subnet := range c.Spec.NetworkSpec.Subnets { + // Skip all non-node subnets if subnet.Role != SubnetNode { continue } nodeSubnetCounter++ - nodeSubnetFound = true + anyNodeSubnetFound = true + // Set has sensible defaults for this existing node subnet. subnet.setNodeSubnetDefaults(c.ObjectMeta.Name, nodeSubnetCounter) + // Because there can be multiple node subnets, we have to update any changes + // after applying defaults to the explicit item at the current index. c.Spec.NetworkSpec.Subnets[i] = subnet } - if !nodeSubnetFound && !clusterSubnetExists { + // We need at least one subnet for nodes. + // If no node subnets are defined, and there is no cluster subnet defined, + // create a default 10.1.0.0/16 node subnet. + if !anyNodeSubnetFound && !clusterSubnetExists { nodeSubnet := SubnetSpec{ SubnetClassSpec: SubnetClassSpec{ Role: SubnetNode,