Skip to content

Commit

Permalink
feat: Add support for loading in-cluster config when running inside a…
Browse files Browse the repository at this point in the history
… 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 openedx#843
  • Loading branch information
CodeWithEmad authored and regisb committed Jun 6, 2023
1 parent 4bb8671 commit 86f846a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion tutor/commands/k8s.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from datetime import datetime
from time import sleep
from typing import Any, Iterable, List, Optional, Type
Expand Down Expand Up @@ -26,7 +27,20 @@ def __init__(self) -> None:
# 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
Expand Down

0 comments on commit 86f846a

Please sign in to comment.