Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 3 | from cfg_checker.common import logger_cli |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 4 | from cfg_checker.modules.network.mapper import SaltNetworkMapper, \ |
| 5 | KubeNetworkMapper |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 6 | from cfg_checker.modules.network.network_errors import NetworkErrors |
Alex Savatieiev | f526dc0 | 2019-03-06 10:11:32 -0600 | [diff] [blame] | 7 | from cfg_checker.reports import reporter |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 8 | |
| 9 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 10 | class NetworkChecker(object): |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 11 | def __init__(self): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 12 | logger_cli.debug("... init error logs folder") |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 13 | self.errors = NetworkErrors() |
| 14 | |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 15 | def _check_duplicate_ips(self): |
| 16 | # shortcuts |
| 17 | logger_cli.debug("... checking for duplicate ips") |
| 18 | _map = self.mapper.map |
| 19 | for _net in _map.keys(): |
| 20 | _ips = set() |
| 21 | for _node_name, _interfaces in _map[_net].items(): |
| 22 | for _if in _interfaces: |
| 23 | if _if["ip_address"] not in _ips: |
| 24 | # there was no such ip yet |
| 25 | _ips.add(_if["ip_address"]) |
| 26 | else: |
| 27 | # this ip already used |
| 28 | logger_cli.warning( |
| 29 | "Warning: Duplicate ip address: " |
| 30 | "'{}: {}' at {} -> {}".format( |
| 31 | _if["interface"], |
| 32 | _if["ip_address"], |
| 33 | _net, |
| 34 | _node_name |
| 35 | ) |
| 36 | ) |
| 37 | self.errors.add_error( |
| 38 | self.errors.NET_DUPLICATE_IP, |
| 39 | network=_net, |
| 40 | node_name=_node_name, |
| 41 | if_name=_if["interface"], |
| 42 | ip_address=_if["ip_address"] |
| 43 | ) |
| 44 | return |
| 45 | |
| 46 | def _check_non_uniform_mtu(self): |
| 47 | # shortcuts |
| 48 | logger_cli.debug("... checking for duplicate ips") |
| 49 | _map = self.mapper.map |
| 50 | for _net in _map.keys(): |
| 51 | _mtus = set() |
| 52 | for _node_name, _interfaces in _map[_net].items(): |
| 53 | for _if in _interfaces: |
| 54 | if len(_mtus) < 1: |
| 55 | # this is the 1st iteration |
| 56 | _mtus.add(_if["rt_mtu"]) |
| 57 | elif _if["rt_mtu"] not in _mtus: |
| 58 | # this ip already used |
| 59 | logger_cli.warning( |
| 60 | "Non-uniform MTU value of '{}' in '{}': " |
| 61 | "{}:{}:{}".format( |
| 62 | _net, |
| 63 | _node_name, |
| 64 | _if["interface"], |
| 65 | _if["ip_address"], |
| 66 | _if["rt_mtu"] |
| 67 | ) |
| 68 | ) |
| 69 | self.errors.add_error( |
| 70 | self.errors.NET_DUPLICATE_IP, |
| 71 | network=_net, |
| 72 | node_name=_node_name, |
| 73 | if_name=_if["interface"], |
| 74 | ip_address=_if["ip_address"], |
| 75 | mtu=_if["rt_mtu"] |
| 76 | ) |
| 77 | return |
| 78 | |
| 79 | def check_networks(self, map=True, skip_keywords=None): |
| 80 | # Load map |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 81 | self.mapper.map_networks() |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 82 | self.mapper.create_map(skip_keywords=skip_keywords) |
| 83 | # Check for errors that is not detectable during mapping |
| 84 | self._check_duplicate_ips() |
| 85 | self._check_non_uniform_mtu() |
| 86 | # print map if requested |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 87 | if map: |
| 88 | self.mapper.print_map() |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 89 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 90 | def print_summary(self): |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 91 | logger_cli.info(self.errors.get_summary(print_zeros=False)) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 92 | |
| 93 | def print_error_details(self): |
| 94 | # Detailed errors |
Alex | b151fbe | 2019-04-22 16:53:30 -0500 | [diff] [blame] | 95 | logger_cli.info( |
| 96 | "\n{}\n".format( |
| 97 | self.errors.get_errors() |
| 98 | ) |
| 99 | ) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 100 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 101 | def create_html_report(self, filename): |
| 102 | """ |
| 103 | Create static html showing network schema-like report |
| 104 | |
| 105 | :return: none |
| 106 | """ |
| 107 | logger_cli.info("### Generating report to '{}'".format(filename)) |
| 108 | _report = reporter.ReportToFile( |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 109 | reporter.HTMLNetworkReport(self.mapper.master), |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 110 | filename |
| 111 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 112 | _report( |
| 113 | { |
| 114 | "domain": self.mapper.domain, |
| 115 | "nodes": self.mapper.nodes, |
| 116 | "map": self.mapper.map, |
| 117 | "mcp_release": self.mapper.cluster['mcp_release'], |
| 118 | "openstack_release": self.mapper.cluster['openstack_release'] |
| 119 | } |
| 120 | ) |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 121 | logger_cli.info("-> Done") |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 122 | |
| 123 | |
| 124 | class SaltNetworkChecker(NetworkChecker): |
| 125 | def __init__( |
| 126 | self, |
| 127 | config, |
| 128 | skip_list=None, |
| 129 | skip_list_file=None |
| 130 | ): |
| 131 | super(SaltNetworkChecker, self).__init__() |
| 132 | self.mapper = SaltNetworkMapper( |
| 133 | config, |
| 134 | errors_class=self.errors, |
| 135 | skip_list=skip_list, |
| 136 | skip_list_file=skip_list_file |
| 137 | ) |
| 138 | |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 139 | |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 140 | class KubeNetworkChecker(NetworkChecker): |
| 141 | def __init__( |
| 142 | self, |
| 143 | config, |
| 144 | skip_list=None, |
| 145 | skip_list_file=None |
| 146 | ): |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 147 | super(KubeNetworkChecker, self).__init__() |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 148 | self.mapper = KubeNetworkMapper( |
| 149 | config, |
| 150 | errors_class=self.errors, |
| 151 | skip_list=skip_list, |
| 152 | skip_list_file=skip_list_file |
Alex | e864364 | 2021-08-23 14:08:46 -0500 | [diff] [blame] | 153 | ) |