Skip to content

Commit

Permalink
Avoid WSL2 ones when finding a context for Breeze (apache#33547)
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr authored Aug 24, 2023
1 parent 9fa782f commit df7c170
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
34 changes: 21 additions & 13 deletions dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Various utils to prepare docker and docker compose commands."""
from __future__ import annotations

import json
import os
import re
import sys
Expand Down Expand Up @@ -800,23 +801,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.[/]"
Expand Down
33 changes: 28 additions & 5 deletions dev/breeze/tests/test_docker_command_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit df7c170

Please sign in to comment.