Skip to content

Commit f029840

Browse files
committed
Move subnet options to SubnetSpec
This commit introduces SubnetSpec field onto the OpenStackClusterSpec that is supposed to hold all options related to subnets created by CAPO. This means nodeCidr and DNSNameservers are moved into that struct.
1 parent 5cc483b commit f029840

25 files changed

+243
-69
lines changed

api/v1alpha5/conversion.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha5_OpenStackClusterSpec(in *
196196
out.ExternalNetworkID = in.ExternalNetwork.ID
197197
}
198198

199+
if len(in.NodeSubnets) > 0 {
200+
out.NodeCIDR = in.NodeSubnets[0].CIDR
201+
out.DNSNameservers = in.NodeSubnets[0].DNSNameservers
202+
}
203+
199204
if in.Subnets != nil {
200205
if len(in.Subnets) >= 1 {
201206
if err := Convert_v1alpha8_SubnetFilter_To_v1alpha5_SubnetFilter(&in.Subnets[0], &out.Subnet, s); err != nil {
@@ -228,6 +233,16 @@ func Convert_v1alpha5_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
228233
out.Subnets = []infrav1.SubnetFilter{subnet}
229234
}
230235

236+
if len(in.NodeCIDR) > 0 {
237+
out.NodeSubnets = []infrav1.SubnetSpec{
238+
{
239+
CIDR: in.NodeCIDR,
240+
DNSNameservers: in.DNSNameservers,
241+
},
242+
}
243+
}
244+
// We're dropping DNSNameservers even if these were set as without NodeCIDR it doesn't make sense.
245+
231246
return nil
232247
}
233248

api/v1alpha5/zz_generated.conversion.go

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha6/conversion.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func restorev1alpha8ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst
112112
}
113113
}
114114

115+
func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *infrav1.OpenStackClusterSpec) {
116+
dst.NodeSubnets = previous.NodeSubnets
117+
}
118+
115119
func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackClusterSpec) {
116120
for i := range previous.ExternalRouterIPs {
117121
dstIP := &dst.ExternalRouterIPs[i]
@@ -127,6 +131,11 @@ func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackCl
127131
}
128132
}
129133

134+
// We only restore DNSNameservers when these were lossly converted when NodeCIDR is empty.
135+
if len(previous.DNSNameservers) > 0 && len(dst.NodeCIDR) == 0 {
136+
dst.DNSNameservers = previous.DNSNameservers
137+
}
138+
130139
prevBastion := previous.Bastion
131140
dstBastion := dst.Bastion
132141
if prevBastion != nil && dstBastion != nil {
@@ -190,6 +199,12 @@ var v1alpha8OpenStackClusterRestorer = conversion.RestorerFor[*infrav1.OpenStack
190199
},
191200
restorev1alpha8ClusterStatus,
192201
),
202+
"spec": conversion.HashedFieldRestorer(
203+
func(c *infrav1.OpenStackCluster) *infrav1.OpenStackClusterSpec {
204+
return &c.Spec
205+
},
206+
restorev1alpha8ClusterSpec,
207+
),
193208
}
194209

195210
func (r *OpenStackCluster) ConvertTo(dstRaw ctrlconversion.Hub) error {
@@ -545,6 +560,11 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *
545560
}
546561
}
547562

563+
if len(in.NodeSubnets) > 0 {
564+
out.NodeCIDR = in.NodeSubnets[0].CIDR
565+
out.DNSNameservers = in.NodeSubnets[0].DNSNameservers
566+
}
567+
548568
return nil
549569
}
550570

@@ -569,6 +589,16 @@ func Convert_v1alpha6_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
569589
out.Subnets = []infrav1.SubnetFilter{subnet}
570590
}
571591

592+
// DNSNameservers without NodeCIDR doesn't make sense, so we drop that.
593+
if len(in.NodeCIDR) > 0 {
594+
out.NodeSubnets = []infrav1.SubnetSpec{
595+
{
596+
CIDR: in.NodeCIDR,
597+
DNSNameservers: in.DNSNameservers,
598+
},
599+
}
600+
}
601+
572602
return nil
573603
}
574604

api/v1alpha6/conversion_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ func TestFuzzyConversion(t *testing.T) {
132132
}
133133
}
134134
},
135+
136+
func(spec *infrav1.SubnetSpec, c fuzz.Continue) {
137+
c.FuzzNoCustom(spec)
138+
139+
// CIDR is required and API validates that it's present, so
140+
// we force it to always be set.
141+
for len(spec.CIDR) == 0 {
142+
spec.CIDR = c.RandString()
143+
}
144+
},
135145
}
136146
}
137147

@@ -156,6 +166,7 @@ func TestFuzzyConversion(t *testing.T) {
156166
Hub: &infrav1.OpenStackClusterTemplate{},
157167
Spoke: &OpenStackClusterTemplate{},
158168
HubAfterMutation: ignoreDataAnnotation,
169+
FuzzerFuncs: []fuzzer.FuzzerFuncs{fuzzerFuncs},
159170
})))
160171

161172
t.Run("for OpenStackClusterTemplate with mutate", runParallel(testhelpers.FuzzMutateTestFunc(testhelpers.FuzzMutateTestFuncInput{

api/v1alpha6/zz_generated.conversion.go

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha7/conversion.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ var v1alpha7OpenStackClusterRestorer = conversion.RestorerFor[*OpenStackCluster]
3939
},
4040
restorev1alpha7Bastion,
4141
),
42+
"spec": conversion.HashedFieldRestorer(
43+
func(c *OpenStackCluster) *OpenStackClusterSpec {
44+
return &c.Spec
45+
},
46+
restorev1alpha7ClusterSpec,
47+
48+
// Filter out Bastion, which is restored separately
49+
conversion.HashedFilterField[*OpenStackCluster, OpenStackClusterSpec](
50+
func(s *OpenStackClusterSpec) *OpenStackClusterSpec {
51+
if s.Bastion != nil {
52+
f := *s
53+
f.Bastion = nil
54+
return &f
55+
}
56+
return s
57+
},
58+
),
59+
),
4260
}
4361

4462
var v1alpha8OpenStackClusterRestorer = conversion.RestorerFor[*infrav1.OpenStackCluster]{
@@ -111,6 +129,19 @@ func restorev1alpha8Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
111129
}
112130
}
113131

132+
func restorev1alpha7ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackClusterSpec) {
133+
prevBastion := previous.Bastion
134+
dstBastion := dst.Bastion
135+
if prevBastion != nil && dstBastion != nil {
136+
restorev1alpha7MachineSpec(&prevBastion.Instance, &dstBastion.Instance)
137+
}
138+
139+
// We only restore DNSNameservers when these were lossly converted when NodeCIDR is empty.
140+
if len(previous.DNSNameservers) > 0 && len(dst.NodeCIDR) == 0 {
141+
dst.DNSNameservers = previous.DNSNameservers
142+
}
143+
}
144+
114145
func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *infrav1.OpenStackClusterSpec) {
115146
prevBastion := previous.Bastion
116147
dstBastion := dst.Bastion
@@ -132,6 +163,8 @@ func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *inf
132163
if len(previous.Subnets) > 1 {
133164
dst.Subnets = append(dst.Subnets, previous.Subnets[1:]...)
134165
}
166+
167+
dst.NodeSubnets = previous.NodeSubnets
135168
}
136169

137170
func (r *OpenStackCluster) ConvertTo(dstRaw ctrlconversion.Hub) error {
@@ -171,6 +204,7 @@ func (r *OpenStackClusterList) ConvertFrom(srcRaw ctrlconversion.Hub) error {
171204
var _ ctrlconversion.Convertible = &OpenStackClusterTemplate{}
172205

173206
func restorev1alpha7ClusterTemplateSpec(previous *OpenStackClusterTemplateSpec, dst *OpenStackClusterTemplateSpec) {
207+
restorev1alpha7ClusterSpec(&previous.Template.Spec, &dst.Template.Spec)
174208
restorev1alpha7Bastion(&previous.Template.Spec.Bastion, &dst.Template.Spec.Bastion)
175209
}
176210

@@ -465,6 +499,16 @@ func Convert_v1alpha7_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
465499
out.Subnets = []infrav1.SubnetFilter{subnet}
466500
}
467501

502+
// DNSNameservers without NodeCIDR doesn't make sense, so we drop that.
503+
if len(in.NodeCIDR) > 0 {
504+
out.NodeSubnets = []infrav1.SubnetSpec{
505+
{
506+
CIDR: in.NodeCIDR,
507+
DNSNameservers: in.DNSNameservers,
508+
},
509+
}
510+
}
511+
468512
return nil
469513
}
470514

@@ -484,5 +528,10 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha7_OpenStackClusterSpec(in *
484528
}
485529
}
486530

531+
if len(in.NodeSubnets) > 0 {
532+
out.NodeCIDR = in.NodeSubnets[0].CIDR
533+
out.DNSNameservers = in.NodeSubnets[0].DNSNameservers
534+
}
535+
487536
return nil
488537
}

api/v1alpha7/conversion_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ func TestFuzzyConversion(t *testing.T) {
100100
}
101101
}
102102
},
103+
104+
func(spec *infrav1.SubnetSpec, c fuzz.Continue) {
105+
c.FuzzNoCustom(spec)
106+
107+
// CIDR is required and API validates that it's present, so
108+
// we force it to always be set.
109+
for len(spec.CIDR) == 0 {
110+
spec.CIDR = c.RandString()
111+
}
112+
},
103113
}
104114
}
105115

api/v1alpha7/zz_generated.conversion.go

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha8/openstackcluster_types.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ type OpenStackClusterSpec struct {
3434
// +optional
3535
CloudName string `json:"cloudName"`
3636

37-
// NodeCIDR is the OpenStack Subnet to be created. Cluster actuator will create a
38-
// network, a subnet with NodeCIDR, and a router connected to this subnet.
37+
// NodeSubnets are describing OpenStack Subnets to be created. Cluster actuator
38+
// will create a network, subnets with NodeCIDR, and a router connected to these subnets.
39+
// Currently only one IPv4 subnet is supported.
3940
// If you leave this empty, no network will be created.
40-
NodeCIDR string `json:"nodeCidr,omitempty"`
41+
// +kubebuilder:validation:MaxItems=1
42+
NodeSubnets []SubnetSpec `json:"nodeSubnets,omitempty"`
4143

4244
// If NodeCIDR is set this option can be used to detect an existing router.
4345
// If specified, no new router will be created.
@@ -58,11 +60,6 @@ type OpenStackClusterSpec struct {
5860
// +optional
5961
NetworkMTU int `json:"networkMtu,omitempty"`
6062

61-
// DNSNameservers is the list of nameservers for OpenStack Subnet being created.
62-
// Set this value when you need create a new network/subnet while the access
63-
// through DNS is required.
64-
// +listType=set
65-
DNSNameservers []string `json:"dnsNameservers,omitempty"`
6663
// ExternalRouterIPs is an array of externalIPs on the respective subnets.
6764
// This is necessary if the router needs a fixed ip in a specific subnet.
6865
ExternalRouterIPs []ExternalRouterIPParam `json:"externalRouterIPs,omitempty"`

api/v1alpha8/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ type RouterFilter struct {
8787
NotTagsAny string `json:"notTagsAny,omitempty"`
8888
}
8989

90+
type SubnetSpec struct {
91+
// CIDR is representing the IP address range used to create the subnet, e.g. 10.0.0.0/24.
92+
// This field is required when defining a subnet.
93+
// +required
94+
CIDR string `json:"cidr"`
95+
// DNSNameservers holds a list of DNS server addresses that will be provided when creating
96+
// the subnet. These addresses need to have the same IP version as CIDR.
97+
DNSNameservers []string `json:"dnsNameservers,omitempty"`
98+
}
99+
90100
type PortOpts struct {
91101
// Network is a query for an openstack network that the port will be created or discovered on.
92102
// This will fail if the query returns more than one network.

0 commit comments

Comments
 (0)