sparse-iron-45589
04/14/2023, 8:48 AMsalmon-account-74572
04/14/2023, 1:31 PMsparse-iron-45589
04/14/2023, 2:22 PMsubnet = network.Subnet(
subnet_name,
subnet_name=subnet_name,
resource_group_name=resource_group_name,
virtual_network_name=existing_vnet.name,
address_prefix=subnet_prefix
)
subnet.virtual_network_name.apply(lambda v: <http://log.info|log.info>("Subnet virtual network is " + v))
subnet.resource_group_name.apply(lambda v: <http://log.info|log.info>("Subnet esource group name is " + v))
subnet.subnet_name.apply(lambda v: <http://log.info|log.info>("Subnet subnet_name is " + v))
if existing_vnet_id not in self.existing_vnets:
self.existing_vnets[existing_vnet_id] = network.VirtualNetwork.get(
f"vnet-{existing_vnet_id}",
id=existing_vnet_id
)
so what I am trying to do is get the VNet, add the first subnet with the next avaialble prefix and then loop again. however on the 2nd iteration of the loop, the VNet subnets are the same as before I created the new subnet.salmon-account-74572
04/14/2023, 2:27 PMsparse-iron-45589
04/14/2023, 2:34 PMdef _get_available_subnet_prefix(self, subnets, vnet_address_space):
used_ranges = [(ipaddress.ip_network(subnet.address_prefix).network_address,
ipaddress.ip_network(subnet.address_prefix).broadcast_address)
for subnet in subnets]
vnet_cidr = ipaddress.ip_network(vnet_address_space)
for prefix in vnet_cidr.subnets(new_prefix=24):
if all(prefix.network_address < used_range[0] or prefix.broadcast_address > used_range[1]
for used_range in used_ranges):
return str(prefix)
raise Exception(f"No available subnet found in {vnet_address_space}")
for cluster_config in environment['aks_clusters']:
existing_vnet_id = cluster_config.get('existing_vnet_id')
resource_group_name = cluster_config['resource_group_name']
vnet_address_space = cluster_config['vnet_address_space']
if existing_vnet_id is not None:
if existing_vnet_id not in self.existing_vnets:
self.existing_vnets[existing_vnet_id] = network.VirtualNetwork.get(
f"vnet-{existing_vnet_id}",
id=existing_vnet_id
)
existing_vnet = self.existing_vnets[existing_vnet_id]
existing_vnet.subnets.apply(lambda s: <http://log.info|log.info>("Length of subnets is " + str(len(s))))
existing_vnet.name.apply(lambda n: <http://log.info|log.info>("Virtual network name is " + n))
subnet_prefix = existing_vnet.subnets.apply(lambda subnets: self._get_available_subnet_prefix(subnets, vnet_address_space))
if subnet_prefix is None:
raise ValueError("No available subnet prefix found within the VNet")
subnet_name = f"{team['name']}-{service['name']}-{environment['name']}-subnet"
subnet = network.Subnet(
subnet_name,
subnet_name=subnet_name,
resource_group_name=resource_group_name,
virtual_network_name=existing_vnet.name,
address_prefix=subnet_prefix
)
salmon-account-74572
04/14/2023, 3:05 PMsparse-iron-45589
04/14/2023, 3:19 PMsalmon-account-74572
04/14/2023, 3:22 PMsparse-iron-45589
04/14/2023, 3:23 PMsalmon-account-74572
04/14/2023, 3:38 PMsparse-iron-45589
04/14/2023, 3:46 PMself.existing_vnets[existing_vnet_id] = network.VirtualNetwork.get(
f"vnet-{existing_vnet_id}",
id=existing_vnet_id
)
subnet = azure_native.network.Subnet("subnet",
address_prefix="10.0.0.0/16",
resource_group_name="subnet-test",
subnet_name="subnet1",
virtual_network_name="vnetname")
subnet.virtual_network_name.apply(lambda v: <http://log.info|log.info>("Subnet virtual network is " + v))
subnet.resource_group_name.apply(lambda v: <http://log.info|log.info>("Subnet esource group name is " + v))
subnet.subnet_name.apply(lambda v: <http://log.info|log.info>("Subnet subnet_name is " + v)
salmon-account-74572
04/14/2023, 3:58 PMsparse-iron-45589
04/14/2023, 3:58 PMaks_clusters:
- name: aks-cluster1
location: eastus
organization: cdx
resource_group_name: Internal-aks-Services-2
existing_vnet_id: /subscriptions/XXXX-XXXX-XXXXX-XXXXX-b0862310b2cc/resourceGroups/InternalServices/providers/Microsoft.Network/virtualNetworks/InternalServices-vnet
vnet_address_space: 10.200.0.0/16
subnet_prefix: 10.200.2.0/24
if I specify the subnet_prefix address in the config and read it in, it works as expected. however, the devops eng asked if we could do it dynamically which is where I am stuck as you can see 🙂salmon-account-74572
04/14/2023, 4:08 PMsparse-iron-45589
04/14/2023, 4:25 PMsalmon-account-74572
04/17/2023, 2:44 PM