Skip to content

Commit 07d3f28

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 e89ec9c commit 07d3f28

15 files changed

+217
-61
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.NodeCIDRs) > 0 {
200+
out.NodeCIDR = in.NodeCIDRs[0].CIDR
201+
out.DNSNameservers = in.NodeCIDRs[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.NodeCIDRs = []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
@@ -94,6 +94,10 @@ func restorev1alpha8ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst
9494
}
9595
}
9696

97+
func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *infrav1.OpenStackClusterSpec) {
98+
dst.NodeCIDRs = previous.NodeCIDRs
99+
}
100+
97101
func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackClusterSpec) {
98102
for i := range previous.ExternalRouterIPs {
99103
dstIP := &dst.ExternalRouterIPs[i]
@@ -109,6 +113,11 @@ func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackCl
109113
}
110114
}
111115

116+
// We only restore DNSNameservers when these were lossly converted when NodeCIDR is empty.
117+
if len(previous.DNSNameservers) > 0 && len(dst.NodeCIDR) == 0 {
118+
dst.DNSNameservers = previous.DNSNameservers
119+
}
120+
112121
prevBastion := previous.Bastion
113122
dstBastion := dst.Bastion
114123
if prevBastion != nil && dstBastion != nil {
@@ -172,6 +181,12 @@ var v1alpha8OpenStackClusterRestorer = conversion.RestorerFor[*infrav1.OpenStack
172181
},
173182
restorev1alpha8ClusterStatus,
174183
),
184+
"spec": conversion.HashedFieldRestorer(
185+
func(c *infrav1.OpenStackCluster) *infrav1.OpenStackClusterSpec {
186+
return &c.Spec
187+
},
188+
restorev1alpha8ClusterSpec,
189+
),
175190
}
176191

177192
func (r *OpenStackCluster) ConvertTo(dstRaw ctrlconversion.Hub) error {
@@ -510,6 +525,11 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *
510525
}
511526
}
512527

528+
if len(in.NodeCIDRs) > 0 {
529+
out.NodeCIDR = in.NodeCIDRs[0].CIDR
530+
out.DNSNameservers = in.NodeCIDRs[0].DNSNameservers
531+
}
532+
513533
return nil
514534
}
515535

@@ -534,6 +554,16 @@ func Convert_v1alpha6_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
534554
out.Subnets = []infrav1.SubnetFilter{subnet}
535555
}
536556

557+
// DNSNameservers without NodeCIDR doesn't make sense, so we drop that.
558+
if len(in.NodeCIDR) > 0 {
559+
out.NodeCIDRs = []infrav1.SubnetSpec{
560+
{
561+
CIDR: in.NodeCIDR,
562+
DNSNameservers: in.DNSNameservers,
563+
},
564+
}
565+
}
566+
537567
return nil
538568
}
539569

api/v1alpha6/conversion_test.go

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

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: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ import (
2626

2727
var _ ctrlconversion.Convertible = &OpenStackCluster{}
2828

29-
var v1alpha7OpenStackClusterRestorer = conversion.RestorerFor[*OpenStackCluster]{}
29+
var v1alpha7OpenStackClusterRestorer = conversion.RestorerFor[*OpenStackCluster]{
30+
"spec": conversion.HashedFieldRestorer(
31+
func(c *OpenStackCluster) *OpenStackClusterSpec {
32+
return &c.Spec
33+
},
34+
restorev1alpha7ClusterSpec,
35+
36+
// Filter out Bastion, which is restored separately
37+
conversion.HashedFilterField[*OpenStackCluster, OpenStackClusterSpec](
38+
func(s *OpenStackClusterSpec) *OpenStackClusterSpec {
39+
if s.Bastion != nil {
40+
f := *s
41+
f.Bastion = nil
42+
return &f
43+
}
44+
return s
45+
},
46+
),
47+
),
48+
}
3049

3150
var v1alpha8OpenStackClusterRestorer = conversion.RestorerFor[*infrav1.OpenStackCluster]{
3251
"bastion": conversion.HashedFieldRestorer(
@@ -80,6 +99,19 @@ func restorev1alpha8Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
8099
}
81100
}
82101

102+
func restorev1alpha7ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackClusterSpec) {
103+
prevBastion := previous.Bastion
104+
dstBastion := dst.Bastion
105+
if prevBastion != nil && dstBastion != nil {
106+
restorev1alpha7MachineSpec(&prevBastion.Instance, &dstBastion.Instance)
107+
}
108+
109+
// We only restore DNSNameservers when these were lossly converted when NodeCIDR is empty.
110+
if len(previous.DNSNameservers) > 0 && len(dst.NodeCIDR) == 0 {
111+
dst.DNSNameservers = previous.DNSNameservers
112+
}
113+
}
114+
83115
func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *infrav1.OpenStackClusterSpec) {
84116
prevBastion := previous.Bastion
85117
dstBastion := dst.Bastion
@@ -101,6 +133,8 @@ func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *inf
101133
if len(previous.Subnets) > 1 {
102134
dst.Subnets = append(dst.Subnets, previous.Subnets[1:]...)
103135
}
136+
137+
dst.NodeCIDRs = previous.NodeCIDRs
104138
}
105139

106140
func (r *OpenStackCluster) ConvertTo(dstRaw ctrlconversion.Hub) error {
@@ -144,7 +178,18 @@ func restorev1alpha8ClusterTemplateSpec(previous *infrav1.OpenStackClusterTempla
144178
restorev1alpha8ClusterSpec(&previous.Template.Spec, &dst.Template.Spec)
145179
}
146180

147-
var v1alpha7OpenStackClusterTemplateRestorer = conversion.RestorerFor[*OpenStackClusterTemplate]{}
181+
func restorev1alpha7ClusterTemplateSpec(previous *OpenStackClusterTemplateSpec, dst *OpenStackClusterTemplateSpec) {
182+
restorev1alpha7ClusterSpec(&previous.Template.Spec, &dst.Template.Spec)
183+
}
184+
185+
var v1alpha7OpenStackClusterTemplateRestorer = conversion.RestorerFor[*OpenStackClusterTemplate]{
186+
"spec": conversion.HashedFieldRestorer(
187+
func(c *OpenStackClusterTemplate) *OpenStackClusterTemplateSpec {
188+
return &c.Spec
189+
},
190+
restorev1alpha7ClusterTemplateSpec,
191+
),
192+
}
148193

149194
var v1alpha8OpenStackClusterTemplateRestorer = conversion.RestorerFor[*infrav1.OpenStackClusterTemplate]{
150195
"spec": conversion.HashedFieldRestorer(
@@ -396,6 +441,16 @@ func Convert_v1alpha7_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
396441
out.Subnets = []infrav1.SubnetFilter{subnet}
397442
}
398443

444+
// DNSNameservers without NodeCIDR doesn't make sense, so we drop that.
445+
if len(in.NodeCIDR) > 0 {
446+
out.NodeCIDRs = []infrav1.SubnetSpec{
447+
{
448+
CIDR: in.NodeCIDR,
449+
DNSNameservers: in.DNSNameservers,
450+
},
451+
}
452+
}
453+
399454
return nil
400455
}
401456

@@ -415,5 +470,10 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha7_OpenStackClusterSpec(in *
415470
}
416471
}
417472

473+
if len(in.NodeCIDRs) > 0 {
474+
out.NodeCIDR = in.NodeCIDRs[0].CIDR
475+
out.DNSNameservers = in.NodeCIDRs[0].DNSNameservers
476+
}
477+
418478
return nil
419479
}

api/v1alpha7/conversion_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ func TestFuzzyConversion(t *testing.T) {
6969
spec.Subnets = append(spec.Subnets, subnet)
7070
}
7171
},
72+
73+
func(spec *infrav1.SubnetSpec, c fuzz.Continue) {
74+
c.FuzzNoCustom(spec)
75+
76+
// CIDR is required and API validates that it's present, so
77+
// we force it to always be set.
78+
if len(spec.CIDR) == 0 {
79+
spec.CIDR = c.RandString()
80+
}
81+
},
7282
}
7383
}
7484

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: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ 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+
// NodeCIDRs 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+
NodeCIDRs []SubnetSpec `json:"nodeCIDRs,omitempty"`
4142

4243
// If NodeCIDR is set this option can be used to detect an existing router.
4344
// If specified, no new router will be created.
@@ -58,11 +59,6 @@ type OpenStackClusterSpec struct {
5859
// +optional
5960
NetworkMTU int `json:"networkMtu,omitempty"`
6061

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"`
6662
// ExternalRouterIPs is an array of externalIPs on the respective subnets.
6763
// This is necessary if the router needs a fixed ip in a specific subnet.
6864
ExternalRouterIPs []ExternalRouterIPParam `json:"externalRouterIPs,omitempty"`

api/v1alpha8/types.go

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

90+
type SubnetSpec struct {
91+
// +required
92+
CIDR string `json:"cidr"`
93+
DNSNameservers []string `json:"dnsNameservers,omitempty"`
94+
}
95+
9096
type PortOpts struct {
9197
// Network is a query for an openstack network that the port will be created or discovered on.
9298
// This will fail if the query returns more than one network.

0 commit comments

Comments
 (0)