Container Prefixes do not report available prefixes correctly #9083

Closed
opened 2025-12-29 20:45:13 +01:00 by adam · 4 comments
Owner

Originally created by @diabolic53 on GitHub (Jan 11, 2024).

Deployment Type

Self-hosted

NetBox Version

v3.7.0

Python Version

3.8

Steps to Reproduce

Create Container prefix with Global VRF to be used as a shared pool for multiple tenants
add child prefix and assign a VRF by using the api to get the first available prefix in the container
http://netboxURL/api/ipam/prefixes/"prefixID"/available-prefixes/
image

add a second prefix and assign a second VRF by using the api to get the first available prefix in the container
http://netboxURL//api/ipam/prefixes/"prefixID"/available-prefixes/
image

Expected Behavior

get the next available prefix from the container
image

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
{
"family": 6,
"prefix": "2a00:da9:101:2001::/64",
"vrf": null
},
{
"family": 6,
"prefix": "2a00:da9:101:2002::/63",
"vrf": null
},

Observed Behavior

get the same prefix as the first prefix assigned due to VRF not being accounted for when calculating prefixes

image

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
{
"family": 6,
"prefix": "2a00:da9:101:2000::/52",
"vrf": null
}
]

Originally created by @diabolic53 on GitHub (Jan 11, 2024). ### Deployment Type Self-hosted ### NetBox Version v3.7.0 ### Python Version 3.8 ### Steps to Reproduce Create Container prefix with Global VRF to be used as a shared pool for multiple tenants add child prefix and assign a VRF by using the api to get the first available prefix in the container http://netboxURL/api/ipam/prefixes/"prefixID"/available-prefixes/ ![image](https://github.com/netbox-community/netbox/assets/48087756/1019ce13-c480-46a0-8d65-2aaf7be59e7c) add a second prefix and assign a second VRF by using the api to get the first available prefix in the container http://netboxURL//api/ipam/prefixes/"prefixID"/available-prefixes/ ![image](https://github.com/netbox-community/netbox/assets/48087756/b45b20fc-5430-4203-8a58-137774b3d33a) ### Expected Behavior get the next available prefix from the container ![image](https://github.com/netbox-community/netbox/assets/48087756/fbcbabb0-b476-48e4-825d-9a9e172ecca1) HTTP 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Type: application/json Vary: Accept [ { "family": 6, "prefix": "2a00:da9:101:2001::/64", "vrf": null }, { "family": 6, "prefix": "2a00:da9:101:2002::/63", "vrf": null }, ### Observed Behavior get the same prefix as the first prefix assigned due to VRF not being accounted for when calculating prefixes ![image](https://github.com/netbox-community/netbox/assets/48087756/cd6e5f73-d8f0-4949-ac6a-13183b2c7332) HTTP 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Type: application/json Vary: Accept [ { "family": 6, "prefix": "2a00:da9:101:2000::/52", "vrf": null } ]
adam added the type: bugstatus: needs ownerpending closureseverity: low labels 2025-12-29 20:45:13 +01:00
adam closed this issue 2025-12-29 20:45:13 +01:00
Author
Owner

@diabolic53 commented on GitHub (Jan 11, 2024):

i've noticed this was introduced in this fix
7ba0b420f1 (diff-b12db49b7b31202cc34428c1dbdf443ae9abb72547c0445e10497cee67698888)

i would propose a different tweak as container prefixes should not be treated the same as Active prefixes.

def get_available_prefixes(self):
        """
        Return all available prefixes within this Aggregate or Prefix as an IPSet.
        """
        params = {
            'prefix__net_contained': str(self.prefix)
        }
        if hasattr(self, 'status') and self.status != "container":
            if hasattr(self, 'vrf'):
                 params['vrf'] = self.vrf

            child_prefixes = Prefix.objects.filter(**params).values_list('prefix', flat=True)
            return netaddr.IPSet(self.prefix) - netaddr.IPSet(child_prefixes)
        else:
            prefix = netaddr.IPSet(self.prefix)
            child_prefixes = netaddr.IPSet([child.prefix for child in self.get_child_prefixes()])
            available_prefixes = prefix - child_prefixes

            return available_prefixes
@diabolic53 commented on GitHub (Jan 11, 2024): i've noticed this was introduced in this fix https://github.com/netbox-community/netbox/pull/10114/commits/7ba0b420f181cffac86a9de384a1ec9d8a9a07dd#diff-b12db49b7b31202cc34428c1dbdf443ae9abb72547c0445e10497cee67698888 i would propose a different tweak as container prefixes should not be treated the same as Active prefixes. ``` def get_available_prefixes(self): """ Return all available prefixes within this Aggregate or Prefix as an IPSet. """ params = { 'prefix__net_contained': str(self.prefix) } if hasattr(self, 'status') and self.status != "container": if hasattr(self, 'vrf'): params['vrf'] = self.vrf child_prefixes = Prefix.objects.filter(**params).values_list('prefix', flat=True) return netaddr.IPSet(self.prefix) - netaddr.IPSet(child_prefixes) else: prefix = netaddr.IPSet(self.prefix) child_prefixes = netaddr.IPSet([child.prefix for child in self.get_child_prefixes()]) available_prefixes = prefix - child_prefixes return available_prefixes ```
Author
Owner

@diabolic53 commented on GitHub (Jan 11, 2024):

To reproduce in the UI
Create Container prefix 2a00:da9:101:2000::/52 with Global VRF to be used as a shared pool for multiple tenants
Click on add prefix under child prefixes and assign a VRF to the new prefix , you will receive 2a00:da9:101:2000::/52, change it to 2a00:da9:101:2000::/64
Click on add prefix under child prefixes a second time and you will receive the same 2a00:da9:101:2000::/52 which leads to duplicates

Expected behavior:
Create Container prefix 2a00:da9:101:2000::/52 with Global VRF to be used as a shared pool for multiple tenants
Click on add prefix under child prefixes and assign a VRF to the new prefix , you will receive 2a00:da9:101:2000::/52, change it to 2a00:da9:101:2000::/64
Click on add prefix under child prefixes a second time, you will receive 2a00:da9:101:2001::/64 for the second prefix

The fix above fixes this issue for container type prefixes but keeps the vrf filtering for Active type prefixes.

@diabolic53 commented on GitHub (Jan 11, 2024): To reproduce in the UI Create Container prefix 2a00:da9:101:2000::/52 with Global VRF to be used as a shared pool for multiple tenants Click on add prefix under child prefixes and assign a VRF to the new prefix , you will receive 2a00:da9:101:2000::/52, change it to 2a00:da9:101:2000::/64 Click on add prefix under child prefixes a second time and you will receive the same 2a00:da9:101:2000::/52 which leads to duplicates Expected behavior: Create Container prefix 2a00:da9:101:2000::/52 with Global VRF to be used as a shared pool for multiple tenants Click on add prefix under child prefixes and assign a VRF to the new prefix , you will receive 2a00:da9:101:2000::/52, change it to 2a00:da9:101:2000::/64 Click on add prefix under child prefixes a second time, you will receive 2a00:da9:101:2001::/64 for the second prefix The fix above fixes this issue for container type prefixes but keeps the vrf filtering for Active type prefixes.
Author
Owner

@github-actions[bot] commented on GitHub (May 15, 2024):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (May 15, 2024): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (Jun 15, 2024):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (Jun 15, 2024): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9083