Skip to content

Crash: AttributeError: 'dict' object has no attribute 'matches_site_cluster' when a VLAN's group is an unresolved reference #529

Description

@tempalov

Environment

  • netbox-sync 1.8.0 (official bbricardo/netbox-sync:1.8.0 image) — the same code is still present on main
  • NetBox 4.6.4
  • Source type: vmware (8 vCenters)

Problem

Every run aborts during source.apply():

File "/app/module/sources/vmware/connection.py", line 2055, in add_host
    self.add_device_vm_to_inventory(NBDevice, object_data=host_data, pnic_data=pnic_data_dict,
File "/app/module/sources/vmware/connection.py", line 1186, in add_device_vm_to_inventory
    nic_object, ip_address_objects = self.add_update_interface(nic_object_dict.get(int_name), device_vm_object,
File "/app/module/sources/common/source_base.py", line 668, in add_update_interface
    matching_tagged_vlan = self.get_vlan_object_if_exists(tagged_vlan, device_object_site,
File "/app/module/sources/common/source_base.py", line 886, in get_vlan_object_if_exists
    if vlan_group.matches_site_cluster(vlan_site, vlan_cluster):
AttributeError: 'dict' object has no attribute 'matches_site_cluster'

Because the exception is raised inside source.apply(), it happens before update_instance(), so a failing run writes nothing to NetBox. In our case this silently froze all virtualization.* data for several weeks.

Cause

In get_vlan_object_if_exists():

# try find matching VLAN by group
if grab(vlan, "data.group") is not None:
    vlan_group = grab(vlan, "data.group")
    if vlan_group.matches_site_cluster(vlan_site, vlan_cluster):

the code assumes vlan.data["group"] is always a resolved NBVLANGroup instance. That holds only for VLAN groups that are part of the inventory netbox-sync fetched itself. For VLANs that already exist in NetBox and whose VLAN group was created by another tool, the relation is never resolved and stays a raw API dict:

{'id': 53392, 'url': '.../api/ipam/vlan-groups/53392/', 'display': '...', 'name': '...'}

The run then dies at the first ESXi host that has a tagged VLAN whose VID matches such a VLAN. The log shows a large number of Problems resolving relation ... messages beforehand, which is the same underlying condition.

How to reproduce

  1. Have NetBox contain VLANs assigned to VLAN groups that netbox-sync does not manage. In our case ~350 groups were created by NetBox Labs orb-agent / Diode ingestion.
  2. Run netbox-sync against a vmware source whose hosts carry tagged VLANs with VIDs matching those VLANs.

The trigger is purely data-side: the exact same netbox-sync version ran fine for months and started crashing only after those VLAN groups appeared.

Existing options do not help

disable_vlan_sync, vlan_sync_exclude_by_name and vlan_sync_exclude_by_id cannot work around this — all three are evaluated inside add_vlan_object_to_netbox(), which is called after get_vlan_object_if_exists() has already crashed.

Suggested fix

if isinstance(vlan_group, NetBoxObject) and vlan_group.matches_site_cluster(vlan_site, vlan_cluster):

If the group cannot be resolved to an object, no group match is attempted and the existing site / global VLAN matching applies — the correct degraded behaviour. NetBoxObject is already in scope via from module.netbox import *.

Verification

Tested against our production data set (8 vCenters, ~1300 VMs, NetBox with ~350 foreign VLAN groups):

unpatched patched
sources processed 0 of 8 — crashed on the first 8 of 8
time to crash ~8 s after the first source started no crash, full run 1 h 16 min
AttributeError reproduced on every run 0

Both runs crashed/passed on the identical trigger host, so the change is what makes the difference.

I will open a PR with this one-line change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions