-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from borod108/rfe/validate_memory_cnv4615
Add check for min memory consistency in generated templates
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |