diff --git a/kubemarine.spec b/kubemarine.spec index 8bfc50023..4df14619d 100644 --- a/kubemarine.spec +++ b/kubemarine.spec @@ -12,6 +12,7 @@ a = Analysis(['./kubemarine/__main__.py'], 'kubemarine.procedures.add_node', 'kubemarine.procedures.check_iaas', 'kubemarine.procedures.check_paas', + 'kubemarine.procedures.config', 'kubemarine.procedures.do', 'kubemarine.procedures.install', 'kubemarine.procedures.migrate_kubemarine', diff --git a/kubemarine/__main__.py b/kubemarine/__main__.py index 04ef0cd65..4459c508f 100755 --- a/kubemarine/__main__.py +++ b/kubemarine/__main__.py @@ -115,6 +115,10 @@ 'description': "Print current release version", 'group': 'other' }, + 'config': { + 'description': "Print supported configurations of 3rd-parties with versions compatibility", + 'group': 'other' + }, 'do': { 'description': "Execute shell command on cluster nodes", 'group': 'other' @@ -198,7 +202,7 @@ def selftest() -> None: if "main" not in dir(module): raise Exception("No main method in %s" % procedure) - if procedure not in ["do", "migrate_kubemarine"]: + if procedure not in ["do", "migrate_kubemarine", "config"]: if "tasks" not in dir(module): raise Exception("Tasks tree is not presented in %s" % procedure) if not isinstance(module.tasks, OrderedDict): diff --git a/kubemarine/procedures/config.py b/kubemarine/procedures/config.py new file mode 100644 index 000000000..299f8f15f --- /dev/null +++ b/kubemarine/procedures/config.py @@ -0,0 +1,70 @@ +import argparse +import json +import sys +from typing import List + +import yaml + +from kubemarine import plugins +from kubemarine.core import static, utils + +HELP_DESCRIPTION = """\ +Print supported configurations of 3rd-parties with versions compatibility. + +How to use: + +""" + + +def make_config() -> dict: + kubernetes_versions: dict = {} + for version, compatibility_map in static.KUBERNETES_VERSIONS['compatibility_map'].items(): + kubernetes_version = kubernetes_versions.setdefault(version, {}) + + minor_version = utils.minor_version(version) + supported = static.KUBERNETES_VERSIONS['kubernetes_versions'][minor_version] + kubernetes_version['supported'] = bool(supported) + + plugins_ = plugins.oob_plugins + thirdparties_ = ['crictl'] + + for software, software_version in compatibility_map.items(): + if software in plugins_: + kubernetes_version.setdefault('plugins', {}) \ + .setdefault(software, {})['version'] = software_version + + if software in thirdparties_: + kubernetes_version.setdefault('thirdparties', {}) \ + .setdefault(software, {})['version'] = software_version + + return {'kubernetes': kubernetes_versions} + + +def print_config(cfg: dict, arguments: dict) -> None: + format_ = arguments['output'] + if format_ == 'yaml': + print(yaml.safe_dump(cfg, sort_keys=False), end='') + elif format_ == 'json': + print(json.dumps(cfg, indent=4)) + + +def main(cli_arguments: List[str] = None) -> None: + if cli_arguments is None: + cli_arguments = sys.argv[1:] + + parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, + prog='config', + description=HELP_DESCRIPTION) + + parser.add_argument('-o', '--output', + choices=['yaml', 'json'], default='yaml', + help='output format') + + arguments = vars(parser.parse_args(cli_arguments)) + + cfg = make_config() + print_config(cfg, arguments) + + +if __name__ == '__main__': + main() diff --git a/kubemarine/procedures/do.py b/kubemarine/procedures/do.py index 34fe1c37b..26eb77ca2 100755 --- a/kubemarine/procedures/do.py +++ b/kubemarine/procedures/do.py @@ -58,7 +58,7 @@ def run(self, res: DynamicResources) -> None: def main(cli_arguments: List[str] = None) -> None: - if not cli_arguments: + if cli_arguments is None: cli_arguments = sys.argv[1:] if '--' in cli_arguments: diff --git a/test/unit/test_config.py b/test/unit/test_config.py new file mode 100644 index 000000000..f05b323a1 --- /dev/null +++ b/test/unit/test_config.py @@ -0,0 +1,15 @@ +import unittest + +from kubemarine.core import static +from kubemarine.procedures import config + + +class ProcedureConfigTest(unittest.TestCase): + def test_make_config(self): + """Does basic smoke test that kubemarine.procedures.config.make_config() exits successfully""" + cfg = config.make_config() + self.assertEqual(list(static.KUBERNETES_VERSIONS['compatibility_map']), list(cfg['kubernetes'])) + + +if __name__ == '__main__': + unittest.main()