Skip to content
4 changes: 4 additions & 0 deletions src/connectedk8s/azext_connectedk8s/_precheckutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def executing_cluster_diagnostic_checks_job(
kube_config,
kube_context,
helm_client_location,
mcr_url,
)

# Watching for cluster diagnostic checks container to reach in completed stage
Expand Down Expand Up @@ -420,6 +421,7 @@ def helm_install_release_cluster_diagnostic_checks(
kube_config: str | None,
kube_context: str | None,
helm_client_location: str,
mcr_url: str,
onboarding_timeout: str = "60",
) -> None:
cmd_helm_install = [
Expand All @@ -437,6 +439,8 @@ def helm_install_release_cluster_diagnostic_checks(
# To set some other helm parameters through file
cmd_helm_install.extend(["--set", f"global.location={location}"])
cmd_helm_install.extend(["--set", f"global.azureCloud={azure_cloud}"])
cmd_helm_install.extend(["--set", f"global.mcrRepository={mcr_url}"])
cmd_helm_install.extend(["--set", f"global.image.registry={mcr_url}"])
if https_proxy:
cmd_helm_install.extend(["--set", f"global.httpsProxy={https_proxy}"])
if http_proxy:
Expand Down
10 changes: 7 additions & 3 deletions src/connectedk8s/azext_connectedk8s/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ def get_mcr_path(active_directory_endpoint: str) -> str:
mcr_postfix = "com"
# special cases for USSec, exclude part of suffix
if len(active_directory_array) == 4 and active_directory_array[2] == "microsoft":
mcr_postfix = active_directory_array[3]
mcr_postfix = active_directory_array[3].strip("/")
# special case for USNat
elif len(active_directory_array) == 5:
mcr_postfix = (
active_directory_array[2]
+ "."
+ active_directory_array[3]
+ "."
+ active_directory_array[4]
+ active_directory_array[4].strip("/")
)

mcr_url = f"mcr.microsoft.{mcr_postfix}"
Expand Down Expand Up @@ -1887,6 +1887,8 @@ def add_agc_endpoint_overrides(
arm_metadata_endpoint_array[2] + "." + arm_metadata_endpoint_array[3]
)
if cloud_name.lower() == "usnat":
if len(arm_metadata_endpoint_array) < 5:
raise CLIInternalError("Unexpected loginEndpoint format for AGC")
cloud_suffix = (
arm_metadata_endpoint_array[2]
+ "."
Expand All @@ -1905,7 +1907,9 @@ def add_agc_endpoint_overrides(
"--set",
f"systemDefaultValues.azureArcAgents.config_dp_endpoint_override=https://{location}.dp.kubernetesconfiguration.azure.{endpoint_suffix}",
"--set",
f"systemDefaultValues.clusterconnect-agent.notification_dp_endpoint_override=https://guestnotificationservice.azure.{endpoint_suffix}",
f"systemDefaultValues.clusterconnect-agent.connect_dp_endpoint_override=https://{location}.dp.kubernetesconfiguration.azure.{endpoint_suffix}",
"--set",
f"systemDefaultValues.clusterconnect-agent.notification_dp_endpoint_override=https://guestnotificationservice.azure.{endpoint_suffix}/",
"--set",
f"systemDefaultValues.clusterconnect-agent.relay_endpoint_suffix_override=.servicebus.cloudapi.{endpoint_suffix}",
"--set",
Expand Down
91 changes: 67 additions & 24 deletions src/connectedk8s/azext_connectedk8s/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ def create_connectedk8s(

# Install kubectl and helm
try:
kubectl_client_location = install_kubectl_client()
helm_client_location = install_helm_client(cmd)
logger.debug("Using helm binary: %s", helm_client_location)
kubectl_client_location = get_kubectl_client_location(
cmd, azure_cloud=azure_cloud
)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)
except Exception as e:
raise CLIInternalError(
f"An exception has occured while trying to perform kubectl or helm install: {e}"
Expand Down Expand Up @@ -2034,7 +2035,7 @@ def delete_connectedk8s(
raise InvalidArgumentValueError(err_msg)

# Send cloud information to telemetry
send_cloud_telemetry(cmd)
azure_cloud = send_cloud_telemetry(cmd)

# Setting kubeconfig
kube_config = set_kube_config(kube_config)
Expand All @@ -2047,8 +2048,7 @@ def delete_connectedk8s(
# AKS clusters if the user had not logged in.
check_kube_connection()

# Install helm client
helm_client_location = install_helm_client(cmd)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)

# Check Release Existance
release_namespace = utils.get_release_namespace(
Expand All @@ -2063,7 +2063,9 @@ def delete_connectedk8s(
# Check forced delete flag
if force_delete:
print(f"Step: {utils.get_utctimestring()}: Performing Force Delete")
kubectl_client_location = install_kubectl_client()
kubectl_client_location = get_kubectl_client_location(
cmd, azure_cloud=azure_cloud
)

delete_cc_resource(
client, resource_group_name, cluster_name, no_wait, force=force_delete
Expand Down Expand Up @@ -2314,7 +2316,7 @@ def update_connected_cluster(
utils.user_confirmation(confirmation_message, yes)

# Send cloud information to telemetry
send_cloud_telemetry(cmd)
azure_cloud = send_cloud_telemetry(cmd)

# Setting kubeconfig
kube_config = set_kube_config(kube_config)
Expand Down Expand Up @@ -2439,8 +2441,7 @@ def update_connected_cluster(
# if the user had not logged in.
kubernetes_version = check_kube_connection()

# Install helm client
helm_client_location = install_helm_client(cmd)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)

release_namespace = validate_release_namespace(
client,
Expand Down Expand Up @@ -2684,7 +2685,7 @@ def upgrade_agents(
logger.warning("This operation might take a while...\n")

# Send cloud information to telemetry
send_cloud_telemetry(cmd)
azure_cloud = send_cloud_telemetry(cmd)

# Setting kubeconfig
kube_config = set_kube_config(kube_config)
Expand All @@ -2702,8 +2703,7 @@ def upgrade_agents(

api_instance = kube_client.CoreV1Api()

# Install helm client
helm_client_location = install_helm_client(cmd)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)

# Check Release Existence
release_namespace = utils.get_release_namespace(
Expand Down Expand Up @@ -3184,7 +3184,7 @@ def enable_features(
raise ClientRequestError("Failed to enable 'custom-locations' feature.")

# Send cloud information to telemetry
send_cloud_telemetry(cmd)
azure_cloud = send_cloud_telemetry(cmd)

# Setting kubeconfig
kube_config = set_kube_config(kube_config)
Expand All @@ -3200,8 +3200,7 @@ def enable_features(
# if the user had not logged in.
kubernetes_version = check_kube_connection()

# Install helm client
helm_client_location = install_helm_client(cmd)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)

release_namespace = validate_release_namespace(
client,
Expand Down Expand Up @@ -3378,7 +3377,7 @@ def disable_features(
)

# Send cloud information to telemetry
send_cloud_telemetry(cmd)
azure_cloud = send_cloud_telemetry(cmd)

# Setting kubeconfig
kube_config = set_kube_config(kube_config)
Expand All @@ -3394,8 +3393,7 @@ def disable_features(
# if the user had not logged in.
kubernetes_version = check_kube_connection()

# Install helm client
helm_client_location = install_helm_client(cmd)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)

release_namespace = validate_release_namespace(
client,
Expand Down Expand Up @@ -4294,11 +4292,11 @@ def troubleshoot(
# Loading the kubeconfig file in kubernetes client configuration
load_kube_config(kube_config, kube_context, skip_ssl_verification)

# Install helm client
helm_client_location = install_helm_client(cmd)

# Install kubectl client
kubectl_client_location = install_kubectl_client()
azure_cloud = send_cloud_telemetry(cmd)
kubectl_client_location = get_kubectl_client_location(
cmd, azure_cloud=azure_cloud
)
helm_client_location = get_helm_client_location(cmd, azure_cloud=azure_cloud)
release_namespace = validate_release_namespace(
client,
cluster_name,
Expand Down Expand Up @@ -4850,3 +4848,48 @@ def add_config_protected_settings(
configuration_protected_settings,
redacted_protected_values,
)


def _is_agc_cloud(azure_cloud: str) -> bool:
cloud_name = azure_cloud.lower()
return cloud_name == "ussec" or cloud_name == "usnat"


def get_helm_client_location(
cmd: CLICommand,
azure_cloud: str | None = None,
) -> str:
azure_cloud = azure_cloud or send_cloud_telemetry(cmd)

if _is_agc_cloud(azure_cloud):
logger.info("Skipping helm install for AGC. Expecting it to be pre-installed.")
helm_client_location = shutil.which("helm")
if not helm_client_location:
raise CLIInternalError(
"helm not found in PATH for AGC environment. Please install it or add to PATH."
)
return helm_client_location

helm_client_location = install_helm_client(cmd)
logger.debug("Using helm binary: %s", helm_client_location)
return helm_client_location


def get_kubectl_client_location(
cmd: CLICommand,
azure_cloud: str | None = None,
) -> str:
azure_cloud = azure_cloud or send_cloud_telemetry(cmd)

if _is_agc_cloud(azure_cloud):
logger.info(
"Skipping kubectl install for AGC. Expecting it to be pre-installed."
)
kubectl_client_location = shutil.which("kubectl")
if not kubectl_client_location:
raise CLIInternalError(
"kubectl not found in PATH for AGC environment. Please install it or add to PATH."
)
return kubectl_client_location

return install_kubectl_client()
Loading