Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DNS plug-in on network change #5331

Merged
merged 2 commits into from
Oct 9, 2024

Conversation

agners
Copy link
Member

@agners agners commented Oct 8, 2024

Proposed change

Restart the DNS plug-in on network change. This makes sure any potential host OS DNS configuration changes get picked up by the DNS plug-in as well.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to cli pull request:
  • Link to client library pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (ruff format supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints or add-on configuration are added/changed:

Summary by CodeRabbit

  • New Features

    • Enhanced network management by ensuring DNS configuration is updated after changes in the primary connection.
    • Added automated tests to verify DNS plugin restart functionality on primary connection changes.
  • Bug Fixes

    • Improved error handling in the network update process.

@agners agners marked this pull request as draft October 8, 2024 13:28
Copy link

coderabbitai bot commented Oct 8, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes involve modifications to the NetworkManager class in the supervisor/host/network.py file. Two new constants, DBUS_ATTR_PRIMARY_CONNECTION and DBUS_OBJECT_BASE, have been added. The _check_connectivity_changed method now includes logic to restart the DNS plugin if the primary connection changes. Additionally, the update method has been altered to also restart the DNS plugin after handling network updates. A new test function has been introduced in tests/host/test_connectivity.py to verify the DNS restart behavior.

Changes

File Path Change Summary
supervisor/host/network.py Added constants DBUS_ATTR_PRIMARY_CONNECTION and DBUS_OBJECT_BASE. Updated _check_connectivity_changed to restart DNS if the primary connection changes. Modified update method to restart DNS after updating network information.
tests/host/test_connectivity.py Added asynchronous test test_dns_restart_on_connection_change to validate DNS restart on primary connection change.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant NetworkManager
    participant DNSPlugin

    User->>NetworkManager: Request to update network
    NetworkManager->>NetworkManager: Handle update logic
    NetworkManager->>DNSPlugin: Restart DNS
    DNSPlugin-->>NetworkManager: DNS restarted
    NetworkManager-->>User: Update complete
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (1)
supervisor/host/network.py (1)

176-178: LGTM! Consider adding error handling for DNS plugin restart.

The addition of restarting the DNS plugin after updating network information is a good improvement. It ensures that the DNS configuration remains up-to-date with any network changes.

Consider adding error handling for the DNS plugin restart to ensure the method continues execution even if the restart fails:

try:
    await self.sys_plugins.dns.restart()
except Exception as err:
    _LOGGER.warning("Failed to restart DNS plugin: %s", err)

This will prevent any potential issues with the DNS plugin from blocking the rest of the update process.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1ee01b1 and 38affe8.

📒 Files selected for processing (1)
  • supervisor/host/network.py (1 hunks)
🧰 Additional context used

@agners agners force-pushed the update-dns-plugin-on-network-change branch from f0d32b0 to 38affe8 Compare October 8, 2024 14:10
@@ -282,6 +282,10 @@ async def apply_changes(
state = msg[0]
_LOGGER.debug("Active connection state changed to %s", state)

Copy link
Member

Choose a reason for hiding this comment

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

It would be great if there was a way for supervisor to tell core that the network has changed so we can solve home-assistant/core#59553

Copy link
Member

Choose a reason for hiding this comment

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

Agree, but that is a good challenge to solve after we've finished replacing/abstracting the communication layer in Core<>Supervisor (currently WIP).

Copy link
Contributor

@mdegat01 mdegat01 Oct 9, 2024

Choose a reason for hiding this comment

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

We do actually fire a message to core currently when system connectivity has changed:

self.sys_homeassistant.websocket.supervisor_update_event(
"network", {ATTR_HOST_INTERNET: state}
)

It doesn't have all the details of the connection if we need that. But if connectivity of the system goes from False to True or vice versa we do let core know. I'm not sure anything is listening for that event currently though.

@agners agners force-pushed the update-dns-plugin-on-network-change branch 4 times, most recently from 9e4efb5 to ba618fb Compare October 9, 2024 15:50
@agners agners requested a review from mdegat01 October 9, 2024 15:50
@agners agners marked this pull request as ready for review October 9, 2024 15:50
Copy link

@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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 38affe8 and ba618fb.

📒 Files selected for processing (1)
  • supervisor/host/network.py (2 hunks)
🧰 Additional context used
🪛 Ruff
supervisor/host/network.py

147-147: Logging statement uses f-string

(G004)

🪛 GitHub Check: Check ruff
supervisor/host/network.py

[failure] 147-147: Ruff (G004)
supervisor/host/network.py:147:22: G004 Logging statement uses f-string

🔇 Additional comments (4)
supervisor/host/network.py (4)

13-15: LGTM: New constants added for D-Bus attributes

The addition of DBUS_ATTR_PRIMARY_CONNECTION and DBUS_OBJECT_BASE constants is appropriate for the new functionality. These constants will help improve code readability and maintainability when working with D-Bus attributes.


154-162: LGTM: Restart DNS plugin on primary connection change

The new logic to restart the DNS plugin when the primary connection changes aligns well with the PR objective. This ensures that the DNS settings are updated promptly when network changes occur, improving the system's responsiveness to network configuration changes.


Line range hint 1-1: Summary of changes

The changes in this file implement the functionality to restart the DNS plugin when there's a change in the network's primary connection. This aligns well with the PR objective of ensuring the DNS plug-in restarts on network changes.

Key points:

  1. New constants added for D-Bus attributes.
  2. Logic implemented to check for primary connection changes and restart the DNS plugin.
  3. Minor improvement suggested for the logging statement.
  4. Verification requested for changes mentioned in the AI summary but not visible in the diff.

Overall, the changes look good and should improve the system's responsiveness to network configuration changes.


Line range hint 182-182: Verify changes to the update method

The AI-generated summary mentioned changes to the update method to restart the DNS plugin after handling network updates. However, these changes are not visible in the provided code diff. Could you please verify if these changes were made or if they were removed in a subsequent update?

To check for any changes in the update method, you can run the following command:

This will show the entire update method, allowing us to verify if any changes were made.

supervisor/host/network.py Outdated Show resolved Hide resolved
@agners agners force-pushed the update-dns-plugin-on-network-change branch 2 times, most recently from 26d3e1d to 042107a Compare October 9, 2024 16:15
Restart the DNS plug-in when the primary network changes network
changes. This makes sure any potential host OS DNS configuration
changes get picked up by the DNS plug-in as well.
@agners agners force-pushed the update-dns-plugin-on-network-change branch from 042107a to f652875 Compare October 9, 2024 16:27
Copy link

@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: 0

🧹 Outside diff range and nitpick comments (1)
tests/host/test_connectivity.py (1)

90-104: Good addition of test, but consider enhancing it further.

The new test function test_dns_restart_on_connection_change is a valuable addition that aligns well with the PR objectives. It effectively verifies that the DNS plugin is restarted when the primary network connection changes. However, consider the following suggestions to enhance the test:

  1. Add an assertion to verify the initial state of the network before simulating changes.
  2. Include more specific assertions after each network change to ensure the behavior is correct.
  3. Consider adding a test case for when the primary connection doesn't change to ensure no unnecessary restarts occur.

Here's a suggested improvement to the test function:

async def test_dns_restart_on_connection_change(
    coresys: CoreSys, network_manager_service: NetworkManagerService
):
    """Test dns plugin is restarted when primary connection changes."""
    await coresys.host.network.load()
    
    # Verify initial state
    assert coresys.host.network.primary_connection == "/"
    
    with patch.object(PluginDns, "restart") as restart:
        # Test: No change in primary connection
        network_manager_service.emit_properties_changed({"PrimaryConnection": "/"})
        await network_manager_service.ping()
        restart.assert_not_called()
        assert coresys.host.network.primary_connection == "/"

        # Test: Change in primary connection
        new_connection = "/org/freedesktop/NetworkManager/ActiveConnection/2"
        network_manager_service.emit_properties_changed(
            {"PrimaryConnection": new_connection}
        )
        await network_manager_service.ping()
        restart.assert_called_once()
        assert coresys.host.network.primary_connection == new_connection

        # Test: No additional restarts for subsequent checks
        await network_manager_service.ping()
        restart.assert_called_once()

This improved version adds more assertions and an additional test case to ensure the behavior is correct in various scenarios.

Also, please ensure that the indentation of the function is consistent with the rest of the file (it seems to be off by one space).

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f652875 and 9aeca6a.

📒 Files selected for processing (1)
  • tests/host/test_connectivity.py (2 hunks)
🧰 Additional context used

@agners agners merged commit 2968a57 into main Oct 9, 2024
20 checks passed
@agners agners deleted the update-dns-plugin-on-network-change branch October 9, 2024 18:16
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants