From 356fef15f5b51fd322fe6eae0c3584e902f8eb28 Mon Sep 17 00:00:00 2001 From: borod108 Date: Mon, 11 May 2020 14:43:29 +0530 Subject: [PATCH 1/2] Add check for min memory consistency in generated templates Currently the minimum memory requirements set in the "validation" part of the generated template might actually be smaller than the requirements of newest minor version of the os, this script will through an error for such a template. IMHO a better solution should be implemented in the future, but the obvious one - setting the minimal memory requirement to the actual OS requirement will not do since we need to keep naming consistency, i.e. "tiny" template means a specific memory requirement throughout the system. Signed-off-by: borod108 --- .travis.yml | 2 +- travis_ci/test_syntax.sh | 1 + travis_ci/validate-min-memory-consistency.py | 66 ++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 travis_ci/validate-min-memory-consistency.py diff --git a/.travis.yml b/.travis.yml index 8eb23e69..20c066ff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ env: jobs: include: - stage: Tests - name: Check syntax of all templates + name: Check syntax and memory requirements of all templates script: bash -x travis_ci/test_syntax.sh - name: Functional test of the Fedora template diff --git a/travis_ci/test_syntax.sh b/travis_ci/test_syntax.sh index fbbd40c1..adc9538a 100755 --- a/travis_ci/test_syntax.sh +++ b/travis_ci/test_syntax.sh @@ -27,3 +27,4 @@ for template in $templates; do done python3 travis_ci/check-validations.py +python3 travis_ci/validate-min-memory-consistency.py diff --git a/travis_ci/validate-min-memory-consistency.py b/travis_ci/validate-min-memory-consistency.py new file mode 100644 index 00000000..04886eb4 --- /dev/null +++ b/travis_ci/validate-min-memory-consistency.py @@ -0,0 +1,66 @@ +#! python + +import logging +import os +import os.path +import yaml +import sys +import json +import importlib.util + +sys.path.insert(1, 'lookup_plugins/') +import osinfo + +def minMemoryReqForOs(os_label): + latest_os_info = osinfo.LookupModule().run([os_label],[])[0] + return latest_os_info["minimum_resources.architecture=x86_64|all.ram"] + +def newestOsLabel(template): + labels = template["metadata"]["labels"] + osl_pref_str = "os.template.kubevirt.io" + os_labels = [label for label in labels if osl_pref_str in label] + return max(os_labels).split('/')[-1] + +def minMemoryReqInTemplate(template): + object = template["objects"][0] + min_str = object["spec"]["template"]["spec"]["domain"]["resources"]["requests"]["memory"] + min_gi_float = float(min_str.replace("Gi","")) + return int(min_gi_float * (1024**3)) + +def checkMemoryReqs(path): + templates = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] + for template_name in templates: + with open(os.path.join(path, template_name), 'r') as stream: + try: + template = yaml.safe_load(stream) + + if template == None: + logging.info("Empty template file: %s", template) + continue + + logging.info("Checking memory requirements consistency for: {}".format(template["metadata"]["name"])) + + try: + newest_os_label = newestOsLabel(template) + actual_min_req = minMemoryReqForOs(newest_os_label) + min_req_in_template = minMemoryReqInTemplate(template) + if min_req_in_template < actual_min_req: + raise Exception("Memory requirements for OS: {} are not compatible" + " with the requirements set in: {}".format(newest_os_label, template_name)) + + except Exception as e: + logging.info("Minimum memory requirements validation failed") + raise e + + except yaml.YAMLError as exc: + raise exc + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + logging.info("Running minimum memory requirements validation in common templates") + + try: + checkMemoryReqs("dist/templates") + except Exception as e: + logging.error(e) + sys.exit(1) From b5a9fb26ccb891c1c2665294e69849ed32b1fb23 Mon Sep 17 00:00:00 2001 From: borod108 Date: Wed, 20 May 2020 13:46:55 +0530 Subject: [PATCH 2/2] Show all OSes that fail validation at once to save time on reruns Signed-off-by: borod108 --- travis_ci/validate-min-memory-consistency.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/travis_ci/validate-min-memory-consistency.py b/travis_ci/validate-min-memory-consistency.py index 04886eb4..824486e3 100644 --- a/travis_ci/validate-min-memory-consistency.py +++ b/travis_ci/validate-min-memory-consistency.py @@ -27,8 +27,14 @@ def minMemoryReqInTemplate(template): min_gi_float = float(min_str.replace("Gi","")) return int(min_gi_float * (1024**3)) +def memoryReqErrorMessage(newest_os_label, template_name): + return "Memory requirements for OS: {} are not compatible with the requirements set in: {}".format(newest_os_label, template_name) + +FAILED_INFO_MESSAGE = "Minimum memory requirements validation failed" + def checkMemoryReqs(path): templates = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] + errors = [] for template_name in templates: with open(os.path.join(path, template_name), 'r') as stream: try: @@ -45,15 +51,18 @@ def checkMemoryReqs(path): actual_min_req = minMemoryReqForOs(newest_os_label) min_req_in_template = minMemoryReqInTemplate(template) if min_req_in_template < actual_min_req: - raise Exception("Memory requirements for OS: {} are not compatible" - " with the requirements set in: {}".format(newest_os_label, template_name)) - + errors.append([newest_os_label, template_name]) except Exception as e: - logging.info("Minimum memory requirements validation failed") + logging.info(FAILED_INFO_MESSAGE) raise e except yaml.YAMLError as exc: raise exc + if len(errors) > 0: + error_messages = [memoryReqErrorMessage(e[0], e[1]) for e in errors] + error_message = "\n".join(error_messages) + logging.info(FAILED_INFO_MESSAGE) + raise Exception(error_message) if __name__ == "__main__": logging.basicConfig(level=logging.INFO)