-
Notifications
You must be signed in to change notification settings - Fork 248
feat(agentd): introduce AgentdConfig to read env vars once at startup #507
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
Open
dijdzv
wants to merge
5
commits into
superradcompany:main
Choose a base branch
from
dijdzv:fix/flaky-pty-reader-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67c6d8b
fix(agentd): serialize env-dependent tests to prevent env var race in…
dijdzv be775a0
fix(agentd): remove dead ENV_USER fallback in resolve_requested_user
dijdzv 7ec3813
feat(agentd): read all MSB_* env vars into AgentdConfig once at startup
dijdzv a907292
refactor(agentd): move env parsing into config.rs so consumers receiv…
dijdzv b036c79
Merge remote-tracking branch 'upstream/main' into fix/flaky-pty-reade…
dijdzv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! Agentd configuration, read once from environment variables at startup. | ||
|
|
||
| use microsandbox_protocol::{ | ||
| ENV_BLOCK_ROOT, ENV_HOSTNAME, ENV_MOUNTS, ENV_NET, ENV_NET_IPV4, ENV_NET_IPV6, ENV_TMPFS, | ||
| ENV_USER, | ||
| }; | ||
|
|
||
| //-------------------------------------------------------------------------------------------------- | ||
| // Types | ||
| //-------------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Configuration for agentd, read once from environment variables at startup. | ||
| /// | ||
| /// All `MSB_*` environment variables are captured into this struct during | ||
| /// construction via [`AgentdConfig::from_env`]. Downstream functions receive | ||
| /// the config by reference, avoiding repeated (and thread-unsafe) env var reads. | ||
| #[derive(Debug, Clone, Default)] | ||
| pub struct AgentdConfig { | ||
| /// `MSB_BLOCK_ROOT` — block device for rootfs switch. | ||
| pub block_root: Option<String>, | ||
|
|
||
| /// `MSB_MOUNTS` — virtiofs volume mount specs. | ||
| pub mounts: Option<String>, | ||
|
|
||
| /// `MSB_TMPFS` — tmpfs mount specs. | ||
| pub tmpfs: Option<String>, | ||
|
|
||
| /// `MSB_HOSTNAME` — guest hostname. | ||
| pub hostname: Option<String>, | ||
|
|
||
| /// `MSB_NET` — network interface config. | ||
| pub net: Option<String>, | ||
|
|
||
| /// `MSB_NET_IPV4` — IPv4 config. | ||
| pub net_ipv4: Option<String>, | ||
|
|
||
| /// `MSB_NET_IPV6` — IPv6 config. | ||
| pub net_ipv6: Option<String>, | ||
|
|
||
| /// `MSB_USER` — default guest user for exec sessions. | ||
| pub user: Option<String>, | ||
| } | ||
|
|
||
| //-------------------------------------------------------------------------------------------------- | ||
| // Implementations | ||
| //-------------------------------------------------------------------------------------------------- | ||
|
|
||
| impl AgentdConfig { | ||
| /// Reads all `MSB_*` environment variables once and returns the config. | ||
| /// | ||
| /// Empty or whitespace-only values are treated as absent (`None`). | ||
| pub fn from_env() -> Self { | ||
| Self { | ||
| block_root: read_env(ENV_BLOCK_ROOT), | ||
| mounts: read_env(ENV_MOUNTS), | ||
| tmpfs: read_env(ENV_TMPFS), | ||
| hostname: read_env(ENV_HOSTNAME), | ||
| net: read_env(ENV_NET), | ||
| net_ipv4: read_env(ENV_NET_IPV4), | ||
| net_ipv6: read_env(ENV_NET_IPV6), | ||
| user: read_env(ENV_USER), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //-------------------------------------------------------------------------------------------------- | ||
| // Helper Functions | ||
| //-------------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Reads a single environment variable, returning `None` for missing or empty values. | ||
| fn read_env(key: &str) -> Option<String> { | ||
| std::env::var(key) | ||
| .ok() | ||
| .map(|v| v.trim().to_string()) | ||
| .filter(|v| !v.is_empty()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one more nit 😅.
It would be nice if all the env parsing operations are moved into this file as well.
So we parse all the env vars once in
from_envand fields inAgentdConfigcan be the actual types. Something likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved all env parsing into
config.rs. Spec types ownStringinstead of borrowing — keeps the API simple with no lifetimes and a single-stepfrom_env(). a907292