From 86f846aba35caa4fde7536200e4b9bd42fcc8531 Mon Sep 17 00:00:00 2001 From: Emad Rad Date: Thu, 1 Jun 2023 16:36:44 +0330 Subject: [PATCH] feat: Add support for loading in-cluster config when running inside a pod in some cases, tutor might run inside a pod, which that pod has access to a cluster via role binding and a service account. this way, there's no ./kube/config file, but kubectl commands run with no issue. Close #843 --- tutor/commands/k8s.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tutor/commands/k8s.py b/tutor/commands/k8s.py index db601f4..ce8c0c4 100644 --- a/tutor/commands/k8s.py +++ b/tutor/commands/k8s.py @@ -1,3 +1,4 @@ +import os from datetime import datetime from time import sleep from typing import Any, Iterable, List, Optional, Type @@ -26,7 +27,20 @@ class K8sClients: # pylint: disable=import-outside-toplevel from kubernetes import client, config - config.load_kube_config() + if os.path.exists(config.kube_config.KUBE_CONFIG_DEFAULT_LOCATION): + # found the kubeconfig file, let's load it! + config.load_kube_config() + elif ( + config.incluster_config.SERVICE_HOST_ENV_NAME in os.environ + and config.incluster_config.SERVICE_PORT_ENV_NAME in os.environ + ): + # We are running inside a cluster, let's load the in-cluster configuration. + config.load_incluster_config() + else: + raise exceptions.TutorError( + f"there is no Kubernetes configuration file located in {config.kube_config.KUBE_CONFIG_DEFAULT_LOCATION}, and the service environment variables {config.incluster_config.SERVICE_HOST_ENV_NAME} and {config.incluster_config.SERVICE_PORT_ENV_NAME} have not been defined. To connect to a cluster, please configure your host appropriately." + ) + self._batch_api = None self._core_api = None self._client = client