|
48 | 48 | from knack.prompting import NoTTYException, prompt_y_n |
49 | 49 | from kubernetes import client as kube_client |
50 | 50 | from kubernetes import config |
| 51 | +from kubernetes.client.rest import ApiException |
51 | 52 | from kubernetes.config.kube_config import KubeConfigMerger |
52 | 53 | from packaging import version |
53 | 54 |
|
@@ -1538,22 +1539,48 @@ def set_security_profile(enable_workload_identity: bool) -> SecurityProfile: |
1538 | 1539 | return security_profile |
1539 | 1540 |
|
1540 | 1541 |
|
1541 | | -def get_k8s_extension_module(module_name): |
| 1542 | +def get_installed_bundle_extensions() -> list[str]: |
| 1543 | + api_instance = kube_client.CustomObjectsApi() |
| 1544 | + |
1542 | 1545 | try: |
1543 | | - # adding the installed extension in the path |
1544 | | - from azure.cli.core.extension.operations import add_extension_to_path |
| 1546 | + extension_configs = api_instance.list_cluster_custom_object( |
| 1547 | + group=consts.Extension_Config_CRD_Group, |
| 1548 | + version=consts.Extension_Config_CRD_Version, |
| 1549 | + plural=consts.Extension_Config_CRD_Plural, |
| 1550 | + ) |
| 1551 | + except Exception as e: |
| 1552 | + if isinstance(e, ApiException) and e.status == 404: |
| 1553 | + # If the ExtensionConfig resource is not found, return an empty list |
| 1554 | + return [] |
| 1555 | + |
| 1556 | + utils.kubernetes_exception_handler( |
| 1557 | + e, |
| 1558 | + consts.List_Extension_Config_Fault_Type, |
| 1559 | + "Failed to list ExtensionConfigs", |
| 1560 | + error_message="Failed to list ExtensionConfigs: ", |
| 1561 | + ) |
1545 | 1562 |
|
1546 | | - add_extension_to_path(consts.CONST_K8S_EXTENSION_NAME) |
1547 | | - # import the extension module |
1548 | | - from importlib import import_module |
| 1563 | + try: |
| 1564 | + installed_extensions = [] |
| 1565 | + for config in extension_configs.get("items", []): |
| 1566 | + config_spec = config.get("spec", {}) |
| 1567 | + isPartOfBundle = config_spec.get("isPartOfBundle") |
| 1568 | + isDependentOnBundle = config_spec.get("isDependentOnBundle") |
| 1569 | + extensionType = config_spec.get("extensionType") |
| 1570 | + if (isPartOfBundle or isDependentOnBundle) and extensionType: |
| 1571 | + installed_extensions.append(extensionType) |
1549 | 1572 |
|
1550 | | - azext_custom = import_module(module_name) |
1551 | | - return azext_custom |
1552 | | - except ImportError: |
1553 | | - raise ImportError( # pylint: disable=raise-missing-from |
1554 | | - "Please add CLI extension `k8s-extension` to disable bundle feature flag.\n" |
1555 | | - "Run command `az extension add --name k8s-extension`" |
| 1573 | + except Exception as e: # pylint: disable=broad-except |
| 1574 | + telemetry.set_exception( |
| 1575 | + exception=e, |
| 1576 | + fault_type=consts.Get_Extension_Config_Bundle_Property_Fault_Type, |
| 1577 | + summary="Failed to get bundle properties from the Arc extension config", |
1556 | 1578 | ) |
| 1579 | + raise CLIInternalError( |
| 1580 | + f"Failed to get bundle properties from the Arc extension config: {e}" |
| 1581 | + ) |
| 1582 | + |
| 1583 | + return installed_extensions |
1557 | 1584 |
|
1558 | 1585 |
|
1559 | 1586 | def get_bundle_feature_flag_from_arc_agentry_config( |
@@ -1755,34 +1782,7 @@ def validate_update_cluster_bundle_feature_flag_value( |
1755 | 1782 | current_bundle_feature_flag_value == "enabled" |
1756 | 1783 | and bundle_feature_flag_value == "disabled" |
1757 | 1784 | ): |
1758 | | - client_factory = get_k8s_extension_module( |
1759 | | - consts.CONST_K8S_EXTENSION_CLIENT_FACTORY_MOD_NAME |
1760 | | - ) |
1761 | | - client = client_factory.cf_k8s_extension_operation(cmd.cli_ctx) |
1762 | | - k8s_extension_custom_mod = get_k8s_extension_module( |
1763 | | - consts.CONST_K8S_EXTENSION_CUSTOM_MOD_NAME |
1764 | | - ) |
1765 | | - |
1766 | | - try: |
1767 | | - extensions = k8s_extension_custom_mod.list_k8s_extension( |
1768 | | - client, |
1769 | | - resource_group_name, |
1770 | | - cluster_name, |
1771 | | - consts.Connected_Cluster_Type, |
1772 | | - ) |
1773 | | - |
1774 | | - except Exception as e: # pylint: disable=broad-except |
1775 | | - utils.arm_exception_handler( |
1776 | | - e, |
1777 | | - consts.List_K8s_Extension_Fault_Type, |
1778 | | - "Failed to list installed k8s extensions for the connected cluster.", |
1779 | | - ) |
1780 | | - |
1781 | | - installed_bundle_extensions = [ |
1782 | | - ext.extension_type.lower() |
1783 | | - for ext in extensions |
1784 | | - if ext.extension_type.lower() in consts.Bundle_Extension_Type_List |
1785 | | - ] |
| 1785 | + installed_bundle_extensions = get_installed_bundle_extensions() |
1786 | 1786 |
|
1787 | 1787 | if installed_bundle_extensions: |
1788 | 1788 | err_msg = ( |
@@ -2395,19 +2395,6 @@ def update_connected_cluster( |
2395 | 2395 | ) |
2396 | 2396 | raise InvalidArgumentValueError(err_msg) |
2397 | 2397 |
|
2398 | | - # Validate and update bundle feature flag value if provided |
2399 | | - validate_update_cluster_bundle_feature_flag_value( |
2400 | | - cmd, |
2401 | | - connected_cluster.arc_agentry_configurations, |
2402 | | - configuration_settings, |
2403 | | - resource_group_name, |
2404 | | - cluster_name, |
2405 | | - ) |
2406 | | - |
2407 | | - arc_agentry_configurations = generate_arc_agent_configuration( |
2408 | | - configuration_settings, redacted_protected_values |
2409 | | - ) |
2410 | | - |
2411 | 2398 | # Patching the connected cluster ARM resource |
2412 | 2399 | arm_properties_unset = ( |
2413 | 2400 | tags is None |
@@ -2476,6 +2463,19 @@ def update_connected_cluster( |
2476 | 2463 | # if the user had not logged in. |
2477 | 2464 | kubernetes_version = check_kube_connection() |
2478 | 2465 |
|
| 2466 | + # Validate and update bundle feature flag value if provided |
| 2467 | + validate_update_cluster_bundle_feature_flag_value( |
| 2468 | + cmd, |
| 2469 | + connected_cluster.arc_agentry_configurations, |
| 2470 | + configuration_settings, |
| 2471 | + resource_group_name, |
| 2472 | + cluster_name, |
| 2473 | + ) |
| 2474 | + |
| 2475 | + arc_agentry_configurations = generate_arc_agent_configuration( |
| 2476 | + configuration_settings, redacted_protected_values |
| 2477 | + ) |
| 2478 | + |
2479 | 2479 | # Install helm client |
2480 | 2480 | helm_client_location = install_helm_client(cmd) |
2481 | 2481 |
|
@@ -2999,7 +2999,7 @@ def upgrade_agents( |
2999 | 2999 | e, |
3000 | 3000 | consts.List_Kubernetes_Namespaced_Pod_Fault_Type, |
3001 | 3001 | "Unable to list pods", |
3002 | | - error_message=f"Unable to list pods in namespace '{namespace}' with label selector '{label_selector}'", |
| 3002 | + error_message=f"Unable to list pods in namespace '{namespace}' with label selector '{label_selector}': ", |
3003 | 3003 | raise_error=False, |
3004 | 3004 | ) |
3005 | 3005 |
|
|
0 commit comments