Skip to content

Commit

Permalink
Merge pull request #149 from borod108/rfe/validate_memory_cnv4615
Browse files Browse the repository at this point in the history
Add check for min memory consistency in generated templates
  • Loading branch information
ksimon1 authored May 20, 2020
2 parents acf1548 + b5a9fb2 commit 06799f1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions travis_ci/test_syntax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ for template in $templates; do
done

python3 travis_ci/check-validations.py
python3 travis_ci/validate-min-memory-consistency.py
75 changes: 75 additions & 0 deletions travis_ci/validate-min-memory-consistency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#! 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 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:
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:
errors.append([newest_os_label, template_name])
except Exception as e:
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)
logging.info("Running minimum memory requirements validation in common templates")

try:
checkMemoryReqs("dist/templates")
except Exception as e:
logging.error(e)
sys.exit(1)

0 comments on commit 06799f1

Please sign in to comment.