Skip to content

Commit 0b10ef9

Browse files
committed
Warn user of kubeconfig context switch during sky local up
1 parent 9aae657 commit 0b10ef9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

sky/cli.py

+9
Original file line numberDiff line numberDiff line change
@@ -4419,6 +4419,15 @@ def local():
44194419
def local_up():
44204420
"""Creates a local cluster."""
44214421
cluster_created = False
4422+
# Check if ~/.kube/config exists:
4423+
if os.path.exists(os.path.expanduser('~/.kube/config')):
4424+
current_context = kubernetes_utils.get_current_kube_config_context()
4425+
skypilot_context = "kind-skypilot"
4426+
if current_context is not None and current_context != skypilot_context:
4427+
click.echo(
4428+
f'Current context in kube config: {current_context}'
4429+
'\nWill automatically switch to kind-skypilot after the local cluster is created.'
4430+
)
44224431
with log_utils.safe_rich_status('Creating local cluster...'):
44234432
path_to_package = os.path.dirname(os.path.dirname(__file__))
44244433
up_script_path = os.path.join(path_to_package, 'sky/utils/kubernetes',

sky/skylet/providers/kubernetes/utils.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Tuple, Optional
1+
from typing import List, Tuple, Optional, Union
22

33
from sky import status_lib
44
from sky.adaptors import kubernetes
@@ -66,3 +66,18 @@ def get_cluster_status(cluster_name: str,
6666
cluster_status.append(status_lib.ClusterStatus.INIT)
6767
# If pods are not found, we don't add them to the return list
6868
return cluster_status
69+
70+
71+
def get_current_kube_config_context() -> Union[str, None]:
72+
"""
73+
Get the current kubernetes context from the kubeconfig file
74+
75+
Returns:
76+
str | None: The current kubernetes context if it exists, None otherwise
77+
"""
78+
k8s = kubernetes.get_kubernetes()
79+
try:
80+
_, current_context = k8s.config.list_kube_config_contexts()
81+
return current_context['name']
82+
except k8s.config.config_exception.ConfigException:
83+
return None

0 commit comments

Comments
 (0)