Skip to content

Conversation

joaomariolago
Copy link
Collaborator

@joaomariolago joaomariolago commented Oct 13, 2025

Closes #3449

Summary by Sourcery

Add default environment variable injection and log rotation configuration to Kraken Docker container setup.

New Features:

  • Automatically inject specified default environment variables into container config if present in host environment.
  • Apply a default Docker HostConfig LogConfig for JSON file logging with size and file rotation limits.

Enhancements:

  • Introduce helper methods to set default ENV variables and host log configuration in extension module.
  • Define DEFAULT_INJECTED_ENV_VARIABLES in Kraken config and export it via all.

Copy link

sourcery-ai bot commented Oct 13, 2025

Reviewer's Guide

This PR introduces helper methods to centralize container HostConfig/LogConfig setup and inject default host environment variables into Kraken container configurations.

Class diagram for updated Kraken extension container config helpers

classDiagram
    class Extension {
    }
    class KrakenExtension {
        +_set_container_config_default_env_variables(config: Dict[str, Any])
        +_set_container_config_host_config(config: Dict[str, Any])
        async start()
    }
    KrakenExtension : _set_container_config_default_env_variables(config)
    KrakenExtension : _set_container_config_host_config(config)
    KrakenExtension : async start()
    KrakenExtension --> Extension
Loading

Class diagram for new DEFAULT_INJECTED_ENV_VARIABLES constant

classDiagram
    class KrakenConfig {
        +DEFAULT_INJECTED_ENV_VARIABLES: List[str]
    }
Loading

File-Level Changes

Change Details Files
Centralize HostConfig and LogConfig setup
  • Extract _set_container_config_host_config helper to initialize HostConfig and LogConfig
  • Replace inline HostConfig/LogConfig block in start() with helper call
core/services/kraken/extension/extension.py
Add default environment variable injection
  • Define DEFAULT_INJECTED_ENV_VARIABLES constant
  • Implement _set_container_config_default_env_variables helper
  • Invoke the new env variable helper in start()
core/services/kraken/extension/extension.py
core/services/kraken/config.py

Assessment against linked issues

Issue Objective Addressed Explanation
#3449 Expose useful BlueOS environment variables (such as MAV_SYSTEM_ID) to extensions so they are available in extension containers.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@joaomariolago joaomariolago force-pushed the add-kraken-default-env-vars branch from ae92f9e to fa43a63 Compare October 13, 2025 14:00
@joaomariolago joaomariolago marked this pull request as ready for review October 13, 2025 14:02
@joaomariolago joaomariolago added the docs-needed Change needs to be documented label Oct 13, 2025
@joaoantoniocardoso joaoantoniocardoso added the move-to-stable Needs to be cherry-picked and move to stable label Oct 13, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • In _set_container_config_default_env_variables you check for variable not in config['Env'] but config['Env'] entries are full "KEY=VALUE" strings, so consider matching by prefix (e.g. startswith "KEY=") to avoid duplicate injections.
  • _set_container_config_host_config currently unconditionally overwrites any existing LogConfig; consider merging with or preserving user-provided log settings instead of always replacing them.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In _set_container_config_default_env_variables you check for `variable not in config['Env']` but config['Env'] entries are full "KEY=VALUE" strings, so consider matching by prefix (e.g. startswith "KEY=") to avoid duplicate injections.
- _set_container_config_host_config currently unconditionally overwrites any existing LogConfig; consider merging with or preserving user-provided log settings instead of always replacing them.

## Individual Comments

### Comment 1
<location> `core/services/kraken/extension/extension.py:107-114` </location>
<code_context>
             self._settings.extensions.append(extension)
         self._manager.save()

+    def _set_container_config_default_env_variables(self, config: Dict[str, Any]) -> None:
+        if "Env" not in config:
+            config["Env"] = []
+
+        for variable in DEFAULT_INJECTED_ENV_VARIABLES:
+            env_val = os.getenv(variable)
+            if variable not in config["Env"] and env_val:
</code_context>

<issue_to_address>
**suggestion:** Consider handling duplicate environment variable entries more robustly.

Currently, the check only looks for the variable name in config["Env"], which may miss cases where the variable exists with a different value. Consider parsing existing entries to ensure no duplicate variable names are added.

```suggestion
    def _set_container_config_default_env_variables(self, config: Dict[str, Any]) -> None:
        if "Env" not in config:
            config["Env"] = []

        # Parse existing env variable names
        existing_var_names = set()
        for entry in config["Env"]:
            if "=" in entry:
                name, _ = entry.split("=", 1)
                existing_var_names.add(name)
            else:
                existing_var_names.add(entry)

        for variable in DEFAULT_INJECTED_ENV_VARIABLES:
            env_val = os.getenv(variable)
            if variable not in existing_var_names and env_val:
                config["Env"].append(f"{variable}={env_val}")
```
</issue_to_address>

### Comment 2
<location> `core/services/kraken/config.py:20-22` </location>
<code_context>
 ]

-__all__ = ["SERVICE_NAME", "DEFAULT_MANIFESTS", "DEFAULT_EXTENSIONS"]
+DEFAULT_INJECTED_ENV_VARIABLES = [
+    "MAV_SYSTEM_ID",
+]
</code_context>

<issue_to_address>
**suggestion:** Consider documenting the rationale for the default injected environment variables.

Providing context for the default variables will help future maintainers avoid accidental changes and ensure correct usage.

```suggestion
# These environment variables are injected by default into service containers.
# Rationale: MAV_SYSTEM_ID is required for correct identification of the MAVLink system
# in all deployments. Changing or removing this variable may break communication with
# MAVLink-based services. Add additional variables here only if they are universally
# required for all services.
DEFAULT_INJECTED_ENV_VARIABLES = [
    "MAV_SYSTEM_ID",
]
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@joaomariolago joaomariolago force-pushed the add-kraken-default-env-vars branch from fa43a63 to d7908dd Compare October 13, 2025 14:18
* Inject default env vars in containers when starting it
@joaomariolago joaomariolago force-pushed the add-kraken-default-env-vars branch from d7908dd to dc3c931 Compare October 13, 2025 14:27
Copy link
Member

@joaoantoniocardoso joaoantoniocardoso left a comment

Choose a reason for hiding this comment

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

Tested ✔️

@joaoantoniocardoso joaoantoniocardoso merged commit 157d6b5 into bluerobotics:master Oct 13, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs-needed Change needs to be documented move-to-stable Needs to be cherry-picked and move to stable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose (useful) BlueOS env vars to extensions

2 participants