fix: Keep hostname and IP Address in sync during stale IP cleanup and reallocation#2252
fix: Keep hostname and IP Address in sync during stale IP cleanup and reallocation#2252abvarshney-nv wants to merge 1 commit into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
crates/api-core/src/tests/dhcp_lease_expiration.rs (1)
343-416: ⚡ Quick winAlso assert
domain_idtransitions in this lifecycle test.This path updates both hostname and domain linkage. Adding
domain_idassertions here will catch stale-DNS regressions that hostname-only checks can miss.Proposed test additions
let iface = db::machine_interface::find_one(&mut *txn, interface_id).await?; + let original_domain_id = iface.domain_id; let expected_hostname = original_ip.replace('.', "-"); assert_eq!( iface.hostname, expected_hostname, "hostname should be derived from the allocated IP" ); @@ assert_eq!( iface_after_expiry.hostname, expected_dormant, "hostname should be reset to dormant placeholder after lease expiry" ); + assert!( + iface_after_expiry.domain_id.is_none(), + "domain_id should be cleared after lease expiry" + ); @@ assert_eq!( iface_after_rediscover.hostname, expected_new_hostname, "hostname should match the newly allocated IP after rediscover" ); + assert_eq!( + iface_after_rediscover.domain_id, original_domain_id, + "domain_id should be restored on rediscover" + );🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/api-core/src/tests/dhcp_lease_expiration.rs` around lines 343 - 416, In test_expire_resets_hostname_and_discover_restores_it capture and assert the domain_id transitions along with the hostname: after the first discover read iface.domain_id into an initial_domain_id and assert it is Some(...) (or equals the expected assigned domain), after expire assert iface_after_expiry.domain_id is None (or the dormant domain id used when leases are released), and after the rediscover assert iface_after_rediscover.domain_id equals initial_domain_id again; use the existing local variables iface, iface_after_expiry, and iface_after_rediscover and the domain_id field to add these three assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/api-core/src/dhcp/expire.rs`:
- Around line 72-83: The current code calls
db::machine_interface::reset_hostname_to_dormant(...) unconditionally after
deleting a DHCP row, which incorrectly wipes hostnames when other addresses
remain; implement a new helper
db::machine_interface::sync_hostname_after_address_change(txn: &mut Transaction,
iface_id: Uuid, mac: &MacAddr) that loads the interface's remaining addresses,
picks an IPv4 address if present (otherwise the first address) to recompute and
store the hostname/domain, and only falls back to reset to dormant (noip-*) when
no addresses remain, then replace the unconditional reset call in expire.rs with
a call to sync_hostname_after_address_change(...) so the hostname is recomputed
correctly after address deletion.
In `@crates/api-db/src/machine_interface.rs`:
- Around line 2146-2156: The code currently picks a hostname candidate only from
allocated_addresses which can mis-set the hostname during family-only
reallocations; change the logic in the hostname sync block to derive the
candidate from the interface’s full active address set (e.g., query or use
machine_interface_addresses for interface_id and combine with
allocated_addresses if needed), prefer an IPv4 address when present, then call
address_to_hostname(selected_addr)? and await update_hostname_and_domain(txn,
interface_id, &hostname, segment.config.subdomain_id). Ensure you reference
allocated_addresses, address_to_hostname, update_hostname_and_domain,
interface_id, txn and segment.config.subdomain_id when making the replacement so
the selection uses the complete current addresses rather than only newly
allocated ones.
---
Nitpick comments:
In `@crates/api-core/src/tests/dhcp_lease_expiration.rs`:
- Around line 343-416: In test_expire_resets_hostname_and_discover_restores_it
capture and assert the domain_id transitions along with the hostname: after the
first discover read iface.domain_id into an initial_domain_id and assert it is
Some(...) (or equals the expected assigned domain), after expire assert
iface_after_expiry.domain_id is None (or the dormant domain id used when leases
are released), and after the rediscover assert iface_after_rediscover.domain_id
equals initial_domain_id again; use the existing local variables iface,
iface_after_expiry, and iface_after_rediscover and the domain_id field to add
these three assertions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 6b1c5a4e-9090-47b6-8a14-f835cd862ee7
📒 Files selected for processing (3)
crates/api-core/src/dhcp/expire.rscrates/api-core/src/tests/dhcp_lease_expiration.rscrates/api-db/src/machine_interface.rs
Description
Keep hostname and IP Address in sync during stale IP cleanup and reallocation.
When a DHCP lease expires, the IP address is removed from machine_interface_addresses but the hostname column in machine_interfaces (derived from the IP, e.g. 192-168-1-5) is left stale. When a new IP is allocated on the next DHCP discover, the hostname is never updated either — leaving DNS permanently out of sync with the interface's actual IP.
Type of Change
Related Issues (Optional)
Breaking Changes
Testing
Additional Notes