Jelajahi Sumber

health checks: factor out get_required_version

Luke Meyer 7 tahun lalu
induk
melakukan
b4b5b68558

+ 18 - 0
roles/openshift_health_checker/openshift_checks/__init__.py

@@ -325,6 +325,24 @@ class OpenShiftCheck(object):
 
         return tuple(int(x) for x in components[:2])
 
+    def get_required_version(self, name, version_map):
+        """Return the correct required version(s) for the current (or nearest) OpenShift version."""
+        openshift_version = self.get_major_minor_version()
+
+        earliest = min(version_map)
+        latest = max(version_map)
+        if openshift_version < earliest:
+            return version_map[earliest]
+        if openshift_version > latest:
+            return version_map[latest]
+
+        required_version = version_map.get(openshift_version)
+        if not required_version:
+            msg = "There is no recommended version of {} for the current version of OpenShift ({})"
+            raise OpenShiftCheckException(msg.format(name, ".".join(str(comp) for comp in openshift_version)))
+
+        return required_version
+
     def find_ansible_mount(self, path):
         """Return the mount point for path from ansible_mounts."""
 

+ 7 - 18
roles/openshift_health_checker/openshift_checks/ovs_version.py

@@ -3,7 +3,7 @@ Ansible module for determining if an installed version of Open vSwitch is incomp
 currently installed version of OpenShift.
 """
 
-from openshift_checks import OpenShiftCheck, OpenShiftCheckException
+from openshift_checks import OpenShiftCheck
 from openshift_checks.mixins import NotContainerizedMixin
 
 
@@ -16,10 +16,10 @@ class OvsVersion(NotContainerizedMixin, OpenShiftCheck):
     tags = ["health"]
 
     openshift_to_ovs_version = {
-        "3.7": ["2.6", "2.7", "2.8"],
-        "3.6": ["2.6", "2.7", "2.8"],
-        "3.5": ["2.6", "2.7"],
-        "3.4": "2.4",
+        (3, 4): "2.4",
+        (3, 5): ["2.6", "2.7"],
+        (3, 6): ["2.6", "2.7", "2.8"],
+        (3, 7): ["2.6", "2.7", "2.8"],
     }
 
     def is_active(self):
@@ -40,16 +40,5 @@ class OvsVersion(NotContainerizedMixin, OpenShiftCheck):
         return self.execute_module("rpm_version", args)
 
     def get_required_ovs_version(self):
-        """Return the correct Open vSwitch version for the current OpenShift version"""
-        openshift_version_tuple = self.get_major_minor_version(self.get_var("openshift_image_tag"))
-
-        if openshift_version_tuple < (3, 5):
-            return self.openshift_to_ovs_version["3.4"]
-
-        openshift_version = ".".join(str(x) for x in openshift_version_tuple)
-        ovs_version = self.openshift_to_ovs_version.get(openshift_version)
-        if ovs_version:
-            return self.openshift_to_ovs_version[openshift_version]
-
-        msg = "There is no recommended version of Open vSwitch for the current version of OpenShift: {}"
-        raise OpenShiftCheckException(msg.format(openshift_version))
+        """Return the correct Open vSwitch version(s) for the current OpenShift version."""
+        return self.get_required_version("Open vSwitch", self.openshift_to_ovs_version)

+ 3 - 31
roles/openshift_health_checker/openshift_checks/package_version.py

@@ -1,6 +1,6 @@
 """Check that available RPM packages match the required versions."""
 
-from openshift_checks import OpenShiftCheck, OpenShiftCheckException
+from openshift_checks import OpenShiftCheck
 from openshift_checks.mixins import NotContainerizedMixin
 
 
@@ -76,36 +76,8 @@ class PackageVersion(NotContainerizedMixin, OpenShiftCheck):
 
     def get_required_ovs_version(self):
         """Return the correct Open vSwitch version(s) for the current OpenShift version."""
-        openshift_version = self.get_major_minor_version()
-
-        earliest = min(self.openshift_to_ovs_version)
-        latest = max(self.openshift_to_ovs_version)
-        if openshift_version < earliest:
-            return self.openshift_to_ovs_version[earliest]
-        if openshift_version > latest:
-            return self.openshift_to_ovs_version[latest]
-
-        ovs_version = self.openshift_to_ovs_version.get(openshift_version)
-        if not ovs_version:
-            msg = "There is no recommended version of Open vSwitch for the current version of OpenShift: {}"
-            raise OpenShiftCheckException(msg.format(".".join(str(comp) for comp in openshift_version)))
-
-        return ovs_version
+        return self.get_required_version("Open vSwitch", self.openshift_to_ovs_version)
 
     def get_required_docker_version(self):
         """Return the correct Docker version(s) for the current OpenShift version."""
-        openshift_version = self.get_major_minor_version()
-
-        earliest = min(self.openshift_to_docker_version)
-        latest = max(self.openshift_to_docker_version)
-        if openshift_version < earliest:
-            return self.openshift_to_docker_version[earliest]
-        if openshift_version > latest:
-            return self.openshift_to_docker_version[latest]
-
-        docker_version = self.openshift_to_docker_version.get(openshift_version)
-        if not docker_version:
-            msg = "There is no recommended version of Docker for the current version of OpenShift: {}"
-            raise OpenShiftCheckException(msg.format(".".join(str(comp) for comp in openshift_version)))
-
-        return docker_version
+        return self.get_required_version("Docker", self.openshift_to_docker_version)

+ 2 - 21
roles/openshift_health_checker/test/ovs_version_test.py

@@ -1,26 +1,7 @@
 import pytest
 
-from openshift_checks.ovs_version import OvsVersion, OpenShiftCheckException
-
-
-def test_openshift_version_not_supported():
-    def execute_module(*_):
-        return {}
-
-    openshift_release = '111.7.0'
-
-    task_vars = dict(
-        openshift=dict(common=dict()),
-        openshift_release=openshift_release,
-        openshift_image_tag='v' + openshift_release,
-        openshift_deployment_type='origin',
-        openshift_service_type='origin'
-    )
-
-    with pytest.raises(OpenShiftCheckException) as excinfo:
-        OvsVersion(execute_module, task_vars).run()
-
-    assert "no recommended version of Open vSwitch" in str(excinfo.value)
+from openshift_checks.ovs_version import OvsVersion
+from openshift_checks import OpenShiftCheckException
 
 
 def test_invalid_openshift_release_format():

+ 2 - 1
roles/openshift_health_checker/test/package_version_test.py

@@ -1,6 +1,7 @@
 import pytest
 
-from openshift_checks.package_version import PackageVersion, OpenShiftCheckException
+from openshift_checks.package_version import PackageVersion
+from openshift_checks import OpenShiftCheckException
 
 
 def task_vars_for(openshift_release, deployment_type):