From 4f7eca1c2dc46cc81b01155c4e5d7fec10b694aa Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Fri, 11 Oct 2024 13:21:53 -0400 Subject: [PATCH] setup: check that docker desktop users have enabled kubernetes --- src/warnet/project.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/warnet/project.py b/src/warnet/project.py index 7e089e8f5..22bd8f6b9 100644 --- a/src/warnet/project.py +++ b/src/warnet/project.py @@ -151,6 +151,21 @@ def is_docker_desktop_running() -> tuple[bool, str]: except FileNotFoundError as err: return False, str(err) + def is_docker_desktop_kube_running() -> tuple[bool, str]: + try: + cluster_info = subprocess.run( + ["kubectl", "cluster-info", "--request-timeout=1"], + capture_output=True, + text=True, + ) + if cluster_info.returncode == 0: + return True, f"\n\t{cluster_info.stdout.strip().replace('\n', '\n\t')}" + else: + return False, "" + except Exception: + print() + return False, "Please enable kubernetes in Docker Desktop" + def is_kubectl_installed_and_offer_if_not() -> tuple[bool, str]: try: version_result = subprocess.run( @@ -264,6 +279,12 @@ def check_installation(tool_info: ToolInfo) -> ToolStatus: install_instruction="Please make sure docker is running", install_url="https://docs.docker.com/engine/install/", ) + docker_desktop_kube_running = ToolInfo( + tool_name="Kubernetes Running in Docker Desktop", + is_installed_func=is_docker_desktop_kube_running, + install_instruction="Please enable the local kubernetes cluster in Docker Desktop", + install_url="https://docs.docker.com/desktop/kubernetes/", + ) minikube_running_info = ToolInfo( tool_name="Running Minikube", is_installed_func=is_minikube_running, @@ -319,10 +340,13 @@ def check_installation(tool_info: ToolInfo) -> ToolStatus: check_results: list[ToolStatus] = [] if answers: + check_results.append(check_installation(kubectl_info)) + check_results.append(check_installation(helm_info)) if answers["platform"] == "Docker Desktop": check_results.append(check_installation(docker_info)) check_results.append(check_installation(docker_desktop_info)) check_results.append(check_installation(docker_running_info)) + check_results.append(check_installation(docker_desktop_kube_running)) elif answers["platform"] == "Minikube": check_results.append(check_installation(docker_info)) check_results.append(check_installation(docker_running_info)) @@ -330,8 +354,6 @@ def check_installation(tool_info: ToolInfo) -> ToolStatus: if is_platform_darwin(): check_results.append(check_installation(minikube_version_info)) check_results.append(check_installation(minikube_running_info)) - check_results.append(check_installation(kubectl_info)) - check_results.append(check_installation(helm_info)) else: click.secho("Please re-run setup.", fg="yellow") sys.exit(1)