You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2023/09/22 03:44:22 UTC
[airflow] 01/01: Avoid WSL2 ones when finding a context for Breeze
This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch avoid-wsl2-desktop-linux-context
in repository https://gitbox.apache.org/repos/asf/airflow.git
commit b6dfdd35631db3b574a758528dcd9b41c5376f0d
Author: Tzu-ping Chung <ur...@gmail.com>
AuthorDate: Sun Aug 20 14:55:10 2023 +0800
Avoid WSL2 ones when finding a context for Breeze
---
.../airflow_breeze/utils/docker_command_utils.py | 34 +++++++++++++---------
dev/breeze/tests/test_docker_command_utils.py | 33 +++++++++++++++++----
2 files changed, 49 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 3dd9b4ffb6..25c443a279 100644
--- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
@@ -17,6 +17,7 @@
"""Various utils to prepare docker and docker compose commands."""
from __future__ import annotations
+import json
import copy
import os
import random
@@ -805,23 +806,30 @@ 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:
+ known_contexts = {info["Name"]: info for info in json.loads(result.stdout)}
+ 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 4b1513e30f..d16dada590 100644
--- a/dev/breeze/tests/test_docker_command_utils.py
+++ b/dev/breeze/tests/test_docker_command_utils.py
@@ -195,19 +195,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):