Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle kube config management for sky local commands #2253

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions sky/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4421,13 +4421,13 @@ def local_up():
cluster_created = False
# Check if ~/.kube/config exists:
if os.path.exists(os.path.expanduser('~/.kube/config')):
# Check if kubeconfig is valid, `kind delete` leaves an empty kubeconfig
valid, reason = kubernetes_utils.check_credentials()
if valid or (not valid and 'Invalid configuration' not in reason):
# Could be a valid kubeconfig or a non-empty but non-functioning
# kubeconfig - check if user wants to overwrite it
prompt = 'Cluster config found at ~/.kube/config. Overwrite it?'
click.confirm(prompt, default=True, abort=True, show_default=True)
current_context = kubernetes_utils.get_current_kube_config_context()
skypilot_context = "kind-skypilot"
if current_context is not None and current_context != skypilot_context:
click.echo(
f'Current context in kube config: {current_context}'
'\nWill automatically switch to kind-skypilot after the local cluster is created.'
)
with log_utils.safe_rich_status('Creating local cluster...'):
path_to_package = os.path.dirname(os.path.dirname(__file__))
up_script_path = os.path.join(path_to_package, 'sky/utils/kubernetes',
Expand Down
15 changes: 15 additions & 0 deletions sky/skylet/providers/kubernetes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ def get_cluster_status(cluster_name: str,
cluster_status.append(status_lib.ClusterStatus.INIT)
# If pods are not found, we don't add them to the return list
return cluster_status


def get_current_kube_config_context() -> Optional[str]:
"""
Get the current kubernetes context from the kubeconfig file

Returns:
str | None: The current kubernetes context if it exists, None otherwise
"""
k8s = kubernetes.get_kubernetes()
try:
_, current_context = k8s.config.list_kube_config_contexts()
return current_context['name']
except k8s.config.config_exception.ConfigException:
return None
7 changes: 7 additions & 0 deletions sky/utils/kubernetes/delete_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ fi

kind delete cluster --name skypilot
echo "Local cluster deleted!"

# Switch to the first available context
AVAILABLE_CONTEXT=$(kubectl config get-contexts -o name | head -n 1)
if [ ! -z "$AVAILABLE_CONTEXT" ]; then
echo "Switching to context $AVAILABLE_CONTEXT"
kubectl config use-context $AVAILABLE_CONTEXT
fi