2323 use_shared_identity,
2424)
2525from azure.cli.core.azclierror import ClientRequestError
26- from azure.core.exceptions import (HttpResponseError)
2726from azure.cli.testsdk import CliTestError, ScenarioTest, live_only
2827from azure.cli.testsdk.scenario_tests import AllowLargeResponse
2928from knack.util import CLIError
@@ -179,6 +178,21 @@ def _get_asm_upgrade_version(self, resource_group, name):
179178 sorted_upgrades = self._sort_revisions(res["upgrades"])
180179 return sorted_upgrades[0]
181180
181+ def _verify_kubectl_installation(self) -> bool:
182+ """Verify if kubectl is installed and accessible."""
183+ try:
184+ subprocess.run(
185+ ["kubectl", "version", "--client"],
186+ check=True,
187+ stdin=subprocess.DEVNULL,
188+ stdout=subprocess.DEVNULL,
189+ stderr=subprocess.DEVNULL,
190+ shell=False,
191+ )
192+ return True
193+ except subprocess.CalledProcessError:
194+ return False
195+
182196 @classmethod
183197 def generate_ssh_keys(cls):
184198 # If the `--ssh-key-value` option is not specified, the validator will try to read the ssh-key from the "~/.ssh" directory,
@@ -10948,11 +10962,12 @@ def test_aks_kollect_with_managed_aad(
1094810962 sp_oid = self._get_test_identity_object_id()
1094910963 print(f"objectid of service principal is {sp_oid}")
1095010964
10951- # Install kubectl (for setting up service principal permissions, and required by the 'kollect' command).
10952- try:
10953- subprocess.call(["az", "aks", "install-cli"])
10954- except subprocess.CalledProcessError as err:
10955- raise CliTestError(f"Failed to install kubectl with error: '{err}'")
10965+ if not self._verify_kubectl_installation():
10966+ # Install kubectl (for setting up service principal permissions, and required by the 'kollect' command).
10967+ try:
10968+ subprocess.call(["az", "aks", "install-cli"])
10969+ except subprocess.CalledProcessError as err:
10970+ raise CliTestError(f"Failed to install kubectl with error: '{err}'")
1095610971
1095710972 # Grant the service principal cluster-admin access using the admin account
1095810973 # (it'd be nice if `az aks command invoke` had an --admin option, but it appears not to, so we have to download admin credentials)
@@ -12679,7 +12694,7 @@ def test_aks_azure_service_mesh_with_egress_gateway(
1267912694 try:
1268012695 subprocess.call(["az", "aks", "install-cli"])
1268112696 except subprocess.CalledProcessError as err:
12682- raise CLITestError ("Failed to install kubectl with error: '{}'!".format(err))
12697+ raise CliTestError ("Failed to install kubectl with error: '{}'!".format(err))
1268312698
1268412699 try:
1268512700 # get credential
@@ -12717,7 +12732,7 @@ def test_aks_azure_service_mesh_with_egress_gateway(
1271712732 stderr=subprocess.STDOUT,
1271812733 )
1271912734 if not f"namespace/{istio_egress_namespace} created" in k_create_sgc_namespace_output:
12720- raise CLITestError (f"failed to create istio egress gateway namespace: {istio_egress_namespace}")
12735+ raise CliTestError (f"failed to create istio egress gateway namespace: {istio_egress_namespace}")
1272112736
1272212737 k_create_sgc_command = ["kubectl", "apply", "-f", sgc_browse_path, "--kubeconfig", browse_path]
1272312738 k_create_sgc_output = subprocess.check_output(
@@ -12726,7 +12741,7 @@ def test_aks_azure_service_mesh_with_egress_gateway(
1272612741 stderr=subprocess.STDOUT,
1272712742 )
1272812743 if not f"staticgatewayconfiguration.egressgateway.kubernetes.azure.com/{istio_sgc_name} created" in k_create_sgc_output:
12729- raise CLITestError ("failed to create StaticGatewayConfiguration")
12744+ raise CliTestError ("failed to create StaticGatewayConfiguration")
1273012745 finally:
1273112746 # Delete files
1273212747 if os.path.exists(browse_path):
@@ -16381,16 +16396,18 @@ def test_aks_bastion(self, resource_group, resource_group_location):
1638116396 f"--vnet-name {vnet_name} --enable-tunneling"
1638216397 self.cmd(create_bastion_cmd, checks=[self.check("provisioningState", "Succeeded")])
1638316398
16384- # install kubectl
16385- _, ctl_temp_file = tempfile.mkstemp()
16386- _, login_temp_file = tempfile.mkstemp()
16387- version = "latest"
16388- install_cmd = 'aks install-cli --client-version={} --install-location={} --base-src-url={} ' \
16389- '--kubelogin-version={} --kubelogin-install-location={} --kubelogin-base-src-url={}'.format(version, ctl_temp_file, "", version, login_temp_file, "")
16390- self.cmd(install_cmd, checks=[self.is_empty()])
16399+ kubectl_path = "kubectl"
16400+ if not self._verify_kubectl_installation():
16401+ # install kubectl
16402+ _, kubectl_path = tempfile.mkstemp()
16403+ _, login_temp_file = tempfile.mkstemp()
16404+ version = "latest"
16405+ install_cmd = 'aks install-cli --client-version={} --install-location={} --base-src-url={} ' \
16406+ '--kubelogin-version={} --kubelogin-install-location={} --kubelogin-base-src-url={}'.format(version, kubectl_path, "", version, login_temp_file, "")
16407+ self.cmd(install_cmd, checks=[self.is_empty()])
1639116408
1639216409 # test bastion connectivity
16393- os.environ["AKS_BASTION_TEST_HOOK"] = ctl_temp_file
16410+ os.environ["AKS_BASTION_TEST_HOOK"] = kubectl_path
1639416411 bastion_cmd = f"aks bastion -g {resource_group} -n {aks_name}"
1639516412 self.cmd(bastion_cmd, checks=[self.is_empty()])
1639616413
0 commit comments