-
Notifications
You must be signed in to change notification settings - Fork 30
♻️ connecting docker-api-proxy to services which require docker swarm socket #8773
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
base: master
Are you sure you want to change the base?
Changes from 23 commits
480952b
299c6fb
b7bb15b
5543640
c6ab37f
8dc7988
f339177
be2f7cf
e964cfe
9e9a78a
ce4121e
4ec57ae
4ad68b1
f5c13eb
f63f62b
65b3744
601acd1
33c739b
02f0ac9
403a166
569731e
4a52a58
2b0284e
af513e5
ca69302
c536ce0
0e0503f
1054025
3cc5654
455355f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| import logging | ||
| from collections.abc import Callable | ||
| from contextlib import AsyncExitStack | ||
|
|
||
| import aiodocker | ||
| import pytest | ||
| from aiohttp import BasicAuth, ClientSession, ClientTimeout | ||
| from fastapi import FastAPI | ||
| from pydantic import TypeAdapter | ||
| from pytest_mock.plugin import MockerFixture | ||
| from settings_library.docker_api_proxy import DockerApiProxysettings | ||
| from tenacity import before_sleep_log, retry, stop_after_delay, wait_fixed | ||
|
|
||
|
|
@@ -58,3 +63,24 @@ async def docker_api_proxy_settings( | |
| await _wait_till_docker_api_proxy_is_responsive(settings) | ||
|
|
||
| return settings | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def mock_setup_remote_docker_client(mocker: MockerFixture) -> Callable[[str], None]: | ||
| def _(target_setip_to_replace: str) -> None: | ||
| def _setup(app: FastAPI, settings: DockerApiProxysettings) -> None: | ||
| _ = settings | ||
| exit_stack = AsyncExitStack() | ||
|
|
||
| async def on_startup() -> None: | ||
| app.state.remote_docker_client = await exit_stack.enter_async_context(aiodocker.Docker()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 question: did you check what happens if the connection to the docker engine is broken. does this client reconnects?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's based on HTTP not TCP, so there is no such issue. If the connection comes back this will work as expcted |
||
|
|
||
| async def on_shutdown() -> None: | ||
| await exit_stack.aclose() | ||
|
|
||
| app.add_event_handler("startup", on_startup) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not using lifespan events in the fast app instead of these deprecate events https://fastapi.tiangolo.com/advanced/events/ if new code keeps the deprecated events ... we will never get the code up to date. Right now we have a nasty melange of all of these events :-(
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because not all services support them. And we cannot mix them easily. Either the entire service is lifespan based or setup based. |
||
| app.add_event_handler("shutdown", on_shutdown) | ||
|
|
||
| mocker.patch(target_setip_to_replace, new=_setup) | ||
|
|
||
| return _ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,31 @@ async def remote_docker_client_lifespan(app: FastAPI, state: State) -> AsyncIter | |
| yield {} | ||
|
|
||
|
|
||
| def setup_remote_docker_client(app: FastAPI, settings: DockerApiProxysettings) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this not be using the new lifespan mechanisms?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should not be a "remote_docker_client" but the "docker_client".
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a lifespan, for services which use it. In this situation it is required since not all of them use it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intent of the name is to avoid any possible confusion that this is a docker client that uses the local docker socket. This is client is intended to use docker swarm API and runs only on maser nodes. If you are on a worker and need to list the containers, this will not work, since the connection will point to a docker master. If you don't like the name, I suggest to make it even more obvious. Something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the reasons above, this should never user the local docker socket |
||
| exit_stack = AsyncExitStack() | ||
|
|
||
| async def on_startup() -> None: | ||
| session = await exit_stack.enter_async_context( | ||
| ClientSession( | ||
| auth=aiohttp.BasicAuth( | ||
| login=settings.DOCKER_API_PROXY_USER, | ||
| password=settings.DOCKER_API_PROXY_PASSWORD.get_secret_value(), | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| app.state.remote_docker_client = await exit_stack.enter_async_context( | ||
| aiodocker.Docker(url=settings.base_url, session=session) | ||
| ) | ||
| await wait_till_docker_api_proxy_is_responsive(app) | ||
|
|
||
| async def on_shutdown() -> None: | ||
| await exit_stack.aclose() | ||
|
|
||
| app.add_event_handler("startup", on_startup) | ||
|
GitHK marked this conversation as resolved.
|
||
| app.add_event_handler("shutdown", on_shutdown) | ||
|
|
||
|
|
||
| @tenacity.retry( | ||
| wait=tenacity.wait_fixed(5), | ||
| stop=tenacity.stop_after_delay(60), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.