From bc48fdb320fb8f729a07bc2b43bd4ced755d188c Mon Sep 17 00:00:00 2001 From: Niranjan Shankar Date: Mon, 26 Jul 2021 20:04:51 -0400 Subject: [PATCH] Readd osm-arc distro validation (#62) * Add distro validation for osm-arc removed release-train logic * Readd osm_arc distro validation * Fix style * Rm space * Edit test * Fixed tests and error logic * Remove dependency * Add delete method Co-authored-by: Jonathan Innis --- .../partner_extensions/OpenServiceMesh.py | 83 +++++++++---------- .../tests/latest/test_open_service_mesh.py | 13 ++- 2 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/OpenServiceMesh.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/OpenServiceMesh.py index 048dfe58637..ff65193b3dd 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/OpenServiceMesh.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/OpenServiceMesh.py @@ -14,6 +14,7 @@ from packaging import version import yaml +import requests from ..partner_extensions import PartnerExtensionModel @@ -32,8 +33,6 @@ class OpenServiceMesh(PartnerExtensionModel): - CHART_NAME = "osm-arc" - CHART_LOCATION = "https://azure.github.io/osm-azure" def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_type, extension_type, scope, auto_upgrade_minor_version, release_train, version, target_namespace, @@ -67,7 +66,7 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t # NOTE-2: Return a valid ExtensionInstance object, Instance name and flag for Identity create_identity = False - # _validate_tested_distro(cmd, resource_group_name, cluster_name, version) + _validate_tested_distro(cmd, resource_group_name, cluster_name, version) extension_instance = ExtensionInstance( extension_type=extension_type, @@ -105,58 +104,52 @@ def Delete(self, client, resource_group_name, cluster_name, name, cluster_type): pass -# def _validate_tested_distro(cmd, cluster_resource_group_name, cluster_name, extension_version): +def _validate_tested_distro(cmd, cluster_resource_group_name, cluster_name, extension_version): -# field_unavailable_error = '\"testedDistros\" field unavailable for version {0} of microsoft.openservicemesh, ' \ -# 'cannot determine if this Kubernetes distribution has been properly tested'.format(extension_version) + field_unavailable_error = '\"testedDistros\" field unavailable for version {0} of microsoft.openservicemesh, ' \ + 'cannot determine if this Kubernetes distribution has been properly tested'.format(extension_version) -# if version.parse(str(extension_version)) <= version.parse("0.8.3"): -# logger.warning(field_unavailable_error) -# return + if version.parse(str(extension_version)) <= version.parse("0.8.3"): + logger.warning(field_unavailable_error) + return -# subscription_id = get_subscription_id(cmd.cli_ctx) -# resources = cf_resources(cmd.cli_ctx, subscription_id) + subscription_id = get_subscription_id(cmd.cli_ctx) + resources = cf_resources(cmd.cli_ctx, subscription_id) -# cluster_resource_id = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Kubernetes' \ -# '/connectedClusters/{2}'.format(subscription_id, cluster_resource_group_name, cluster_name) + cluster_resource_id = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Kubernetes' \ + '/connectedClusters/{2}'.format(subscription_id, cluster_resource_group_name, cluster_name) -# resource = resources.get_by_id(cluster_resource_id, '2020-01-01-preview') -# cluster_distro = resource.properties['distribution'].lower() + resource = resources.get_by_id(cluster_resource_id, '2020-01-01-preview') + cluster_distro = resource.properties['distribution'].lower() -# if cluster_distro == "general": -# logger.warning('Unable to determine if distro has been tested for microsoft.openservicemesh, ' -# 'kubernetes distro: \"general\"') -# return + if cluster_distro == "general": + logger.warning('Unable to determine if distro has been tested for microsoft.openservicemesh, ' + 'kubernetes distro: \"general\"') + return -# tested_distros = _get_tested_distros(extension_version) + tested_distros = _get_tested_distros(extension_version) -# if tested_distros is None: -# logger.warning(field_unavailable_error) -# elif cluster_distro not in tested_distros.split(): -# logger.warning('Untested kubernetes distro for microsoft.openservicemesh, Kubernetes distro is %s', -# cluster_distro) + if tested_distros is None: + logger.warning(field_unavailable_error) + elif cluster_distro not in tested_distros.split(): + logger.warning('Untested kubernetes distro for microsoft.openservicemesh, Kubernetes distro is %s', + cluster_distro) -# def _get_tested_distros(chart_version): +def _get_tested_distros(chart_version): -# try: -# chart_arc = ChartBuilder({ -# "name": OpenServiceMesh.CHART_NAME, -# "version": str(chart_version), -# "source": { -# "type": "repo", -# "location": OpenServiceMesh.CHART_LOCATION -# } -# }) -# except VersionError: -# raise InvalidArgumentValueError( -# "Invalid version '{}' for microsoft.openservicemesh".format(chart_version) -# ) + chart_url = 'https://raw.githubusercontent.com/Azure/osm-azure/' \ + 'v{0}/charts/osm-arc/values.yaml'.format(chart_version) + chart_request = requests.get(url=chart_url) -# values = chart_arc.get_values() -# values_yaml = yaml.load(values.raw, Loader=yaml.FullLoader) + if chart_request.status_code == 404: + raise InvalidArgumentValueError( + "Invalid version '{}' for microsoft.openservicemesh".format(chart_version) + ) + + values_yaml = yaml.load(chart_request.text, Loader=yaml.FullLoader) -# try: -# return values_yaml['OpenServiceMesh']['testedDistros'] -# except KeyError: -# return None + try: + return values_yaml['OpenServiceMesh']['testedDistros'] + except KeyError: + return None diff --git a/src/k8s-extension/azext_k8s_extension/tests/latest/test_open_service_mesh.py b/src/k8s-extension/azext_k8s_extension/tests/latest/test_open_service_mesh.py index 61b774045ce..72e94a06831 100644 --- a/src/k8s-extension/azext_k8s_extension/tests/latest/test_open_service_mesh.py +++ b/src/k8s-extension/azext_k8s_extension/tests/latest/test_open_service_mesh.py @@ -9,15 +9,14 @@ import unittest from azure.cli.core.azclierror import InvalidArgumentValueError -# from azext_k8s_extension.partner_extensions.OpenServiceMesh import _get_tested_distros +from azext_k8s_extension.partner_extensions.OpenServiceMesh import _get_tested_distros TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..')) class TestOpenServiceMesh(unittest.TestCase): def test_bad_osm_arc_version(self): - # version = "0.7.1" - # err = "Invalid version \'" + str(version) + "\' for microsoft.openservicemesh" - # with self.assertRaises(InvalidArgumentValueError) as argError: - # _get_tested_distros(version) - # self.assertEqual(str(argError.exception), err) - pass + version = "0.7.1" + err = "Invalid version \'" + str(version) + "\' for microsoft.openservicemesh" + with self.assertRaises(InvalidArgumentValueError) as argError: + _get_tested_distros(version) + self.assertEqual(str(argError.exception), err)