Skip to content

fix: Keep hostname and IP Address in sync during stale IP cleanup and reallocation#2252

Open
abvarshney-nv wants to merge 1 commit into
NVIDIA:mainfrom
abvarshney-nv:ip_cleanup
Open

fix: Keep hostname and IP Address in sync during stale IP cleanup and reallocation#2252
abvarshney-nv wants to merge 1 commit into
NVIDIA:mainfrom
abvarshney-nv:ip_cleanup

Conversation

@abvarshney-nv
Copy link
Copy Markdown
Contributor

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

  • Add - New feature or capability
  • Change - Changes in existing functionality
  • Fix - Bug fixes
  • Remove - Removed features or deprecated functionality
  • Internal - Internal changes (refactoring, tests, docs, etc.)

Related Issues (Optional)

Breaking Changes

  • This PR contains breaking changes

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • No testing required (docs, internal refactor, etc.)

Additional Notes

@abvarshney-nv abvarshney-nv requested a review from a team as a code owner June 5, 2026 17:54
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 5, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: cdf59446-7ec1-4033-87cf-2639ae8bfcfd

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@abvarshney-nv abvarshney-nv requested review from chet and wminckler June 5, 2026 17:55
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
crates/api-core/src/tests/dhcp_lease_expiration.rs (1)

343-416: ⚡ Quick win

Also assert domain_id transitions in this lifecycle test.

This path updates both hostname and domain linkage. Adding domain_id assertions 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

📥 Commits

Reviewing files that changed from the base of the PR and between d7aa6dc and 3531a01.

📒 Files selected for processing (3)
  • crates/api-core/src/dhcp/expire.rs
  • crates/api-core/src/tests/dhcp_lease_expiration.rs
  • crates/api-db/src/machine_interface.rs

Comment thread crates/api-core/src/dhcp/expire.rs
Comment thread crates/api-db/src/machine_interface.rs
@abvarshney-nv abvarshney-nv enabled auto-merge (squash) June 5, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants