feat(ipam): several IP addresses per instance
Some checks failed
Backend Tests / pytest (Python 3.12) (push) Failing after 1m46s
Deploy to Production / Trigger production update (push) Failing after 10s
SonarCloud / SonarCloud Analysis (push) Failing after 3m7s

A guest was implicitly limited to a single allocation: lookups were
"(server, vmid) -> one row", release_ip_by_vmid freed one address, and
the link tool skipped guests that already had one. An instance can now
hold a primary address plus any number of aliases, managed from the
Network tab of the instance and visible on the IPAM pages.

Schema and service:
- ipam_allocations gains is_primary, assignment_kind, target_interface
  and apply_* columns; migration 43 marks existing rows as primary
- find_allocations_by_resource/set_primary; release_ip_by_vmid now frees
  every address of a deleted guest instead of just the first one
- next-available search may scan the whole subnet when a network has no
  auto-assign pool, skipping node and cached guest addresses so it never
  hands out an address that is already live

Applying to the guest (services/guest_ip_service.py):
- reuses script_engine, so LXC goes through pct exec and VMs through the
  guest agent; the shell script synchronises the whole alias list of an
  interface, which makes add, remove and retry the same code path
- persists via ifupdown, netplan, NetworkManager or sysconfig and never
  restarts guest networking; an unknown stack degrades to runtime_only
  and an unreachable guest keeps the allocation as pending

API and UI:
- /ipam/api/guests/{server_id}/{vmid}/addresses: list, add, apply,
  primary, delete; allocations list accepts guest filters
- instance list exposes ips[] alongside the primary ip
- addresses card on the Network tab, "+N" badge in the IP column,
  primary badge in the allocations table

Fixes found along the way:
- link-allocations registers every address of a guest, carries the MAC
  over, refuses to take an address away from another live guest and no
  longer matches allocations by resource name (which leaked an address
  between same-named guests on different servers)
- IPAM history no longer fails to serialise datetime values, which used
  to abort re-linking with a 500
This commit is contained in:
2026-08-21 23:48:20 +05:00
parent c9d8e8207f
commit 6796903712
23 changed files with 2865 additions and 146 deletions

View File

@@ -87,6 +87,21 @@ class IPAMAllocation(Base):
status = Column(String(20), default="allocated", nullable=False)
allocation_type = Column(String(20), default="static", nullable=False)
# Гость может держать несколько адресов: ровно один из них основной
# (живёт в net0/ipconfig0 и показывается в колонке IP), остальные
# навешиваются алиасами на интерфейс уже внутри гостя.
is_primary = Column(Boolean, default=False, nullable=False)
assignment_kind = Column(String(10), default="primary", nullable=False) # primary | alias
target_interface = Column(String(20), nullable=True) # eth0, ens18, ...
# Состояние применения алиаса к живому гостю.
# applied — поднят и закреплён в конфиге ОС; runtime_only — поднят, но
# стек не распознан и ребут его не переживёт; pending — гость был
# недоступен; failed — скрипт вернул ошибку.
apply_status = Column(String(20), nullable=True)
apply_error = Column(Text, nullable=True)
applied_at = Column(DateTime(timezone=True), nullable=True)
hostname = Column(String(255), nullable=True, index=True)
fqdn = Column(String(255), nullable=True)
@@ -106,6 +121,7 @@ class IPAMAllocation(Base):
Index('idx_ipam_alloc_network', 'network_id'),
Index('idx_ipam_alloc_resource', 'resource_type', 'resource_id'),
Index('idx_ipam_alloc_proxmox', 'proxmox_server_id', 'proxmox_vmid'),
Index('idx_ipam_alloc_primary', 'proxmox_server_id', 'proxmox_vmid', 'is_primary'),
Index('idx_ipam_alloc_status', 'status'),
)