Repository: skypilot-org/skypilot
Pull request: [k8s] Hints for querying stale kube current context
<!-- Describe the changes in this PR -->
Fixes #5258 
![Screenshot 2025-04-18 at 10 04 28 AM](https://github.com/user-attachments/assets/ac4d805b-8b2c-46a9-968e-31380c61eb86)



<!-- Describe the tests ran -->
<!-- Unit tests (tests/test_*.py) are part of GitHub CI; below are tests that launch on the cloud. -->

Tested (run the relevant ones):

- [X] Code formatting: install pre-commit (auto-check on commit) or `bash format.sh`
- [X] Any manual or new tests for this PR (please specify below)
- [ ] All smoke tests: `/smoke-test` (CI) or `pytest tests/test_smoke.py` (local)
- [ ] Relevant individual tests: `/smoke-test -k test_name` (CI) or `pytest tests/test_smoke.py::test_name` (local)
- [ ] Backward compatibility: `/quicktest-core` (CI) or `pytest tests/smoke_tests/test_backward_compat.py` (local)

<!-- CI commands (/-prefixed) can only be triggered by repo members -->


Problem statement:
[k8s] Confusing Error for Stale Current-Context in KubeConfig File
```
E 04-17 13:02:39 sdk.py:1496] === Traceback on SkyPilot API Server ===
...
E 04-17 13:02:39 sdk.py:1496]     _load_config(context)
E 04-17 13:02:39 sdk.py:1496]   File "/Users/kyuds/dev/skypilot/sky/adaptors/kubernetes.py", line 102, in _load_config
E 04-17 13:02:39 sdk.py:1496]     _load_config_from_kubeconfig()
E 04-17 13:02:39 sdk.py:1496]   File "/Users/kyuds/dev/skypilot/sky/adaptors/kubernetes.py", line 89, in _load_config_from_kubeconfig
E 04-17 13:02:39 sdk.py:1496]     raise ValueError(err_str) from None
E 04-17 13:02:39 sdk.py:1496] ValueError: Failed to load Kubernetes configuration for None. Please check if your kubeconfig file exists at ~/.kube/config and is valid.
E 04-17 13:02:39 sdk.py:1496] [kubernetes.config.config_exception.ConfigException] Invalid kube-config file. Expected object with name kyuds@<redacted> in /Users/kyuds/.kube/config/contexts list
E 04-17 13:02:39 sdk.py:1496] To disable Kubernetes for SkyPilot: run `sky check`.
E 04-17 13:02:39 sdk.py:1496]
D 04-17 13:02:39 sdk.py:82] To stream request logs: sky api logs <redacted>
ValueError: Failed to load Kubernetes configuration for None. Please check if your kubeconfig file exists at ~/.kube/config and is valid.
[kubernetes.config.config_exception.ConfigException] Invalid kube-config file. Expected object with name kyuds@<redacted> in /Users/kyuds/.kube/config/contexts list
To disable Kubernetes for SkyPilot: run `sky check`.
```
when we remove the current-context from kubeconfig using `kubectl`, the current-context still records the now deleted context. If the kubeconfig file contains another context and we run `sky jobs launch`, then skypilot will automatically try to check the "current-context" (now non-existent" first hence causing this error.

While this is more of a problem with `kubectl` than skypilot, we should nevertheless give users a hint to check their current-context value.

Hints from issue discussion:

Commit message: styling

Patch to review:
diff --git a/sky/adaptors/kubernetes.py b/sky/adaptors/kubernetes.py
index 1e8607870c3..e8a098c166b 100644
--- a/sky/adaptors/kubernetes.py
+++ b/sky/adaptors/kubernetes.py
@@ -3,6 +3,8 @@
 import os
 from typing import Any, Callable, Optional, Set
 
+import colorama
+
 from sky.adaptors import common
 from sky.sky_logging import set_logging_level
 from sky.utils import annotations
@@ -85,6 +87,11 @@ def _load_config_from_kubeconfig(context: Optional[str] = None):
                     'Please check if your kubeconfig file exists at '
                     f'{kubeconfig_path} and is valid.\n{suffix}')
             err_str += '\nTo disable Kubernetes for SkyPilot: run `sky check`.'
+            if context is None:  # kubernetes defaults to current-context.
+                err_str += (
+                    f'\n{colorama.Fore.YELLOW}Hint: Kubernetes attempted '
+                    'to query the current-context set in kubeconfig. Check if '
+                    f'the current-context is valid.{colorama.Style.RESET_ALL}')
             with ux_utils.print_exception_no_traceback():
                 raise ValueError(err_str) from None
 


Code review comments:
diff_hunk: @@ -85,6 +87,11 @@ def _load_config_from_kubeconfig(context: Optional[str] = None):
                     'Please check if your kubeconfig file exists at '
                     f'{kubeconfig_path} and is valid.\n{suffix}')
             err_str += '\nTo disable Kubernetes for SkyPilot: run `sky check`.'
+            if context is None:  # kubernetes defaults to current-context.
+                err_str += (
+                    f'\n{colorama.Fore.YELLOW}Hint: Kubernetes attempted '
path: sky/adaptors/kubernetes.py
text: @user1:
Best practice is to have simple error messages in strs without any ascii codes. Remove colorama from here and the import?

@author:
done