Skip to content

Commit ab169ba

Browse files
committed
Allow for filtering subnets rather than passing an UUID
Requesting the subnetID is very limiting as it requires to create subnets in advance and know the uuid. We allow for filtering netwoks and we should allow for filtering subnets too. This will make the API more consistent.
1 parent 79b6922 commit ab169ba

File tree

6 files changed

+244
-21
lines changed

6 files changed

+244
-21
lines changed

config/crds/openstackproviderconfig_v1alpha1_openstackclusterproviderspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ spec:
2424
type: string
2525
kind:
2626
type: string
27+
managedSecurityGroups:
28+
type: boolean
2729
metadata:
2830
type: object
2931
nodeCidr:
3032
type: string
33+
required:
34+
- managedSecurityGroups
3135
version: v1alpha1
3236
status:
3337
acceptedNames:

config/crds/openstackproviderconfig_v1alpha1_openstackclusterproviderstatus.yaml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,98 @@ spec:
1616
properties:
1717
apiVersion:
1818
type: string
19+
controlPlaneSecurityGroup:
20+
properties:
21+
id:
22+
type: string
23+
name:
24+
type: string
25+
rules:
26+
items:
27+
properties:
28+
direction:
29+
type: string
30+
etherType:
31+
type: string
32+
name:
33+
type: string
34+
portRangeMax:
35+
format: int64
36+
type: integer
37+
portRangeMin:
38+
format: int64
39+
type: integer
40+
protocol:
41+
type: string
42+
remoteGroupID:
43+
type: string
44+
remoteIPPrefix:
45+
type: string
46+
securityGroupID:
47+
type: string
48+
required:
49+
- name
50+
- direction
51+
- etherType
52+
- securityGroupID
53+
- portRangeMin
54+
- portRangeMax
55+
- protocol
56+
- remoteGroupID
57+
- remoteIPPrefix
58+
type: object
59+
type: array
60+
required:
61+
- name
62+
- id
63+
- rules
64+
type: object
65+
globalSecurityGroup:
66+
properties:
67+
id:
68+
type: string
69+
name:
70+
type: string
71+
rules:
72+
items:
73+
properties:
74+
direction:
75+
type: string
76+
etherType:
77+
type: string
78+
name:
79+
type: string
80+
portRangeMax:
81+
format: int64
82+
type: integer
83+
portRangeMin:
84+
format: int64
85+
type: integer
86+
protocol:
87+
type: string
88+
remoteGroupID:
89+
type: string
90+
remoteIPPrefix:
91+
type: string
92+
securityGroupID:
93+
type: string
94+
required:
95+
- name
96+
- direction
97+
- etherType
98+
- securityGroupID
99+
- portRangeMin
100+
- portRangeMax
101+
- protocol
102+
- remoteGroupID
103+
- remoteIPPrefix
104+
type: object
105+
type: array
106+
required:
107+
- name
108+
- id
109+
- rules
110+
type: object
19111
kind:
20112
type: string
21113
metadata:

config/crds/openstackproviderconfig_v1alpha1_openstackproviderspec.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ spec:
5555
tags:
5656
type: string
5757
type: object
58+
subnets:
59+
items:
60+
properties:
61+
filter:
62+
type: object
63+
uuid:
64+
type: string
65+
type: object
66+
type: array
5867
uuid:
5968
type: string
6069
type: object
@@ -65,6 +74,8 @@ spec:
6574
type: array
6675
sshUserName:
6776
type: string
77+
trunk:
78+
type: boolean
6879
userDataSecret:
6980
type: object
7081
required:

pkg/apis/openstackproviderconfig/v1alpha1/types.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ type NetworkParam struct {
7979
// Filters for optional network query
8080
Filter Filter `json:"filter,omitempty"`
8181
// Subnet within a network to use
82-
SubnetID string `json:"subnet_id,omitempty"`
82+
Subnets []SubnetParam `json:"subnets,omitempty"`
8383
}
8484

8585
type Filter struct {
@@ -101,6 +101,37 @@ type Filter struct {
101101
NotTagsAny string `json:"not-tags-any,omitempty"`
102102
}
103103

104+
type SubnetParam struct {
105+
// The UUID of the network. Required if you omit the port attribute.
106+
UUID string `json:"uuid,omitempty"`
107+
108+
// Filters for optional network query
109+
Filter SubnetFilter `json:"filter,omitempty"`
110+
}
111+
112+
type SubnetFilter struct {
113+
Name string `json:"name"`
114+
EnableDHCP *bool `json:"enable_dhcp"`
115+
NetworkID string `json:"network_id"`
116+
TenantID string `json:"tenant_id"`
117+
ProjectID string `json:"project_id"`
118+
IPVersion int `json:"ip_version"`
119+
GatewayIP string `json:"gateway_ip"`
120+
CIDR string `json:"cidr"`
121+
IPv6AddressMode string `json:"ipv6_address_mode"`
122+
IPv6RAMode string `json:"ipv6_ra_mode"`
123+
ID string `json:"id"`
124+
SubnetPoolID string `json:"subnetpool_id"`
125+
Limit int `json:"limit"`
126+
Marker string `json:"marker"`
127+
SortKey string `json:"sort_key"`
128+
SortDir string `json:"sort_dir"`
129+
Tags string `json:"tags"`
130+
TagsAny string `json:"tags-any"`
131+
NotTags string `json:"not-tags"`
132+
NotTagsAny string `json:"not-tags-any"`
133+
}
134+
104135
type RootVolume struct {
105136
VolumeType string `json:"volumeType"`
106137
Size int `json:"diskSize,omitempty"`

pkg/apis/openstackproviderconfig/v1alpha1/zz_generated.deepcopy.go

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

pkg/cloud/openstack/clients/machineservice.go

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,31 @@ func getNetworkIDsByFilter(is *InstanceService, opts *networks.ListOpts) ([]stri
259259
return uuids, nil
260260
}
261261

262+
// A function for getting the id of a subnet by querying openstack with filters
263+
func getSubnetsByFilter(is *InstanceService, opts *subnets.ListOpts) ([]subnets.Subnet, error) {
264+
if opts == nil {
265+
return []subnets.Subnet{}, fmt.Errorf("No Filters were passed")
266+
}
267+
pager := subnets.List(is.networkClient, opts)
268+
var snets []subnets.Subnet
269+
err := pager.EachPage(func(page pagination.Page) (bool, error) {
270+
subnetList, err := subnets.ExtractSubnets(page)
271+
if err != nil {
272+
return false, err
273+
} else if len(subnetList) == 0 {
274+
return false, fmt.Errorf("No subnets could be found with the filters provided")
275+
}
276+
for _, subnet := range subnetList {
277+
snets = append(snets, subnet)
278+
}
279+
return true, nil
280+
})
281+
if err != nil {
282+
return []subnets.Subnet{}, err
283+
}
284+
return snets, nil
285+
}
286+
262287
func CreatePort(is *InstanceService, name string, net ServerNetwork, securityGroups *[]string) (ports.Port, error) {
263288
portCreateOpts := ports.CreateOpts{
264289
Name: name,
@@ -295,32 +320,36 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, confi
295320
// Get all network UUIDs
296321
var nets []ServerNetwork
297322
for _, net := range config.Networks {
298-
if net.UUID == "" && net.SubnetID == "" {
299-
opts := networks.ListOpts(net.Filter)
300-
ids, err := getNetworkIDsByFilter(is, &opts)
301-
if err != nil {
302-
return nil, err
303-
}
304-
for _, netID := range ids {
323+
opts := networks.ListOpts(net.Filter)
324+
opts.ID = net.UUID
325+
ids, err := getNetworkIDsByFilter(is, &opts)
326+
if err != nil {
327+
return nil, err
328+
}
329+
for _, netID := range ids {
330+
if net.Subnets == nil {
305331
nets = append(nets, ServerNetwork{
306332
networkID: netID,
307333
})
308334
}
309-
} else if net.UUID == "" && net.SubnetID != "" {
310-
subnet, err := subnets.Get(is.networkClient, net.SubnetID).Extract()
311-
if err != nil {
312-
return nil, err
335+
336+
for _, snet := range net.Subnets {
337+
sopts := subnets.ListOpts(snet.Filter)
338+
sopts.ID = netID
339+
sopts.NetworkID = net.UUID
340+
snets, err := getSubnetsByFilter(is, &sopts)
341+
if err != nil {
342+
return nil, err
343+
}
344+
for _, snet := range snets {
345+
nets = append(nets, ServerNetwork{
346+
networkID: snet.NetworkID,
347+
subnetID: snet.ID,
348+
})
349+
}
313350
}
314-
nets = append(nets, ServerNetwork{
315-
networkID: subnet.NetworkID,
316-
subnetID: net.SubnetID,
317-
})
318-
} else {
319-
nets = append(nets, ServerNetwork{
320-
networkID: net.UUID,
321-
subnetID: net.SubnetID,
322-
})
323351
}
352+
324353
if len(net.Filter.Tags) > 0 {
325354
clusterTags = append(clusterTags, net.Filter.Tags)
326355
}

0 commit comments

Comments
 (0)