blob: ee28e25ead0f8413edfe9289a9925ca6b8663840 [file] [log] [blame]
Alex0989ecf2022-03-29 13:43:21 -05001# Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com)
2# Copyright 2019-2022 Mirantis, Inc.
Alex3ebc5632019-04-18 16:47:18 -05003from cfg_checker.common import logger_cli
Alex205546c2020-12-30 19:22:30 -06004from cfg_checker.modules.network.mapper import SaltNetworkMapper, \
5 KubeNetworkMapper
Alex3ebc5632019-04-18 16:47:18 -05006from cfg_checker.modules.network.network_errors import NetworkErrors
Alex Savatieievf526dc02019-03-06 10:11:32 -06007from cfg_checker.reports import reporter
Alex Savatieiev9b2f6512019-02-20 18:05:00 -06008
9
Alexe0c5b9e2019-04-23 18:51:23 -050010class NetworkChecker(object):
Alex205546c2020-12-30 19:22:30 -060011 def __init__(self):
Alexe0c5b9e2019-04-23 18:51:23 -050012 logger_cli.debug("... init error logs folder")
Alex3ebc5632019-04-18 16:47:18 -050013 self.errors = NetworkErrors()
14
Alex3cdb1bd2021-09-10 15:51:11 -050015 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
Alex1f90e7b2021-09-03 15:31:28 -050081 self.mapper.map_networks()
Alex3cdb1bd2021-09-10 15:51:11 -050082 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
Alex836fac82019-08-22 13:36:16 -050087 if map:
88 self.mapper.print_map()
Alex Savatieiev42b89fa2019-03-07 18:45:26 -060089
Alex3ebc5632019-04-18 16:47:18 -050090 def print_summary(self):
Alex3ebc5632019-04-18 16:47:18 -050091 logger_cli.info(self.errors.get_summary(print_zeros=False))
Alex3ebc5632019-04-18 16:47:18 -050092
93 def print_error_details(self):
94 # Detailed errors
Alexb151fbe2019-04-22 16:53:30 -050095 logger_cli.info(
96 "\n{}\n".format(
97 self.errors.get_errors()
98 )
99 )
Alex3ebc5632019-04-18 16:47:18 -0500100
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600101 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(
Alex205546c2020-12-30 19:22:30 -0600109 reporter.HTMLNetworkReport(self.mapper.master),
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600110 filename
111 )
Alex9a4ad212020-10-01 18:04:25 -0500112 _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 Savatieiev9b2f6512019-02-20 18:05:00 -0600121 logger_cli.info("-> Done")
Alex205546c2020-12-30 19:22:30 -0600122
123
124class 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
Alex359e5752021-08-16 17:28:30 -0500139
Alex205546c2020-12-30 19:22:30 -0600140class KubeNetworkChecker(NetworkChecker):
141 def __init__(
142 self,
143 config,
144 skip_list=None,
145 skip_list_file=None
146 ):
Alexc4f59622021-08-27 13:42:00 -0500147 super(KubeNetworkChecker, self).__init__()
Alex205546c2020-12-30 19:22:30 -0600148 self.mapper = KubeNetworkMapper(
149 config,
150 errors_class=self.errors,
151 skip_list=skip_list,
152 skip_list_file=skip_list_file
Alexe8643642021-08-23 14:08:46 -0500153 )