This is an automated email from the ASF dual-hosted git repository. ephraimanierobi pushed a commit to branch v2-7-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit df43cbb6c57d40d887cbd2c303f68952a729a5e5 Author: Jarek Potiuk <[email protected]> AuthorDate: Fri Sep 22 05:13:50 2023 -0400 Avoid WSL2 ones when finding a context for Breeze (#34538) * Avoid WSL2 ones when finding a context for Breeze * fixup! Avoid WSL2 ones when finding a context for Breeze --------- Co-authored-by: Tzu-ping Chung <[email protected]> (cherry picked from commit 2e764fb0978fde33f59918416bd2732294c4bf23) --- .../airflow_breeze/utils/docker_command_utils.py | 38 ++++++++++++++-------- dev/breeze/tests/test_docker_command_utils.py | 33 ++++++++++++++++--- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py index b7b8041cb5..6aae84fb5f 100644 --- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py +++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py @@ -18,6 +18,7 @@ from __future__ import annotations import copy +import json import os import random import re @@ -822,23 +823,34 @@ def autodetect_docker_context(): :return: name of the docker context to use """ - output = run_command(["docker", "context", "ls", "-q"], capture_output=True, check=False, text=True) - if output.returncode != 0: + result = run_command( + ["docker", "context", "ls", "--format=json"], + capture_output=True, + check=False, + text=True, + ) + if result.returncode != 0: get_console().print("[warning]Could not detect docker builder. Using default.[/]") return "default" - context_list = output.stdout.splitlines() - if not context_list: + context_json = json.loads(result.stdout) + if isinstance(context_json, dict): + # In case there is one context it is returned as dict not array of dicts ¯\_(ツ)_/¯ + context_json = [context_json] + known_contexts = {info["Name"]: info for info in context_json} + if not known_contexts: get_console().print("[warning]Could not detect docker builder. Using default.[/]") return "default" - elif len(context_list) == 1: - get_console().print(f"[info]Using {context_list[0]} as context.[/]") - return context_list[0] - else: - for preferred_context in PREFERRED_CONTEXTS: - if preferred_context in context_list: - get_console().print(f"[info]Using {preferred_context} as context.[/]") - return preferred_context - fallback_context = context_list[0] + for preferred_context_name in PREFERRED_CONTEXTS: + try: + context = known_contexts[preferred_context_name] + except KeyError: + continue + # On Windows, some contexts are used for WSL2. We don't want to use those. + if context["DockerEndpoint"] == "npipe:////./pipe/dockerDesktopLinuxEngine": + continue + get_console().print(f"[info]Using {preferred_context_name} as context.[/]") + return preferred_context_name + fallback_context = next(iter(known_contexts)) get_console().print( f"[warning]Could not use any of the preferred docker contexts {PREFERRED_CONTEXTS}.\n" f"Using {fallback_context} as context.[/]" diff --git a/dev/breeze/tests/test_docker_command_utils.py b/dev/breeze/tests/test_docker_command_utils.py index 83081ad770..b125fb2bd7 100644 --- a/dev/breeze/tests/test_docker_command_utils.py +++ b/dev/breeze/tests/test_docker_command_utils.py @@ -191,19 +191,42 @@ def test_check_docker_compose_version_ok(mock_get_console, mock_run_command): ) +def _fake_ctx(name: str) -> dict[str, str]: + return { + "Name": name, + "DockerEndpoint": f"unix://{name}", + } + + @pytest.mark.parametrize( "context_output, selected_context, console_output", [ ( + json.dumps([_fake_ctx("default")]), "default", + "[info]Using default as context", + ), + ("[]", "default", "[warning]Could not detect docker builder"), + ( + json.dumps([_fake_ctx("a"), _fake_ctx("b")]), + "a", + "[warning]Could not use any of the preferred docker contexts", + ), + ( + json.dumps([_fake_ctx("a"), _fake_ctx("desktop-linux")]), + "desktop-linux", + "[info]Using desktop-linux as context", + ), + ( + json.dumps([_fake_ctx("a"), _fake_ctx("default")]), "default", "[info]Using default as context", ), - ("", "default", "[warning]Could not detect docker builder"), - ("a\nb", "a", "[warning]Could not use any of the preferred docker contexts"), - ("a\ndesktop-linux", "desktop-linux", "[info]Using desktop-linux as context"), - ("a\ndefault", "default", "[info]Using default as context"), - ("a\ndefault\ndesktop-linux", "desktop-linux", "[info]Using desktop-linux as context"), + ( + json.dumps([_fake_ctx("a"), _fake_ctx("default"), _fake_ctx("desktop-linux")]), + "desktop-linux", + "[info]Using desktop-linux as context", + ), ], ) def test_autodetect_docker_context(context_output: str, selected_context: str, console_output: str):
