Skip to content

Commit 41d6f64

Browse files
change helm package download path to mcr (Azure#32)
1 parent d3443af commit 41d6f64

File tree

3 files changed

+45
-117
lines changed

3 files changed

+45
-117
lines changed

src/connectedk8s/azext_connectedk8s/_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@
482482

483483
# URL constants
484484
CLIENT_PROXY_MCR_TARGET = "mcr.microsoft.com/azureconnectivity/proxy"
485-
HELM_STORAGE_URL = "https://k8connecthelm.azureedge.net"
485+
HELM_MCR_URL = "mcr.microsoft.com/azurearck8s/helm"
486486
HELM_VERSION = "v3.12.2"
487487
Download_And_Install_Kubectl_Fault_Type = "Failed to download and install kubectl"
488488
Azure_Access_Token_Variable = "AZURE_ACCESS_TOKEN"

src/connectedk8s/azext_connectedk8s/custom.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import stat
1717
import tempfile
1818
import time
19-
import urllib.request
2019
from base64 import b64decode, b64encode
2120
from concurrent.futures import ThreadPoolExecutor
2221
from subprocess import DEVNULL, PIPE, Popen
2322
from typing import TYPE_CHECKING, Any, Iterable
2423

24+
import oras.client # type: ignore[import-untyped]
2525
import yaml
2626
from azure.cli.command_modules.role import graph_client_factory
2727
from azure.cli.core import get_default_cli, telemetry
@@ -1170,20 +1170,23 @@ def install_helm_client() -> str:
11701170
telemetry.add_extension_event(
11711171
"connectedk8s", {"Context.Default.AzureCLI.MachineType": machine_type}
11721172
)
1173-
11741173
# Set helm binary download & install locations
11751174
if operating_system == "windows":
1176-
download_location_string = f".azure\\helm\\{consts.HELM_VERSION}\\helm-{consts.HELM_VERSION}-{operating_system}-amd64.zip"
1175+
download_location_string = f".azure\\helm\\{consts.HELM_VERSION}"
1176+
download_file_name = f"helm-{consts.HELM_VERSION}-{operating_system}-amd64.zip"
11771177
install_location_string = (
11781178
f".azure\\helm\\{consts.HELM_VERSION}\\{operating_system}-amd64\\helm.exe"
11791179
)
1180-
requestUri = f"{consts.HELM_STORAGE_URL}/helmsigned/helm-{consts.HELM_VERSION}-{operating_system}-amd64.zip"
1180+
artifactTag = f"helm-{consts.HELM_VERSION}-{operating_system}-amd64"
11811181
elif operating_system == "linux" or operating_system == "darwin":
1182-
download_location_string = f".azure/helm/{consts.HELM_VERSION}/helm-{consts.HELM_VERSION}-{operating_system}-amd64.tar.gz"
1182+
download_location_string = f".azure/helm/{consts.HELM_VERSION}"
1183+
download_file_name = (
1184+
f"helm-{consts.HELM_VERSION}-{operating_system}-amd64.tar.gz"
1185+
)
11831186
install_location_string = (
11841187
f".azure/helm/{consts.HELM_VERSION}/{operating_system}-amd64/helm"
11851188
)
1186-
requestUri = f"{consts.HELM_STORAGE_URL}/helm/helm-{consts.HELM_VERSION}-{operating_system}-amd64.tar.gz"
1189+
artifactTag = f"helm-{consts.HELM_VERSION}-{operating_system}-amd64"
11871190
else:
11881191
telemetry.set_exception(
11891192
exception="Unsupported OS for installing helm client",
@@ -1216,11 +1219,15 @@ def install_helm_client() -> str:
12161219
logger.warning(
12171220
"Downloading helm client for first time. This can take few minutes..."
12181221
)
1222+
client = oras.client.OrasClient()
12191223
retry_count = 3
12201224
retry_delay = 5
12211225
for i in range(retry_count):
12221226
try:
1223-
response = urllib.request.urlopen(requestUri)
1227+
client.pull(
1228+
target=f"{consts.HELM_MCR_URL}:{artifactTag}",
1229+
outdir=download_location,
1230+
)
12241231
break
12251232
except Exception as e:
12261233
if i == retry_count - 1:
@@ -1237,34 +1244,21 @@ def install_helm_client() -> str:
12371244
)
12381245
time.sleep(retry_delay)
12391246

1240-
responseContent = response.read()
1241-
response.close()
1242-
1243-
# Creating the compressed helm binaries
1244-
try:
1245-
with open(download_location, "wb") as f:
1246-
f.write(responseContent)
1247-
except Exception as e:
1248-
telemetry.set_exception(
1249-
exception=e,
1250-
fault_type=consts.Create_HelmExe_Fault_Type,
1251-
summary="Unable to create helm executable",
1252-
)
1253-
reco_str = f"Please ensure that you delete the directory '{download_dir}' before trying again."
1254-
raise ClientRequestError(
1255-
"Failed to create helm executable." + str(e), recommendation=reco_str
1256-
)
12571247
# Extract the archive.
12581248
try:
1259-
shutil.unpack_archive(download_location, download_dir)
1249+
extract_dir = download_location
1250+
download_location = os.path.expanduser(
1251+
os.path.join(download_location, download_file_name)
1252+
)
1253+
shutil.unpack_archive(download_location, extract_dir)
12601254
os.chmod(install_location, os.stat(install_location).st_mode | stat.S_IXUSR)
12611255
except Exception as e:
12621256
telemetry.set_exception(
12631257
exception=e,
12641258
fault_type=consts.Extract_HelmExe_Fault_Type,
12651259
summary="Unable to extract helm executable",
12661260
)
1267-
reco_str = f"Please ensure that you delete the directory '{download_dir}' before trying again."
1261+
reco_str = f"Please ensure that you delete the directory '{extract_dir}' before trying again."
12681262
raise ClientRequestError(
12691263
"Failed to extract helm executable." + str(e), recommendation=reco_str
12701264
)

src/connectedk8s/azext_connectedk8s/tests/latest/test_connectedk8s_scenario.py

Lines changed: 24 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import stat
1313
import subprocess
1414
import time
15-
import urllib.request
1615
from subprocess import PIPE
1716

17+
import oras.client # type: ignore[import-untyped]
1818
import psutil
1919
import requests
2020
from azure.cli.core import get_default_cli
@@ -71,23 +71,21 @@ def install_helm_client():
7171

7272
# Set helm binary download & install locations
7373
if operating_system == "windows":
74-
download_location_string = (
75-
f".azure\\helm\\{consts.HELM_VERSION}\\helm-{consts.HELM_VERSION}-\
76-
{operating_system}-amd64.zip"
77-
)
74+
download_location_string = f".azure\\helm\\{consts.HELM_VERSION}"
75+
download_file_name = f"helm-{consts.HELM_VERSION}-{operating_system}-amd64.zip"
7876
install_location_string = (
7977
f".azure\\helm\\{consts.HELM_VERSION}\\{operating_system}-amd64\\helm.exe"
8078
)
81-
requestUri = f"{consts.HELM_STORAGE_URL}/helm/helm-{consts.HELM_VERSION}-{operating_system}-amd64.zip"
79+
artifactTag = f"helm-{consts.HELM_VERSION}-{operating_system}-amd64"
8280
elif operating_system == "linux" or operating_system == "darwin":
83-
download_location_string = (
84-
f".azure/helm/{consts.HELM_VERSION}/helm-{consts.HELM_VERSION}-\
85-
{operating_system}-amd64.tar.gz"
81+
download_location_string = f".azure/helm/{consts.HELM_VERSION}"
82+
download_file_name = (
83+
f"helm-{consts.HELM_VERSION}-{operating_system}-amd64.tar.gz"
8684
)
8785
install_location_string = (
8886
f".azure/helm/{consts.HELM_VERSION}/{operating_system}-amd64/helm"
8987
)
90-
requestUri = f"{consts.HELM_STORAGE_URL}/helm/helm-{consts.HELM_VERSION}-{operating_system}-amd64.tar.gz"
88+
artifactTag = f"helm-{consts.HELM_VERSION}-{operating_system}-amd64"
9189
else:
9290
logger.warning(
9391
f"The {operating_system} platform is not currently supported for installing helm client."
@@ -98,8 +96,8 @@ def install_helm_client():
9896
download_dir = os.path.dirname(download_location)
9997
install_location = os.path.expanduser(os.path.join("~", install_location_string))
10098

101-
# Download compressed helm binary if not already present
102-
if not os.path.isfile(download_location):
99+
# Download compressed Helm binary if not already present
100+
if not os.path.isfile(install_location):
103101
# Creating the helm folder if it doesnt exist
104102
if not os.path.exists(download_dir):
105103
try:
@@ -109,27 +107,25 @@ def install_helm_client():
109107
return
110108

111109
# Downloading compressed helm client executable
110+
logger.warning(
111+
"Downloading helm client for first time. This can take few minutes..."
112+
)
113+
client = oras.client.OrasClient()
112114
try:
113-
response = urllib.request.urlopen(requestUri)
115+
client.pull(
116+
target=f"{consts.HELM_MCR_URL}:{artifactTag}", outdir=download_location
117+
)
114118
except Exception as e:
115119
logger.warning("Failed to download helm client." + str(e))
116120
return
117121

118-
responseContent = response.read()
119-
response.close()
120-
121-
# Creating the compressed helm binaries
122+
# Extract the archive.
122123
try:
123-
with open(download_location, "wb") as f:
124-
f.write(responseContent)
125-
except Exception as e:
126-
logger.warning("Failed to extract helm executable" + str(e))
127-
return
128-
129-
# Extract compressed helm binary
130-
if not os.path.isfile(install_location):
131-
try:
132-
shutil.unpack_archive(download_location, download_dir)
124+
extract_dir = download_location
125+
download_location = os.path.expanduser(
126+
os.path.join(download_location, download_file_name)
127+
)
128+
shutil.unpack_archive(download_location, extract_dir)
133129
os.chmod(install_location, os.stat(install_location).st_mode | stat.S_IXUSR)
134130
except Exception as e:
135131
logger.warning("Failed to extract helm executable" + str(e))
@@ -220,52 +216,6 @@ def test_connect(self, resource_group):
220216
# delete the kube config
221217
os.remove(_get_test_data_file(managed_cluster_name + "-config.yaml"))
222218

223-
@live_only()
224-
@ResourceGroupPreparer(
225-
name_prefix="conk8stest", location=CONFIG["location"], random_name_length=16
226-
)
227-
def test_connect_withoidcandworkloadidentity(self, resource_group):
228-
managed_cluster_name = self.create_random_name(prefix="test-connect", length=24)
229-
kubeconfig = _get_test_data_file(managed_cluster_name + "-config.yaml")
230-
self.kwargs.update(
231-
{
232-
"rg": resource_group,
233-
"name": self.create_random_name(prefix="cc-", length=12),
234-
"kubeconfig": kubeconfig,
235-
"managed_cluster_name": managed_cluster_name,
236-
"location": CONFIG["location"],
237-
}
238-
)
239-
240-
# scenario - oidc issuer and workload identity enabled
241-
self.cmd("aks create -g {rg} -n {managed_cluster_name} --generate-ssh-keys")
242-
self.cmd(
243-
"aks get-credentials -g {rg} -n {managed_cluster_name} -f {kubeconfig} --admin"
244-
)
245-
self.cmd(
246-
"connectedk8s connect -n {name} -g {rg} -l {location} --tags foo=doo --enable-oidc-issuer \
247-
--enable-workload-identity --kube-config {kubeconfig} --kube-context {managed_cluster_name}-admin"
248-
)
249-
self.cmd(
250-
"connectedk8s show -g {rg} -n {name}",
251-
checks=[
252-
self.check("tags.foo", "doo"),
253-
self.check("name", "{name}"),
254-
self.check("resourceGroup", "{rg}"),
255-
self.check("oidcIssuerProfile.enabled", True),
256-
self.check("securityProfile.workloadIndentity.enabled", True),
257-
],
258-
)
259-
260-
self.cmd(
261-
"connectedk8s delete -g {rg} -n {name} --kube-config {kubeconfig} --kube-context \
262-
{managed_cluster_name}-admin -y"
263-
)
264-
self.cmd("aks delete -g {rg} -n {managed_cluster_name} -y")
265-
266-
# delete the kube config
267-
os.remove(_get_test_data_file(managed_cluster_name + "-config.yaml"))
268-
269219
@live_only()
270220
@ResourceGroupPreparer(
271221
name_prefix="conk8stest", location=CONFIG["location"], random_name_length=16
@@ -470,7 +420,7 @@ def test_enable_disable_features(self, resource_group):
470420
with self.assertRaisesRegex(
471421
CLIError,
472422
"Disabling 'cluster-connect' feature is not allowed when \
473-
'custom-locations' feature is enabled.",
423+
'custom-locations' feature is enabled",
474424
):
475425
self.cmd(
476426
"connectedk8s disable-features -n {name} -g {rg} --features cluster-connect --kube-config \
@@ -847,22 +797,6 @@ def test_update(self, resource_group):
847797
],
848798
)
849799

850-
# scenario - oidc issuer and workload identity enabled
851-
self.cmd(
852-
"connectedk8s update -n {name} -g {rg} --enable-oidc-issuer \
853-
--enable-workload-identity --kube-config {kubeconfig} \
854-
--kube-context {managed_cluster_name}-admin"
855-
)
856-
self.cmd(
857-
"connectedk8s show -g {rg} -n {name}",
858-
checks=[
859-
self.check("name", "{name}"),
860-
self.check("resourceGroup", "{rg}"),
861-
self.check("oidcIssuerProfile.enabled", True),
862-
self.check("securityProfile.workloadIndentity.enabled", True),
863-
],
864-
)
865-
866800
# scenario - oidc issuer enabled and self hosted issuer url set
867801
self.cmd(
868802
'connectedk8s update -n {name} -g {rg} --enable-oidc-issuer \

0 commit comments

Comments
 (0)