Skip to content

Commit 357e525

Browse files
committed
Allow subnet without network
When specifying a subnet filter without a network filter we would first execute a network list with no filter, which returns all networks, then execute the subnet filter for each network. With this change we: * Don't filter subnets by network if there is no network filter * Don't execute a network query if we have a network uuid * Don't execute a subnet query if we have a subnet uuid The changes to unit tests are: * Don't expect gophercloud calls which we no longer make * "Subnet without network" now returns subnets instead of error
1 parent c8e4966 commit 357e525

7 files changed

+126
-132
lines changed

api/v1alpha4/types.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ type SecurityGroupFilter struct {
5555
}
5656

5757
type NetworkParam struct {
58-
// The UUID of the network. Required if you omit the port attribute.
58+
// Optional UUID of the network.
59+
// If specified this will not be validated prior to server creation.
60+
// Required if `Subnets` specifies a subnet by UUID.
5961
UUID string `json:"uuid,omitempty"`
6062
// A fixed IPv4 address for the NIC.
6163
FixedIP string `json:"fixedIP,omitempty"`
@@ -85,10 +87,12 @@ type Filter struct {
8587
}
8688

8789
type SubnetParam struct {
88-
// The UUID of the network. Required if you omit the port attribute.
90+
// Optional UUID of the subnet.
91+
// If specified this will not be validated prior to server creation.
92+
// If specified, the enclosing `NetworkParam` must also be specified by UUID.
8993
UUID string `json:"uuid,omitempty"`
9094

91-
// Filters for optional network query
95+
// Filters for optional subnet query
9296
Filter SubnetFilter `json:"filter,omitempty"`
9397
}
9498

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ spec:
12111211
items:
12121212
properties:
12131213
filter:
1214-
description: Filters for optional network query
1214+
description: Filters for optional subnet query
12151215
properties:
12161216
cidr:
12171217
type: string
@@ -1257,14 +1257,17 @@ spec:
12571257
type: string
12581258
type: object
12591259
uuid:
1260-
description: The UUID of the network. Required
1261-
if you omit the port attribute.
1260+
description: Optional UUID of the subnet. If specified
1261+
this will not be validated prior to server creation.
1262+
If specified, the enclosing `NetworkParam` must
1263+
also be specified by UUID.
12621264
type: string
12631265
type: object
12641266
type: array
12651267
uuid:
1266-
description: The UUID of the network. Required if you
1267-
omit the port attribute.
1268+
description: Optional UUID of the network. If specified
1269+
this will not be validated prior to server creation.
1270+
Required if `Subnets` specifies a subnet by UUID.
12681271
type: string
12691272
type: object
12701273
type: array
@@ -1508,7 +1511,7 @@ spec:
15081511
Gateway of this router
15091512
properties:
15101513
filter:
1511-
description: Filters for optional network query
1514+
description: Filters for optional subnet query
15121515
properties:
15131516
cidr:
15141517
type: string
@@ -1554,8 +1557,10 @@ spec:
15541557
type: string
15551558
type: object
15561559
uuid:
1557-
description: The UUID of the network. Required if you omit
1558-
the port attribute.
1560+
description: Optional UUID of the subnet. If specified this
1561+
will not be validated prior to server creation. If specified,
1562+
the enclosing `NetworkParam` must also be specified by
1563+
UUID.
15591564
type: string
15601565
type: object
15611566
required:

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ spec:
194194
items:
195195
properties:
196196
filter:
197-
description: Filters for optional network
197+
description: Filters for optional subnet
198198
query
199199
properties:
200200
cidr:
@@ -241,14 +241,19 @@ spec:
241241
type: string
242242
type: object
243243
uuid:
244-
description: The UUID of the network.
245-
Required if you omit the port attribute.
244+
description: Optional UUID of the subnet.
245+
If specified this will not be validated
246+
prior to server creation. If specified,
247+
the enclosing `NetworkParam` must also
248+
be specified by UUID.
246249
type: string
247250
type: object
248251
type: array
249252
uuid:
250-
description: The UUID of the network. Required
251-
if you omit the port attribute.
253+
description: Optional UUID of the network. If
254+
specified this will not be validated prior
255+
to server creation. Required if `Subnets`
256+
specifies a subnet by UUID.
252257
type: string
253258
type: object
254259
type: array
@@ -501,7 +506,7 @@ spec:
501506
for the Gateway of this router
502507
properties:
503508
filter:
504-
description: Filters for optional network query
509+
description: Filters for optional subnet query
505510
properties:
506511
cidr:
507512
type: string
@@ -547,8 +552,10 @@ spec:
547552
type: string
548553
type: object
549554
uuid:
550-
description: The UUID of the network. Required if
551-
you omit the port attribute.
555+
description: Optional UUID of the subnet. If specified
556+
this will not be validated prior to server creation.
557+
If specified, the enclosing `NetworkParam` must
558+
also be specified by UUID.
552559
type: string
553560
type: object
554561
required:

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ spec:
487487
items:
488488
properties:
489489
filter:
490-
description: Filters for optional network query
490+
description: Filters for optional subnet query
491491
properties:
492492
cidr:
493493
type: string
@@ -533,14 +533,17 @@ spec:
533533
type: string
534534
type: object
535535
uuid:
536-
description: The UUID of the network. Required if you
537-
omit the port attribute.
536+
description: Optional UUID of the subnet. If specified
537+
this will not be validated prior to server creation.
538+
If specified, the enclosing `NetworkParam` must also
539+
be specified by UUID.
538540
type: string
539541
type: object
540542
type: array
541543
uuid:
542-
description: The UUID of the network. Required if you omit the
543-
port attribute.
544+
description: Optional UUID of the network. If specified this
545+
will not be validated prior to server creation. Required if
546+
`Subnets` specifies a subnet by UUID.
544547
type: string
545548
type: object
546549
type: array

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ spec:
432432
items:
433433
properties:
434434
filter:
435-
description: Filters for optional network query
435+
description: Filters for optional subnet query
436436
properties:
437437
cidr:
438438
type: string
@@ -478,14 +478,17 @@ spec:
478478
type: string
479479
type: object
480480
uuid:
481-
description: The UUID of the network. Required
482-
if you omit the port attribute.
481+
description: Optional UUID of the subnet. If specified
482+
this will not be validated prior to server creation.
483+
If specified, the enclosing `NetworkParam` must
484+
also be specified by UUID.
483485
type: string
484486
type: object
485487
type: array
486488
uuid:
487-
description: The UUID of the network. Required if you
488-
omit the port attribute.
489+
description: Optional UUID of the network. If specified
490+
this will not be validated prior to server creation.
491+
Required if `Subnets` specifies a subnet by UUID.
489492
type: string
490493
type: object
491494
type: array

pkg/cloud/services/compute/instance.go

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -313,40 +313,73 @@ func applyServerGroupID(opts servers.CreateOptsBuilder, serverGroupID string) se
313313

314314
func (s *Service) getServerNetworks(networkParams []infrav1.NetworkParam) ([]infrav1.Network, error) {
315315
var nets []infrav1.Network
316-
for _, networkParam := range networkParams {
317-
opts := networks.ListOpts(networkParam.Filter)
318-
opts.ID = networkParam.UUID
319-
ids, err := s.networkingService.GetNetworkIDsByFilter(&opts)
320-
if err != nil {
321-
return nil, err
316+
317+
addSubnet := func(netID, subnetID string) {
318+
nets = append(nets, infrav1.Network{
319+
ID: netID,
320+
Subnet: &infrav1.Subnet{
321+
ID: subnetID,
322+
},
323+
})
324+
}
325+
326+
addSubnets := func(networkParam infrav1.NetworkParam, netID string) error {
327+
if len(networkParam.Subnets) == 0 && netID != "" {
328+
addSubnet(netID, "")
329+
return nil
322330
}
323-
for _, netID := range ids {
324-
if networkParam.Subnets == nil {
325-
nets = append(nets, infrav1.Network{
326-
ID: netID,
327-
Subnet: &infrav1.Subnet{},
328-
})
329-
continue
330-
}
331331

332-
for _, subnet := range networkParam.Subnets {
332+
for _, subnet := range networkParam.Subnets {
333+
// Don't lookup subnet if it was specified explicitly by UUID
334+
if subnet.UUID != "" {
335+
// If subnet was supplied by UUID then network
336+
// must also have been supplied by UUID.
337+
if netID == "" {
338+
return fmt.Errorf("validation error adding network for subnet %s: "+
339+
"network uuid must be specified when subnet uuid is specified", subnet.UUID)
340+
}
341+
342+
addSubnet(netID, subnet.UUID)
343+
} else {
333344
subnetOpts := subnets.ListOpts(subnet.Filter)
334-
subnetOpts.ID = subnet.UUID
335-
subnetOpts.NetworkID = netID
345+
if netID != "" {
346+
subnetOpts.NetworkID = netID
347+
}
336348
subnetsByFilter, err := s.networkingService.GetSubnetsByFilter(&subnetOpts)
337349
if err != nil {
338-
return nil, err
350+
return err
339351
}
340352
for _, subnetByFilter := range subnetsByFilter {
341-
nets = append(nets, infrav1.Network{
342-
ID: subnetByFilter.NetworkID,
343-
Subnet: &infrav1.Subnet{
344-
ID: subnetByFilter.ID,
345-
},
346-
})
353+
addSubnet(subnetByFilter.NetworkID, subnetByFilter.ID)
347354
}
348355
}
349356
}
357+
358+
return nil
359+
}
360+
361+
for _, networkParam := range networkParams {
362+
// Don't lookup network if we specified one explicitly by UUID
363+
// Don't lookup network if we didn't specify a network filter
364+
// If there is no explicit network UUID and no network filter,
365+
// we will look for subnets matching any given subnet filters in
366+
// all networks.
367+
if networkParam.UUID != "" || networkParam.Filter == (infrav1.Filter{}) {
368+
if err := addSubnets(networkParam, networkParam.UUID); err != nil {
369+
return nil, err
370+
}
371+
continue
372+
}
373+
opts := networks.ListOpts(networkParam.Filter)
374+
ids, err := s.networkingService.GetNetworkIDsByFilter(&opts)
375+
if err != nil {
376+
return nil, err
377+
}
378+
for _, netID := range ids {
379+
if err := addSubnets(networkParam, netID); err != nil {
380+
return nil, err
381+
}
382+
}
350383
}
351384
return nets, nil
352385
}

0 commit comments

Comments
 (0)