Skip to content

Commit

Permalink
Add support to set allocation_pools for subnet
Browse files Browse the repository at this point in the history
This commit adds API that allows users to set `allocations_pools` in the
subnet created by CAPO. This allows the users to restrict the IP address
ranges that will be allocated automatically by OpenStack when creating
Machines.

Users can utilize this to reserve addresses for VIPs (virtual IPs) or
special nodes that will have predefined addresses and will be created
later.
  • Loading branch information
dulek committed Jan 30, 2024
1 parent e89ec9c commit 8e64c5d
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/v1alpha5/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions api/v1alpha6/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ var v1alpha8OpenStackClusterRestorer = conversion.RestorerFor[*infrav1.OpenStack
return &c.Spec.NetworkMTU
},
),
"allocationPools": conversion.UnconditionalFieldRestorer(
func(c *infrav1.OpenStackCluster) *[]infrav1.AllocationPool {
return &c.Spec.AllocationPools
},
),
"bastion": conversion.HashedFieldRestorer(
func(c *infrav1.OpenStackCluster) **infrav1.Bastion {
return &c.Spec.Bastion
Expand Down Expand Up @@ -240,6 +245,11 @@ var v1alpha8OpenStackClusterTemplateRestorer = conversion.RestorerFor[*infrav1.O
return &c.Spec.Template.Spec.NetworkMTU
},
),
"allocationPools": conversion.UnconditionalFieldRestorer(
func(c *infrav1.OpenStackClusterTemplate) *[]infrav1.AllocationPool {
return &c.Spec.Template.Spec.AllocationPools
},
),
"bastion": conversion.HashedFieldRestorer(
func(c *infrav1.OpenStackClusterTemplate) **infrav1.Bastion {
return &c.Spec.Template.Spec.Bastion
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha6/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/v1alpha7/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func restorev1alpha8ClusterSpec(previous *infrav1.OpenStackClusterSpec, dst *inf

dst.DisableExternalNetwork = previous.DisableExternalNetwork

dst.AllocationPools = previous.AllocationPools

if len(previous.Subnets) > 1 {
dst.Subnets = append(dst.Subnets, previous.Subnets[1:]...)
}
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha7/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/v1alpha8/openstackcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ type OpenStackClusterSpec struct {
// through DNS is required.
// +listType=set
DNSNameservers []string `json:"dnsNameservers,omitempty"`

// AllocationPools is an array of AllocationPool objects that will be applied to OpenStack Subnet being created.
// If set, OpenStack will only allocate these IPs for Machines. It will still be possible to create ports from
// outside of these ranges manually.
AllocationPools []AllocationPool `json:"allocationPools,omitempty"`

// ExternalRouterIPs is an array of externalIPs on the respective subnets.
// This is necessary if the router needs a fixed ip in a specific subnet.
ExternalRouterIPs []ExternalRouterIPParam `json:"externalRouterIPs,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha8/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ type RouterFilter struct {
NotTagsAny string `json:"notTagsAny,omitempty"`
}

type AllocationPool struct {
Start string `json:"start"`
End string `json:"end"`
}

type PortOpts struct {
// Network is a query for an openstack network that the port will be created or discovered on.
// This will fail if the query returns more than one network.
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha8/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4838,6 +4838,22 @@ spec:
spec:
description: OpenStackClusterSpec defines the desired state of OpenStackCluster.
properties:
allocationPools:
description: |-
AllocationPools is an array of AllocationPool objects that will be applied to OpenStack Subnet being created.
If set, OpenStack will only allocate these IPs for Machines. It will still be possible to create ports from
outside of these ranges manually.
items:
properties:
end:
type: string
start:
type: string
required:
- end
- start
type: object
type: array
allowAllInClusterTraffic:
description: |-
AllowAllInClusterTraffic is only used when managed security groups are in use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,22 @@ spec:
description: OpenStackClusterSpec defines the desired state of
OpenStackCluster.
properties:
allocationPools:
description: |-
AllocationPools is an array of AllocationPool objects that will be applied to OpenStack Subnet being created.
If set, OpenStack will only allocate these IPs for Machines. It will still be possible to create ports from
outside of these ranges manually.
items:
properties:
end:
type: string
start:
type: string
required:
- end
- start
type: object
type: array
allowAllInClusterTraffic:
description: |-
AllowAllInClusterTraffic is only used when managed security groups are in use.
Expand Down
6 changes: 5 additions & 1 deletion docs/book/src/topics/crd-changes/v1alpha7-to-v1alpha8.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ In v1alpha8, this will be automatically converted to:

`Subnets` allows specifications of maximum two `SubnetFilter` one being IPv4 and the other IPv6. Both subnets must be on the same network. Any filtered subnets will be added to `OpenStackCluster.Status.Network.Subnets`.

When subnets are not specified on `OpenStackCluster` and only the network is, the network is used to identify the subnets to use. If more than two subnets exist in the network, the user must specify which ones to use by defining the `OpenStackCluster.Spec.Subnets` field.
When subnets are not specified on `OpenStackCluster` and only the network is, the network is used to identify the subnets to use. If more than two subnets exist in the network, the user must specify which ones to use by defining the `OpenStackCluster.Spec.Subnets` field.

#### Addition of allocationPools

In v1alpha8, an `AllocationPools` property is introduced to `OpenStackClusterSpec`. 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.
4 changes: 4 additions & 0 deletions pkg/cloud/services/networking/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, clust
Description: names.GetDescription(clusterName),
}

for _, pool := range openStackCluster.Spec.AllocationPools {
opts.AllocationPools = append(opts.AllocationPools, subnets.AllocationPool{Start: pool.Start, End: pool.End})
}

subnet, err := s.client.CreateSubnet(opts)
if err != nil {
record.Warnf(openStackCluster, "FailedCreateSubnet", "Failed to create subnet %s: %v", name, err)
Expand Down
Loading

0 comments on commit 8e64c5d

Please sign in to comment.