Skip to content

Commit 1b0f1ea

Browse files
authored
Merge pull request #1847 from shiftstack/allocation-pools
✨ Add support to set `allocation_pools` for subnet
2 parents fd37971 + ccfcb59 commit 1b0f1ea

File tree

10 files changed

+381
-0
lines changed

10 files changed

+381
-0
lines changed

api/v1alpha6/conversion.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ var v1alpha8OpenStackClusterRestorer = conversion.RestorerFor[*infrav1.OpenStack
235235
},
236236
restorev1alpha8ClusterStatus,
237237
),
238+
"managedSubnets": conversion.UnconditionalFieldRestorer(
239+
func(c *infrav1.OpenStackCluster) *[]infrav1.SubnetSpec {
240+
return &c.Spec.ManagedSubnets
241+
},
242+
),
238243
}
239244

240245
func (r *OpenStackCluster) ConvertTo(dstRaw ctrlconversion.Hub) error {
@@ -325,6 +330,11 @@ var v1alpha8OpenStackClusterTemplateRestorer = conversion.RestorerFor[*infrav1.O
325330
},
326331
restorev1alpha8ManagedSecurityGroups,
327332
),
333+
"managedSubnets": conversion.UnconditionalFieldRestorer(
334+
func(c *infrav1.OpenStackClusterTemplate) *[]infrav1.SubnetSpec {
335+
return &c.Spec.Template.Spec.ManagedSubnets
336+
},
337+
),
328338
}
329339

330340
func (r *OpenStackClusterTemplate) ConvertTo(dstRaw ctrlconversion.Hub) error {

api/v1alpha6/conversion_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ func TestFuzzyConversion(t *testing.T) {
142142
spec.CIDR = c.RandString()
143143
}
144144
},
145+
146+
func(pool *infrav1.AllocationPool, c fuzz.Continue) {
147+
c.FuzzNoCustom(pool)
148+
149+
// Start and End are required properties, let's make sure both are set
150+
for pool.Start == "" {
151+
pool.Start = c.RandString()
152+
}
153+
154+
for pool.End == "" {
155+
pool.End = c.RandString()
156+
}
157+
},
145158
}
146159
}
147160

api/v1alpha7/conversion_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ func TestFuzzyConversion(t *testing.T) {
110110
spec.CIDR = c.RandString()
111111
}
112112
},
113+
114+
func(pool *infrav1.AllocationPool, c fuzz.Continue) {
115+
c.FuzzNoCustom(pool)
116+
117+
// Start and End are required properties, let's make sure both are set
118+
for pool.Start == "" {
119+
pool.Start = c.RandString()
120+
}
121+
122+
for pool.End == "" {
123+
pool.End = c.RandString()
124+
}
125+
},
113126
}
114127
}
115128

api/v1alpha8/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,25 @@ type SubnetSpec struct {
9292
// This field is required when defining a subnet.
9393
// +required
9494
CIDR string `json:"cidr"`
95+
9596
// DNSNameservers holds a list of DNS server addresses that will be provided when creating
9697
// the subnet. These addresses need to have the same IP version as CIDR.
9798
DNSNameservers []string `json:"dnsNameservers,omitempty"`
99+
100+
// AllocationPools is an array of AllocationPool objects that will be applied to OpenStack Subnet being created.
101+
// If set, OpenStack will only allocate these IPs for Machines. It will still be possible to create ports from
102+
// outside of these ranges manually.
103+
AllocationPools []AllocationPool `json:"allocationPools,omitempty"`
104+
}
105+
106+
type AllocationPool struct {
107+
// Start represents the start of the AllocationPool, that is the lowest IP of the pool.
108+
// +required
109+
Start string `json:"start"`
110+
111+
// End represents the end of the AlloctionPool, that is the highest IP of the pool.
112+
// +required
113+
End string `json:"end"`
98114
}
99115

100116
type PortOpts struct {

api/v1alpha8/zz_generated.deepcopy.go

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

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5537,6 +5537,26 @@ spec:
55375537
subnet is supported. If you leave this empty, no network will be created.
55385538
items:
55395539
properties:
5540+
allocationPools:
5541+
description: |-
5542+
AllocationPools is an array of AllocationPool objects that will be applied to OpenStack Subnet being created.
5543+
If set, OpenStack will only allocate these IPs for Machines. It will still be possible to create ports from
5544+
outside of these ranges manually.
5545+
items:
5546+
properties:
5547+
end:
5548+
description: End represents the end of the AlloctionPool,
5549+
that is the highest IP of the pool.
5550+
type: string
5551+
start:
5552+
description: Start represents the start of the AllocationPool,
5553+
that is the lowest IP of the pool.
5554+
type: string
5555+
required:
5556+
- end
5557+
- start
5558+
type: object
5559+
type: array
55405560
cidr:
55415561
description: |-
55425562
CIDR is representing the IP address range used to create the subnet, e.g. 10.0.0.0/24.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,6 +2972,27 @@ spec:
29722972
subnet is supported. If you leave this empty, no network will be created.
29732973
items:
29742974
properties:
2975+
allocationPools:
2976+
description: |-
2977+
AllocationPools is an array of AllocationPool objects that will be applied to OpenStack Subnet being created.
2978+
If set, OpenStack will only allocate these IPs for Machines. It will still be possible to create ports from
2979+
outside of these ranges manually.
2980+
items:
2981+
properties:
2982+
end:
2983+
description: End represents the end of the AlloctionPool,
2984+
that is the highest IP of the pool.
2985+
type: string
2986+
start:
2987+
description: Start represents the start of the
2988+
AllocationPool, that is the lowest IP of the
2989+
pool.
2990+
type: string
2991+
required:
2992+
- end
2993+
- start
2994+
type: object
2995+
type: array
29752996
cidr:
29762997
description: |-
29772998
CIDR is representing the IP address range used to create the subnet, e.g. 10.0.0.0/24.

docs/book/src/topics/crd-changes/v1alpha7-to-v1alpha8.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ In v1alpha8, this will be automatically converted to:
210210

211211
Please note that currently `managedSubnets` can only hold one element.
212212

213+
#### Addition of allocationPools
214+
215+
In v1alpha8, an `AllocationPools` property is introduced to `OpenStackCluster.Spec.ManagedSubnets`. When specified, OpenStack subnet created by CAPO will have the given values set as the `allocation_pools` property. This allows users to make sure OpenStack will not allocate some IP ranges in the subnet automatically. If the subnet is precreated and configured, CAPO will ignore `AllocationPools` property.
216+
213217
#### Change to managedSecurityGroups
214218

215219
The field `managedSecurityGroups` is now a pointer to a `ManagedSecurityGroups` object rather than a boolean.

pkg/cloud/services/networking/network.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, clust
256256
Description: names.GetDescription(clusterName),
257257
}
258258

259+
for _, pool := range openStackCluster.Spec.ManagedSubnets[0].AllocationPools {
260+
opts.AllocationPools = append(opts.AllocationPools, subnets.AllocationPool{Start: pool.Start, End: pool.End})
261+
}
262+
259263
subnet, err := s.client.CreateSubnet(opts)
260264
if err != nil {
261265
record.Warnf(openStackCluster, "FailedCreateSubnet", "Failed to create subnet %s: %v", name, err)

0 commit comments

Comments
 (0)