@@ -94,41 +94,56 @@ func (c *AzureCluster) setVnetDefaults() {
9494	c .Spec .NetworkSpec .Vnet .VnetClassSpec .setDefaults ()
9595}
9696
97+ // setSubnetDefaults ensures a fully populated, default subnet configuration 
98+ // and in certain scenarios creates new, default subnet configurations. 
9799func  (c  * AzureCluster ) setSubnetDefaults () {
98100	clusterSubnet , err  :=  c .Spec .NetworkSpec .GetSubnet (SubnetCluster )
99101	clusterSubnetExists  :=  err  ==  nil 
102+ 	// If we already have a cluster subnet defined, ensure it has sensible defaults 
103+ 	// for all properties. 
100104	if  clusterSubnetExists  {
101105		clusterSubnet .setClusterSubnetDefaults (c .ObjectMeta .Name )
102106		c .Spec .NetworkSpec .UpdateSubnet (clusterSubnet , SubnetCluster )
103107	}
104108
105109	if  c .Spec .ControlPlaneEnabled  {
106- 		/* if there is a cp subnet set defaults 
107- 		   if no cp subnet and cluster subnet create a default cp subnet */ 
108110		cpSubnet , errcp  :=  c .Spec .NetworkSpec .GetSubnet (SubnetControlPlane )
111+ 		// If we already have a control plane subnet defined, ensure it has sensible defaults 
112+ 		// for all properties. 
109113		if  errcp  ==  nil  {
110114			cpSubnet .setControlPlaneSubnetDefaults (c .ObjectMeta .Name )
111115			c .Spec .NetworkSpec .UpdateSubnet (cpSubnet , SubnetControlPlane )
116+ 			// If we don't have either a control plane subnet or a cluster subnet, 
117+ 			// create a new control plane subnet from scratch and populate with sensible defaults. 
112118		} else  if  ! clusterSubnetExists  {
113119			cpSubnet  =  SubnetSpec {SubnetClassSpec : SubnetClassSpec {Role : SubnetControlPlane }}
114120			cpSubnet .setControlPlaneSubnetDefaults (c .ObjectMeta .Name )
115121			c .Spec .NetworkSpec .Subnets  =  append (c .Spec .NetworkSpec .Subnets , cpSubnet )
116122		}
117123	}
118124
119- 	var  nodeSubnetFound  bool 
125+ 	// anyNodeSubnetFound tracks whether or not we have one or more node subnets defined. 
126+ 	var  anyNodeSubnetFound  bool 
127+ 	// nodeSubnetCounter tracks all node subnets to aid automatic CIDR configuration. 
120128	var  nodeSubnetCounter  int 
121129	for  i , subnet  :=  range  c .Spec .NetworkSpec .Subnets  {
130+ 		// Skip all non-node subnets 
122131		if  subnet .Role  !=  SubnetNode  {
123132			continue 
124133		}
125134		nodeSubnetCounter ++ 
126- 		nodeSubnetFound  =  true 
135+ 		anyNodeSubnetFound  =  true 
136+ 		// Set has sensible defaults for this existing node subnet. 
127137		subnet .setNodeSubnetDefaults (c .ObjectMeta .Name , nodeSubnetCounter )
138+ 		// Because there can be multiple node subnets, we have to update any changes 
139+ 		// after applying defaults to the explicit item at the current index. 
128140		c .Spec .NetworkSpec .Subnets [i ] =  subnet 
129141	}
130142
131- 	if  ! nodeSubnetFound  &&  ! clusterSubnetExists  {
143+ 	// We need at least one subnet for nodes. 
144+ 	// If no node subnets are defined, and there is no cluster subnet defined, 
145+ 	// create a default 10.1.0.0/16 node subnet. 
146+ 	if  ! anyNodeSubnetFound  &&  ! clusterSubnetExists  {
132147		nodeSubnet  :=  SubnetSpec {
133148			SubnetClassSpec : SubnetClassSpec {
134149				Role :       SubnetNode ,
0 commit comments