Skip to content

Commit e5f7a60

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 e5f7a60

23 files changed

+261
-84
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.ManagedSubnets) > 0 {
200+
out.NodeCIDR = in.ManagedSubnets[0].CIDR
201+
out.DNSNameservers = in.ManagedSubnets[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.ManagedSubnets = []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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackCl
127127
}
128128
}
129129

130+
// We only restore DNSNameservers when these were lossly converted when NodeCIDR is empty.
131+
if len(previous.DNSNameservers) > 0 && dst.NodeCIDR == "" {
132+
dst.DNSNameservers = previous.DNSNameservers
133+
}
134+
130135
prevBastion := previous.Bastion
131136
dstBastion := dst.Bastion
132137
if prevBastion != nil && dstBastion != nil {
@@ -545,6 +550,11 @@ func Convert_v1alpha8_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *
545550
}
546551
}
547552

553+
if len(in.ManagedSubnets) > 0 {
554+
out.NodeCIDR = in.ManagedSubnets[0].CIDR
555+
out.DNSNameservers = in.ManagedSubnets[0].DNSNameservers
556+
}
557+
548558
return nil
549559
}
550560

@@ -569,6 +579,16 @@ func Convert_v1alpha6_OpenStackClusterSpec_To_v1alpha8_OpenStackClusterSpec(in *
569579
out.Subnets = []infrav1.SubnetFilter{subnet}
570580
}
571581

582+
// DNSNameservers without NodeCIDR doesn't make sense, so we drop that.
583+
if len(in.NodeCIDR) > 0 {
584+
out.ManagedSubnets = []infrav1.SubnetSpec{
585+
{
586+
CIDR: in.NodeCIDR,
587+
DNSNameservers: in.DNSNameservers,
588+
},
589+
}
590+
}
591+
572592
return nil
573593
}
574594

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 spec.CIDR == "" {
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 && dst.NodeCIDR == "" {
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.ManagedSubnets = previous.ManagedSubnets
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.ManagedSubnets = []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.ManagedSubnets) > 0 {
532+
out.NodeCIDR = in.ManagedSubnets[0].CIDR
533+
out.DNSNameservers = in.ManagedSubnets[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 spec.CIDR == "" {
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 & 9 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.
39-
// If you leave this empty, no network will be created.
40-
NodeCIDR string `json:"nodeCidr,omitempty"`
37+
// ManagedSubnets describe OpenStack Subnets to be created. Cluster actuator will create a network,
38+
// subnets with the defined CIDR, and a router connected to these subnets. Currently only one IPv4
39+
// subnet is supported. If you leave this empty, no network will be created.
40+
// +kubebuilder:validation:MaxItems=1
41+
ManagedSubnets []SubnetSpec `json:"managedSubnets,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: 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.

api/v1alpha8/zz_generated.deepcopy.go

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

0 commit comments

Comments
 (0)