|
14 | 14 | import threading |
15 | 15 | import time |
16 | 16 | import webbrowser |
| 17 | +import subprocess |
17 | 18 |
|
18 | 19 | from azext_aks_preview._client_factory import ( |
19 | 20 | CUSTOM_MGMT_AKS_PREVIEW, |
|
72 | 73 | get_all_extensions_in_allow_list, |
73 | 74 | raise_validation_error_if_extension_type_not_in_allow_list, |
74 | 75 | get_extension_in_allow_list, |
| 76 | + uses_kubelogin_devicecode, |
| 77 | + which, |
75 | 78 | ) |
76 | 79 | from azext_aks_preview._podidentity import ( |
77 | 80 | _ensure_managed_identity_operator_permission, |
@@ -1526,6 +1529,29 @@ def aks_get_credentials( |
1526 | 1529 | encoding='UTF-8') |
1527 | 1530 | print_or_merge_credentials( |
1528 | 1531 | path, kubeconfig, overwrite_existing, context_name) |
| 1532 | + # Check if kubeconfig requires kubelogin with devicecode and convert it |
| 1533 | + if uses_kubelogin_devicecode(kubeconfig): |
| 1534 | + if which("kubelogin"): |
| 1535 | + try: |
| 1536 | + # Run kubelogin convert-kubeconfig -l azurecli |
| 1537 | + subprocess.run( |
| 1538 | + ["kubelogin", "convert-kubeconfig", "-l", "azurecli"], |
| 1539 | + cwd=os.path.dirname(path), |
| 1540 | + check=True, |
| 1541 | + ) |
| 1542 | + logger.warning("Converted kubeconfig to use Azure CLI authentication.") |
| 1543 | + except subprocess.CalledProcessError as e: |
| 1544 | + logger.warning("Failed to convert kubeconfig with kubelogin: %s", str(e)) |
| 1545 | + except Exception as e: # pylint: disable=broad-except |
| 1546 | + logger.warning("Error running kubelogin: %s", str(e)) |
| 1547 | + else: |
| 1548 | + logger.warning( |
| 1549 | + "The kubeconfig uses devicecode authentication which requires kubelogin. " |
| 1550 | + "Please install kubelogin from https://github.com/Azure/kubelogin or run " |
| 1551 | + "'az aks install-cli' to install both kubectl and kubelogin. " |
| 1552 | + "If devicecode login fails, try running " |
| 1553 | + "'kubelogin convert-kubeconfig -l azurecli' to unblock yourself." |
| 1554 | + ) |
1529 | 1555 | except (IndexError, ValueError) as exc: |
1530 | 1556 | raise CLIError("Fail to find kubeconfig file.") from exc |
1531 | 1557 |
|
|
0 commit comments