-
Notifications
You must be signed in to change notification settings - Fork 115
core:services:kraken: Reduce install function code complexity
#3627
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
core:services:kraken: Reduce install function code complexity
#3627
Conversation
* Reduce code complexity of install function by spliting in multiple parts
Reviewer's GuideRefactor install method into modular helpers to streamline extension installation flow by extracting disabling, settings setup, authentication, image pulling, and cleanup steps into separate functions. Sequence diagram for refactored install process in ExtensionsequenceDiagram
participant E as Extension
participant S as ExtensionSettings
participant D as DockerCtx
participant Auth as Docker Auth
participant Other as Other Extension Tags
E->>E: _disable_running_extension()
E->>E: _create_extension_settings()
E->>E: _prepare_docker_auth()
E->>D: _pull_docker_image(docker_auth)
D-->>E: progress updates
E->>E: _clear_remaining_tags()
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `core/services/kraken/extension/extension.py:173-178` </location>
<code_context>
self._save_settings(new_extension)
+ return new_extension
+
+ def _prepare_docker_auth(self) -> Optional[str]:
+ """Prepare Docker authentication string from source auth credentials."""
+ if self.source.auth is None:
+ return None
+ docker_auth = f"{self.source.auth.username}:{self.source.auth.password}"
+ return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")
+
+ async def _pull_docker_image(self, docker_auth: Optional[str]) -> AsyncGenerator[bytes, None]:
</code_context>
<issue_to_address>
**suggestion:** Consider handling empty username or password in Docker auth preparation.
Validate that both username and password are non-empty before encoding, and raise an error if either is missing to prevent invalid Docker auth strings.
```suggestion
def _prepare_docker_auth(self) -> Optional[str]:
"""Prepare Docker authentication string from source auth credentials."""
if self.source.auth is None:
return None
username = getattr(self.source.auth, "username", None)
password = getattr(self.source.auth, "password", None)
if not username or not password:
raise ValueError("Docker authentication requires both non-empty username and password.")
docker_auth = f"{username}:{password}"
return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8")
```
</issue_to_address>
### Comment 2
<location> `core/services/kraken/extension/extension.py:206` </location>
<code_context>
+ # First we should make sure no other tag is running
+ running_ext = await self._disable_running_extension()
+ self._create_extension_settings()
try:
self.lock(self.unique_entry)
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Extension settings are created before acquiring the lock.
Moving the creation and saving of extension settings inside the locked section will prevent race conditions during concurrent installs.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| def _prepare_docker_auth(self) -> Optional[str]: | ||
| """Prepare Docker authentication string from source auth credentials.""" | ||
| if self.source.auth is None: | ||
| return None | ||
| docker_auth = f"{self.source.auth.username}:{self.source.auth.password}" | ||
| return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8") |
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.
suggestion: Consider handling empty username or password in Docker auth preparation.
Validate that both username and password are non-empty before encoding, and raise an error if either is missing to prevent invalid Docker auth strings.
| def _prepare_docker_auth(self) -> Optional[str]: | |
| """Prepare Docker authentication string from source auth credentials.""" | |
| if self.source.auth is None: | |
| return None | |
| docker_auth = f"{self.source.auth.username}:{self.source.auth.password}" | |
| return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8") | |
| def _prepare_docker_auth(self) -> Optional[str]: | |
| """Prepare Docker authentication string from source auth credentials.""" | |
| if self.source.auth is None: | |
| return None | |
| username = getattr(self.source.auth, "username", None) | |
| password = getattr(self.source.auth, "password", None) | |
| if not username or not password: | |
| raise ValueError("Docker authentication requires both non-empty username and password.") | |
| docker_auth = f"{username}:{password}" | |
| return base64.b64encode(docker_auth.encode("utf-8")).decode("utf-8") |
| # First we should make sure no other tag is running | ||
| running_ext = await self._disable_running_extension() | ||
|
|
||
| self._create_extension_settings() |
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.
suggestion (bug_risk): Extension settings are created before acquiring the lock.
Moving the creation and saving of extension settings inside the locked section will prevent race conditions during concurrent installs.
Summary by Sourcery
Refactor the install function by extracting its substeps into private helper methods to reduce complexity and improve readability
Enhancements:
_disable_running_extension_create_extension_settings_prepare_docker_auth_pull_docker_image_clear_remaining_tagsinstallmethod by orchestrating the new helper methods