From a13f2ba15b499acf3e6e5d475d34c78254bb840f Mon Sep 17 00:00:00 2001
From: Jarek Potiuk <jarek@potiuk.com>
Date: Thu, 24 Aug 2023 16:23:32 +0200
Subject: [PATCH] Revert "Avoid WSL2 ones when finding a context for Breeze
 (#33547)"

This reverts commit df7c170e55dd9af894947219fb8025d3387505bf.
---
 .../utils/docker_command_utils.py             | 34 +++++++------------
 dev/breeze/tests/test_docker_command_utils.py | 33 +++---------------
 2 files changed, 18 insertions(+), 49 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 d711d3d933814..37a2ba3f8a9f5 100644
--- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
@@ -17,7 +17,6 @@
 """Various utils to prepare docker and docker compose commands."""
 from __future__ import annotations
 
-import json
 import os
 import re
 import sys
@@ -801,30 +800,23 @@ def autodetect_docker_context():
 
     :return: name of the docker context to use
     """
-    result = run_command(
-        ["docker", "context", "ls", "--format=json"],
-        capture_output=True,
-        check=False,
-        text=True,
-    )
-    if result.returncode != 0:
+    output = run_command(["docker", "context", "ls", "-q"], capture_output=True, check=False, text=True)
+    if output.returncode != 0:
         get_console().print("[warning]Could not detect docker builder. Using default.[/]")
         return "default"
-    known_contexts = {info["Name"]: info for info in json.loads(result.stdout)}
-    if not known_contexts:
+    context_list = output.stdout.splitlines()
+    if not context_list:
         get_console().print("[warning]Could not detect docker builder. Using default.[/]")
         return "default"
-    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))
+    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]
     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 d16dada5903a5..4b1513e30fc71 100644
--- a/dev/breeze/tests/test_docker_command_utils.py
+++ b/dev/breeze/tests/test_docker_command_utils.py
@@ -195,42 +195,19 @@ 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",
         ),
-        (
-            json.dumps([_fake_ctx("a"), _fake_ctx("default"), _fake_ctx("desktop-linux")]),
-            "desktop-linux",
-            "[info]Using desktop-linux 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"),
     ],
 )
 def test_autodetect_docker_context(context_output: str, selected_context: str, console_output: str):
