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

Add HEALTHCHECK support #421

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Conversation

lmagyar
Copy link
Contributor

@lmagyar lmagyar commented Oct 22, 2024

Proposed Changes

This PR treats the following situations unhealthy:

  • always: .BackendState == "Stopped"
  • optionally: if once was online but gets offline for more then healthcheck_offline_timeout seconds
  • optionally: if never gets online for more then healthcheck_restart_timeout seconds

In my case a plain WAN switch-over to backup WAN on the router was able to permanently make Tailscale offline. And it's not only me: tailscale/tailscale#12021 It seems to be an issue when TS is in a container. And similar bugs can happen in the future.

And after testing it in real life circumstances, TS was able to not go online even after a restart, if the transient in the WAN state was long enough. But after (re)start, we should restart the add-on again much slower, eg. to give time to authenticate on the very first time.

Note: I was able to put TS into Stopped state only manually, but maybe this can be helpful once, at least won't harm.

Note: This schema (below) would look nicer, but until a bug in the Supervisor/UI is not fixed (see #2640), we can't do this.

healthcheck:
  offline_timeout: int?
  restart_timeout: int(900,)?

Related Issues

Summary by CodeRabbit

  • New Features

    • Introduced a health check feature to monitor the Tailscale service's operational status.
    • Enhanced service management to recognize additional states, improving service handling.
  • Bug Fixes

    • Improved logic for determining the Tailscale service state, allowing for better control flow.
  • Documentation

    • Updated health check script to log service health status and manage timestamps effectively.

Copy link

coderabbitai bot commented Oct 24, 2024

Walkthrough

The pull request introduces several modifications to the Tailscale project. A health check command has been added to the Dockerfile, enabling monitoring of the container's health. Additionally, the script managing the Tailscale service's control flow has been updated to recognize an additional state, "Stopped." Furthermore, a new healthcheck script has been implemented to evaluate the operational state of the Tailscale service, logging health status and managing timestamps for service activity.

Changes

File Change Summary
tailscale/Dockerfile Added HEALTHCHECK CMD healthcheck to monitor container health.
tailscale/rootfs/etc/s6-overlay/s6-rc.d/post-tailscaled/run Updated condition to check for "Stopped" state in Tailscale service management script.
tailscale/rootfs/usr/bin/healthcheck Introduced a healthcheck script to monitor Tailscale service health, evaluating BackendState and online status.

Possibly related PRs

Poem

🐇 In the land of Tailscale, where packets do race,
A health check was added, to keep up the pace.
With states now expanded, the service can thrive,
Monitoring closely, keeping all alive.
So hop with delight, for changes are here,
A healthier Tailscale, let’s all give a cheer! 🎉


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5a3ea4d and 593ddd2.

📒 Files selected for processing (1)
  • tailscale/rootfs/usr/bin/healthcheck (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tailscale/rootfs/usr/bin/healthcheck

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 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.

@frenck frenck added the new-feature New features or options. label Nov 15, 2024
@lmagyar
Copy link
Contributor Author

lmagyar commented Dec 3, 2024

This behaves as expected in live environments and no issues reported by real users.

@lmagyar lmagyar marked this pull request as ready for review December 3, 2024 18:25
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 (6)
tailscale/rootfs/usr/bin/healthcheck (1)

25-25: Use printf with format strings to prevent unintended behavior

In lines 25, 36, 49, and 57, you're using printf without specifying a format string. This can lead to unexpected behavior if the variables contain format specifiers or special characters. It's safer to provide a format string.

Apply this diff to fix the issue:

-printf "${STARTED_TIMESTAMP}" > /var/run/s6/container_environment/STARTED_TIMESTAMP
+printf '%s\n' "${STARTED_TIMESTAMP}" > /var/run/s6/container_environment/STARTED_TIMESTAMP

-printf "${LAST_ONLINE_TIMESTAMP}" > /var/run/s6/container_environment/LAST_ONLINE_TIMESTAMP
+printf '%s\n' "${LAST_ONLINE_TIMESTAMP}" > /var/run/s6/container_environment/LAST_ONLINE_TIMESTAMP

-printf "UNHEALTHY" > /var/run/s6/container_environment/LAST_REPORTED_HEALTH_STATE
+printf '%s\n' "UNHEALTHY" > /var/run/s6/container_environment/LAST_REPORTED_HEALTH_STATE

-printf "HEALTHY" > /var/run/s6/container_environment/LAST_REPORTED_HEALTH_STATE
+printf '%s\n' "HEALTHY" > /var/run/s6/container_environment/LAST_REPORTED_HEALTH_STATE

Also applies to: 36-36, 49-49, 57-57

tailscale/translations/en.yaml (1)

47-55: Minor grammatical improvements in descriptions

Consider the following minor changes to enhance clarity:

  • Line 47: Add "the" before "timeout": "This option allows you to set the timeout in seconds..."
  • Line 54: Change "ie." to "i.e.": "The minimum value is 900, i.e., 15 minutes."
  • Line 55: Rephrase for clarity: "You may set it to several minutes or even hours."

Apply this diff to implement the changes:

-      This option allows you to set timeout in seconds for Tailscale to be offline
+      This option allows you to set the timeout in seconds for Tailscale to be offline

-          after (re)start. The minimum value is 900, ie. 15 minutes. Set it to several
+          after (re)start. The minimum value is 900, i.e., 15 minutes. You may set it to several
tailscale/DOCS.md (4)

73-74: Consider documenting the rationale for example timeout values

The example configuration shows specific timeout values (110s for offline and 3600s for restart) without explaining why these particular values were chosen. Consider adding a brief comment to help users understand these choices.

 healthcheck_offline_timeout: 110  # Time to wait before restarting after going offline
 healthcheck_restart_timeout: 3600  # Time to wait for recovery after restart (1 hour)

194-215: Fix abbreviation formatting and enhance clarity

The documentation for healthcheck_offline_timeout is comprehensive but needs some grammatical improvements:

  1. Fix abbreviation formatting:
-Tailscale was once online, ie. it won't have any effect when
+Tailscale was once online, i.e., it won't have any effect when
-Tailscale's status is eg. Starting
+Tailscale's status is e.g., Starting
-Assistant in each 30s, ie. the effective
+Assistant in each 30s, i.e., the effective
  1. Consider adding an example scenario to make it more concrete:
+For example, if set to 300 seconds (5 minutes), the add-on will restart if Tailscale goes offline and doesn't recover within 5 minutes of detecting the offline state.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~204-~204: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...s only after Tailscale was once online, ie. it won't have any effect when Tailscale...

(I_E)


[uncategorized] ~205-~205: The abbreviation “e.g.” (= for example) requires two periods.
Context: ...e any effect when Tailscale's status is eg. Starting, NeedsLogin or NeedsMachineAut...

(E_G)


[uncategorized] ~213-~213: The abbreviation “i.e.” (= that is) requires two periods.
Context: ... checked by Home Assistant in each 30s, ie. the effective resolution of this option...

(I_E)


216-241: Fix abbreviation formatting and clarify timeout behavior

The documentation for healthcheck_restart_timeout needs grammatical improvements:

Fix abbreviation formatting:

-The minimum value is 900, ie. 15 minutes
+The minimum value is 900, i.e., 15 minutes
-Tailscale is starting, ie. it won't have
+Tailscale is starting, i.e., it won't have
-Assistant in each 30s, ie. the effective
+Assistant in each 30s, i.e., the effective
🧰 Tools
🪛 LanguageTool

[uncategorized] ~221-~221: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...r (re)start. The minimum value is 900, ie. 15 minutes, to always give you enough t...

(I_E)


[uncategorized] ~229-~229: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...appens only when Tailscale is starting, ie. it won't have any effect after Tailscal...

(I_E)


[uncategorized] ~239-~239: The abbreviation “i.e.” (= that is) requires two periods.
Context: ... checked by Home Assistant in each 30s, ie. the effective resolution of this option...

(I_E)


194-241: Consider adding a visual timeline diagram

The documentation thoroughly explains the health check mechanisms, but a visual timeline diagram could help users better understand how healthcheck_offline_timeout and healthcheck_restart_timeout interact with different Tailscale states.

Example diagram structure:

Timeline:
[Starting] -> [Online] -> [Offline] -> [Restarting] -> [Online]
     |            |           |             |
     |            |           |             |
  restart_timeout |    offline_timeout      restart_timeout
🧰 Tools
🪛 LanguageTool

[uncategorized] ~204-~204: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...s only after Tailscale was once online, ie. it won't have any effect when Tailscale...

(I_E)


[uncategorized] ~205-~205: The abbreviation “e.g.” (= for example) requires two periods.
Context: ...e any effect when Tailscale's status is eg. Starting, NeedsLogin or NeedsMachineAut...

(E_G)


[uncategorized] ~213-~213: The abbreviation “i.e.” (= that is) requires two periods.
Context: ... checked by Home Assistant in each 30s, ie. the effective resolution of this option...

(I_E)


[uncategorized] ~221-~221: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...r (re)start. The minimum value is 900, ie. 15 minutes, to always give you enough t...

(I_E)


[uncategorized] ~229-~229: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...appens only when Tailscale is starting, ie. it won't have any effect after Tailscal...

(I_E)


[uncategorized] ~239-~239: The abbreviation “i.e.” (= that is) requires two periods.
Context: ... checked by Home Assistant in each 30s, ie. the effective resolution of this option...

(I_E)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between de0aae9 and 5a3ea4d.

📒 Files selected for processing (6)
  • tailscale/DOCS.md (2 hunks)
  • tailscale/Dockerfile (1 hunks)
  • tailscale/config.yaml (1 hunks)
  • tailscale/rootfs/etc/s6-overlay/s6-rc.d/post-tailscaled/run (1 hunks)
  • tailscale/rootfs/usr/bin/healthcheck (1 hunks)
  • tailscale/translations/en.yaml (1 hunks)
🧰 Additional context used
🪛 LanguageTool
tailscale/DOCS.md

[uncategorized] ~204-~204: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...s only after Tailscale was once online, ie. it won't have any effect when Tailscale...

(I_E)


[uncategorized] ~205-~205: The abbreviation “e.g.” (= for example) requires two periods.
Context: ...e any effect when Tailscale's status is eg. Starting, NeedsLogin or NeedsMachineAut...

(E_G)


[uncategorized] ~213-~213: The abbreviation “i.e.” (= that is) requires two periods.
Context: ... checked by Home Assistant in each 30s, ie. the effective resolution of this option...

(I_E)


[uncategorized] ~221-~221: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...r (re)start. The minimum value is 900, ie. 15 minutes, to always give you enough t...

(I_E)


[uncategorized] ~229-~229: The abbreviation “i.e.” (= that is) requires two periods.
Context: ...appens only when Tailscale is starting, ie. it won't have any effect after Tailscal...

(I_E)


[uncategorized] ~239-~239: The abbreviation “i.e.” (= that is) requires two periods.
Context: ... checked by Home Assistant in each 30s, ie. the effective resolution of this option...

(I_E)

🔇 Additional comments (3)
tailscale/config.yaml (1)

39-39: Verify the schema definition for healthcheck_restart_timeout

The schema defines healthcheck_restart_timeout as int(900,)?. Please ensure that this syntax correctly enforces a minimum value of 900 as intended. If the schema requires a different format to enforce a minimum value, consider updating it accordingly.

tailscale/Dockerfile (1)

38-40: HEALTHCHECK command added correctly

The addition of the HEALTHCHECK command appears to be correct and properly implemented.

tailscale/rootfs/etc/s6-overlay/s6-rc.d/post-tailscaled/run (1)

93-93: Verify inclusion of "Stopped" in backend state condition

Including .BackendState == "Stopped" in the condition may affect the script's logic. Please verify that considering the "Stopped" state here is intentional and ensures correct behavior during service startup and monitoring.

Comment on lines 73 to 74
healthcheck_offline_timeout: 110
healthcheck_restart_timeout: 3600
Copy link
Member

Choose a reason for hiding this comment

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

I really don't want to keep add options like this.

People shouldn't have to worry about this stuff.

../Frenck

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hardwire 5 minutes and 1 hour? Based on my logs, 2-3 minutes long dropouts occure approx. daily.

It also means that healthcheck will be live after update. I'm just a bit afraid that a "regular" restart can ruin something. Maybe DNS. I was never able to configure HA to use TS's DNS, so I can't test it, it's just a wild guess.

@lmagyar lmagyar marked this pull request as draft December 6, 2024 21:37
@lmagyar lmagyar marked this pull request as ready for review December 7, 2024 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature New features or options.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants