-
Notifications
You must be signed in to change notification settings - Fork 32
♻️ Containers are also removed via agent when the dynamic-sidecar is stopped (⚠️ devops) #6924
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
Merged
pcrespov
merged 24 commits into
ITISFoundation:master
from
GitHK:pr-osparc-orphaned-containers-removal
Dec 17, 2024
Merged
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
dc1bd4f
added force container cleanup step when stopping services
e8c1aaa
fixed imports
23825e8
fixed imports
66bd31f
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
b15b4a0
fixed broken tests
2e7c6cd
display removed containers
9686a88
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
f21f43a
fixed failing test
e68afac
refactor test
4424d0f
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
83d9286
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
e41d9c5
adding message that is always visible if orphans are detected
688f10d
added validator for docker_node_id
2354e68
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
d426f7c
refactor
aa239cf
update specs
b71b054
refactor
f9ddcd0
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
039f781
Merge branch 'master' into pr-osparc-orphaned-containers-removal
GitHK 2d5cd71
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
d9bbfa5
fixed regex
a950fc3
Merge branch 'pr-osparc-orphaned-containers-removal' of github.com:Gi…
45e9d2e
fixed openapispecs
e452ef4
Merge remote-tracking branch 'upstream/master' into pr-osparc-orphane…
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
37 changes: 37 additions & 0 deletions
37
packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/agent/containers.py
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,37 @@ | ||
| import logging | ||
| from datetime import timedelta | ||
| from typing import Final | ||
|
|
||
| from models_library.docker import DockerNodeID | ||
| from models_library.projects_nodes_io import NodeID | ||
| from models_library.rabbitmq_basic_types import RPCMethodName, RPCNamespace | ||
| from pydantic import NonNegativeInt, TypeAdapter | ||
| from servicelib.logging_utils import log_decorator | ||
| from servicelib.rabbitmq import RabbitMQRPCClient | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| _REQUEST_TIMEOUT: Final[NonNegativeInt] = int(timedelta(minutes=60).total_seconds()) | ||
|
|
||
|
|
||
| @log_decorator(_logger, level=logging.DEBUG) | ||
| async def force_container_cleanup( | ||
| rabbitmq_rpc_client: RabbitMQRPCClient, | ||
| *, | ||
| docker_node_id: DockerNodeID, | ||
| swarm_stack_name: str, | ||
| node_id: NodeID, | ||
| ) -> None: | ||
| result = await rabbitmq_rpc_client.request( | ||
| RPCNamespace.from_entries( | ||
| { | ||
| "service": "agent", | ||
| "docker_node_id": docker_node_id, | ||
| "swarm_stack_name": swarm_stack_name, | ||
| } | ||
| ), | ||
| TypeAdapter(RPCMethodName).validate_python("force_container_cleanup"), | ||
| node_id=node_id, | ||
| timeout_s=_REQUEST_TIMEOUT, | ||
| ) | ||
| assert result is None # nosec |
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
20 changes: 20 additions & 0 deletions
20
services/agent/src/simcore_service_agent/api/rpc/_containers.py
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,20 @@ | ||
| import logging | ||
|
|
||
| from fastapi import FastAPI | ||
| from models_library.projects_nodes_io import NodeID | ||
| from servicelib.logging_utils import log_context | ||
| from servicelib.rabbitmq import RPCRouter | ||
|
|
||
| from ...services.containers_manager import ContainersManager | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
| router = RPCRouter() | ||
|
|
||
|
|
||
| @router.expose() | ||
| async def force_container_cleanup(app: FastAPI, *, node_id: NodeID) -> None: | ||
| with log_context( | ||
| _logger, logging.INFO, f"removing all orphan container for {node_id=}" | ||
| ): | ||
| await ContainersManager.get_from_app_state(app).force_container_cleanup(node_id) |
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
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
71 changes: 71 additions & 0 deletions
71
services/agent/src/simcore_service_agent/services/containers_manager.py
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,71 @@ | ||
| import logging | ||
| from dataclasses import dataclass, field | ||
|
|
||
| from aiodocker import Docker | ||
| from fastapi import FastAPI | ||
| from models_library.api_schemas_directorv2.services import ( | ||
| DYNAMIC_PROXY_SERVICE_PREFIX, | ||
| DYNAMIC_SIDECAR_SERVICE_PREFIX, | ||
| ) | ||
| from models_library.projects_nodes_io import NodeID | ||
| from servicelib.fastapi.app_state import SingletonInAppStateMixin | ||
|
|
||
| from .docker_utils import get_containers_with_prefixes, remove_container_forcefully | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ContainersManager(SingletonInAppStateMixin): | ||
| app_state_name: str = "containers_manager" | ||
|
|
||
| docker: Docker = field(default_factory=Docker) | ||
|
|
||
| async def force_container_cleanup(self, node_id: NodeID) -> None: | ||
| # compose all possible used container prefixes | ||
| proxy_prefix = f"{DYNAMIC_PROXY_SERVICE_PREFIX}_{node_id}" | ||
| dy_sidecar_prefix = f"{DYNAMIC_SIDECAR_SERVICE_PREFIX}_{node_id}" | ||
| user_service_prefix = f"{DYNAMIC_SIDECAR_SERVICE_PREFIX}-{node_id}" | ||
GitHK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| orphan_containers = await get_containers_with_prefixes( | ||
GitHK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.docker, {proxy_prefix, dy_sidecar_prefix, user_service_prefix} | ||
| ) | ||
| _logger.debug( | ||
| "Detected orphan containers for node_id='%s': %s", | ||
| node_id, | ||
| orphan_containers, | ||
| ) | ||
|
|
||
| unexpected_orphans = { | ||
GitHK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| orphan | ||
| for orphan in orphan_containers | ||
| if orphan.startswith(user_service_prefix) | ||
| } | ||
| if unexpected_orphans: | ||
| _logger.warning( | ||
| "Unexpected orphans detected for node_id='%s': %s", | ||
GitHK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| node_id, | ||
| unexpected_orphans, | ||
| ) | ||
|
|
||
| # avoids parallel requests to docker engine | ||
| for container in orphan_containers: | ||
| await remove_container_forcefully(self.docker, container) | ||
|
|
||
| async def shutdown(self) -> None: | ||
| await self.docker.close() | ||
|
|
||
|
|
||
| def get_containers_manager(app: FastAPI) -> ContainersManager: | ||
| return ContainersManager.get_from_app_state(app) | ||
|
|
||
|
|
||
| def setup_containers_manager(app: FastAPI) -> None: | ||
| async def _on_startup() -> None: | ||
| ContainersManager().set_to_app_state(app) | ||
|
|
||
| async def _on_shutdown() -> None: | ||
| await ContainersManager.get_from_app_state(app).shutdown() | ||
|
|
||
| app.add_event_handler("startup", _on_startup) | ||
| app.add_event_handler("shutdown", _on_shutdown) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.