Skip to content

Commit a3287fb

Browse files
committed
Check the installed bundle extensions through the extension configs.
1 parent 9df2ace commit a3287fb

File tree

3 files changed

+61
-73
lines changed

3 files changed

+61
-73
lines changed

src/connectedk8s/azext_connectedk8s/_constants.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
Get_HelmRegistery_Path_Fault_Type = "helm-registry-path-fetch-error"
135135
Pull_HelmChart_Fault_Type = "helm-chart-pull-error"
136136
Export_HelmChart_Fault_Type = "helm-chart-export-error"
137+
List_Extension_Config_Fault_Type = "kubernetes-list-extension-config-error"
137138
List_Kubernetes_Namespaced_Pod_Fault_Type = "kubernetes-list-namespaced-pod-error"
138139
Get_Kubernetes_Distro_Fault_Type = "kubernetes-get-distribution-error"
139140
Get_Kubernetes_Namespace_Fault_Type = "kubernetes-get-namespace-error"
@@ -526,16 +527,12 @@
526527
Arc_Agentry_Bundle_Feature_Setting = "versionManagedExtensions"
527528

528529
Bundle_Feature_Value_List = ["enabled", "disabled", "preview"]
529-
Bundle_Extension_Type_List = [
530-
"microsoft.iotoperations",
531-
"microsoft.extensiondiagnostics",
532-
"microsoft.arc.containerstorage",
533-
"microsoft.azure.secretstore",
534-
]
535530

536-
CONST_K8S_EXTENSION_NAME = "k8s-extension"
537-
CONST_K8S_EXTENSION_CLIENT_FACTORY_MOD_NAME = "azext_k8s_extension._client_factory"
538-
CONST_K8S_EXTENSION_CUSTOM_MOD_NAME = "azext_k8s_extension.custom"
531+
Extension_Config_CRD_Group = "clusterconfig.azure.com"
532+
Extension_Config_CRD_Version = "v1beta1"
533+
Extension_Config_CRD_Plural = "extensionconfigs"
539534

540-
List_K8s_Extension_Fault_Type = "list-k8s-extension-error"
541535
Get_Bundle_Feature_Flag_Fault_Type = "get-bundle-feature-flag-error"
536+
Get_Extension_Config_Bundle_Property_Fault_Type = (
537+
"get-extension-config-bundle-property-error"
538+
)

src/connectedk8s/azext_connectedk8s/_help.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --enable-oidc-issuer --self-hosted-issuer aksissuerurl
3939
- name: Onboard a connected kubernetes cluster with azure gateway feature enabled.
4040
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --gateway-resource-id gatewayResourceId
41-
- name: Onboard a connected kubernetes cluster with the bundling feature enabled.
42-
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --disable-auto-upgrade --config extensionSets.versionManagedExtensions='enabled'
43-
- name: Onboard a connected Kubernetes cluster with the bundling feature in preview mode.
44-
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --disable-auto-upgrade --config extensionSets.versionManagedExtensions='preview'
4541
4642
"""
4743

@@ -65,11 +61,6 @@
6561
text: az connectedk8s update -g resourceGroupName -n connectedClusterName --gateway-resource-id gatewayResourceId
6662
- name: Disable the azure gateway feature on a connected kubernetes cluster.
6763
text: az connectedk8s update -g resourceGroupName -n connectedClusterName --disable-gateway
68-
- name: Enable the bundling feature on a connected kubernetes cluster.
69-
text: az connectedk8s update -g resourceGroupName -n connectedClusterName --auto-upgrade false --config extensionSets.versionManagedExtensions='enabled'
70-
- name: Disable the bundling feature on a connected kubernetes cluster.
71-
text: az connectedk8s update -g resourceGroupName -n connectedClusterName --auto-upgrade false --config extensionSets.versionManagedExtensions='disabled'
72-
7364
"""
7465

7566
helps["connectedk8s upgrade"] = """

src/connectedk8s/azext_connectedk8s/custom.py

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from knack.prompting import NoTTYException, prompt_y_n
4949
from kubernetes import client as kube_client
5050
from kubernetes import config
51+
from kubernetes.client.rest import ApiException
5152
from kubernetes.config.kube_config import KubeConfigMerger
5253
from packaging import version
5354

@@ -1538,22 +1539,48 @@ def set_security_profile(enable_workload_identity: bool) -> SecurityProfile:
15381539
return security_profile
15391540

15401541

1541-
def get_k8s_extension_module(module_name):
1542+
def get_installed_bundle_extensions() -> list[str]:
1543+
api_instance = kube_client.CustomObjectsApi()
1544+
15421545
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+
)
15451562

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)
15491572

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",
15561578
)
1579+
raise CLIInternalError(
1580+
f"Failed to get bundle properties from the Arc extension config: {e}"
1581+
)
1582+
1583+
return installed_extensions
15571584

15581585

15591586
def get_bundle_feature_flag_from_arc_agentry_config(
@@ -1755,34 +1782,7 @@ def validate_update_cluster_bundle_feature_flag_value(
17551782
current_bundle_feature_flag_value == "enabled"
17561783
and bundle_feature_flag_value == "disabled"
17571784
):
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()
17861786

17871787
if installed_bundle_extensions:
17881788
err_msg = (
@@ -2395,19 +2395,6 @@ def update_connected_cluster(
23952395
)
23962396
raise InvalidArgumentValueError(err_msg)
23972397

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-
24112398
# Patching the connected cluster ARM resource
24122399
arm_properties_unset = (
24132400
tags is None
@@ -2476,6 +2463,19 @@ def update_connected_cluster(
24762463
# if the user had not logged in.
24772464
kubernetes_version = check_kube_connection()
24782465

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+
24792479
# Install helm client
24802480
helm_client_location = install_helm_client(cmd)
24812481

@@ -2999,7 +2999,7 @@ def upgrade_agents(
29992999
e,
30003000
consts.List_Kubernetes_Namespaced_Pod_Fault_Type,
30013001
"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}': ",
30033003
raise_error=False,
30043004
)
30053005

0 commit comments

Comments
 (0)