Skip to content

Commit

Permalink
Ensure custom mode VPC networks can be created
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 697135144
  • Loading branch information
m4r1k authored and copybara-github committed Nov 16, 2024
1 parent 4a8b7fa commit 518f5b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
11 changes: 9 additions & 2 deletions perfkitbenchmarker/providers/gcp/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,15 @@
'The name of an already created '
'network to use instead of creating a new one.',
)
GCE_NETWORK_TYPE = flags.DEFINE_string(
'gce_network_type', None, 'The network type or mode (i.e. auto, custom)'
GCE_NETWORK_TYPE = flags.DEFINE_enum(
'gce_network_type',
None,
[
'auto',
'custom',
'legacy',
],
'The subnet mode of the network (i.e. auto, custom, legacy)',
)
GCE_SUBNET_NAMES = flags.DEFINE_list(
'gce_subnet_name',
Expand Down
9 changes: 7 additions & 2 deletions perfkitbenchmarker/providers/gcp/gce_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,13 +918,18 @@ def __init__(self, network_spec: GceNetworkSpec):
self.subnet_resources = []
mode = gcp_flags.GCE_NETWORK_TYPE.value
self.subnet_resource = None

# Handle GCP network attachment on legacy networks.
if not self.is_existing_network or mode == 'legacy':
for name in self.subnet_names:
mode = 'auto'
if mode != 'custom':
mode = 'auto'
self.network_resources.append(
GceNetworkResource(name, mode, self.project, self.mtu)
)
else:

# Create GCP network subnet for custom mode.
if mode == 'custom':
subnet_region = util.GetRegionFromZone(network_spec.zone)
for name in self.subnet_names:
self.subnet_resources.append(
Expand Down
16 changes: 13 additions & 3 deletions tests/providers/gcp/gcp_network_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,12 @@ def testSpecifyNetworkName(self):
)
net = gce_network.GceNetwork.GetNetwork(vm)
self.assertTrue(net.is_existing_network)
self.assertEqual('my-network', net.network_resource.name)
self.assertEqual('my-network', net.subnet_resource.name)
name = (
net.subnet_resource.name
if net.subnet_resource
else net.network_resource.name
)
self.assertEqual('my-network', name)

@flagsaver.flagsaver(
gce_network_name=['my-network'],
Expand Down Expand Up @@ -410,8 +414,14 @@ def testSubnetFlagPrecedence(self):
subnet_names='default',
)
net = gce_network.GceNetwork.GetNetwork(vm)
name = (
net.subnet_resource.name
if net.subnet_resource
else net.network_resource.name
)

self.assertTrue(net.is_existing_network)
self.assertEqual('default', net.subnet_resource.name)
self.assertEqual('default', name)

########
# FireWall Names
Expand Down

0 comments on commit 518f5b5

Please sign in to comment.