Skip to content

Commit

Permalink
[k8s] Fix in-cluster auth namespace fetching (#4420)
Browse files Browse the repository at this point in the history
* Fix incluster auth namespace fetching

* Fixes
  • Loading branch information
romilbhardwaj authored Nov 27, 2024
1 parent 74a8075 commit 7c20bd8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions sky/provision/kubernetes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,11 +1045,15 @@ def get_kube_config_context_namespace(
the default namespace.
"""
k8s = kubernetes.kubernetes
# Get namespace if using in-cluster config
ns_path = '/var/run/secrets/kubernetes.io/serviceaccount/namespace'
if os.path.exists(ns_path):
with open(ns_path, encoding='utf-8') as f:
return f.read().strip()
# If using in-cluster context, get the namespace from the service account
# namespace file. Uses the same logic as adaptors.kubernetes._load_config()
# to stay consistent with in-cluster config loading.
if (context_name == kubernetes.in_cluster_context_name() or
context_name is None):
if os.path.exists(ns_path):
with open(ns_path, encoding='utf-8') as f:
return f.read().strip()
# If not in-cluster, get the namespace from kubeconfig
try:
contexts, current_context = k8s.config.list_kube_config_contexts()
Expand Down

0 comments on commit 7c20bd8

Please sign in to comment.