@@ -94,41 +94,56 @@ func (c *AzureCluster) setVnetDefaults() {
94
94
c .Spec .NetworkSpec .Vnet .VnetClassSpec .setDefaults ()
95
95
}
96
96
97
+ // setSubnetDefaults ensures a fully populated, default subnet configuration
98
+ // and in certain scenarios creates new, default subnet configurations.
97
99
func (c * AzureCluster ) setSubnetDefaults () {
98
100
clusterSubnet , err := c .Spec .NetworkSpec .GetSubnet (SubnetCluster )
99
101
clusterSubnetExists := err == nil
102
+ // If we already have a cluster subnet defined, ensure it has sensible defaults
103
+ // for all properties.
100
104
if clusterSubnetExists {
101
105
clusterSubnet .setClusterSubnetDefaults (c .ObjectMeta .Name )
102
106
c .Spec .NetworkSpec .UpdateSubnet (clusterSubnet , SubnetCluster )
103
107
}
104
108
105
109
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 */
108
110
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.
109
113
if errcp == nil {
110
114
cpSubnet .setControlPlaneSubnetDefaults (c .ObjectMeta .Name )
111
115
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.
112
118
} else if ! clusterSubnetExists {
113
119
cpSubnet = SubnetSpec {SubnetClassSpec : SubnetClassSpec {Role : SubnetControlPlane }}
114
120
cpSubnet .setControlPlaneSubnetDefaults (c .ObjectMeta .Name )
115
121
c .Spec .NetworkSpec .Subnets = append (c .Spec .NetworkSpec .Subnets , cpSubnet )
116
122
}
117
123
}
118
124
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.
120
128
var nodeSubnetCounter int
121
129
for i , subnet := range c .Spec .NetworkSpec .Subnets {
130
+ // Skip all non-node subnets
122
131
if subnet .Role != SubnetNode {
123
132
continue
124
133
}
125
134
nodeSubnetCounter ++
126
- nodeSubnetFound = true
135
+ anyNodeSubnetFound = true
136
+ // Set has sensible defaults for this existing node subnet.
127
137
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.
128
140
c .Spec .NetworkSpec .Subnets [i ] = subnet
129
141
}
130
142
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 {
132
147
nodeSubnet := SubnetSpec {
133
148
SubnetClassSpec : SubnetClassSpec {
134
149
Role : SubnetNode ,
0 commit comments