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 | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 3 | import ipaddress |
| 4 | import json |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 5 | from copy import deepcopy |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 6 | |
| 7 | from cfg_checker.common import logger_cli |
| 8 | from cfg_checker.common.exception import InvalidReturnException |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 9 | from cfg_checker.common.exception import ConfigException |
| 10 | from cfg_checker.common.exception import KubeException |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 11 | from cfg_checker.modules.network.network_errors import NetworkErrors |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 12 | from cfg_checker.nodes import SaltNodes, KubeNodes |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 13 | |
| 14 | # TODO: use templated approach |
| 15 | # net interface structure should be the same |
| 16 | _if_item = { |
| 17 | "name": "unnamed interface", |
| 18 | "mac": "", |
| 19 | "routes": {}, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 20 | "proto": "", |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 21 | "ip": [], |
| 22 | "parameters": {} |
| 23 | } |
| 24 | |
| 25 | # collection of configurations |
| 26 | _network_item = { |
| 27 | "runtime": {}, |
| 28 | "config": {}, |
| 29 | "reclass": {} |
| 30 | } |
| 31 | |
| 32 | |
| 33 | class NetworkMapper(object): |
| 34 | RECLASS = "reclass" |
| 35 | CONFIG = "config" |
| 36 | RUNTIME = "runtime" |
| 37 | |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 38 | def __init__( |
| 39 | self, |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 40 | config, |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 41 | errors_class=None, |
| 42 | skip_list=None, |
| 43 | skip_list_file=None |
| 44 | ): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 45 | logger_cli.info("# Initializing mapper") |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 46 | self.env_config = config |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 47 | # init networks and nodes |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 48 | self.networks = {} |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 49 | self.nodes = self.master.get_nodes( |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 50 | skip_list=skip_list, |
| 51 | skip_list_file=skip_list_file |
| 52 | ) |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 53 | self.cluster = self.master.get_info() |
| 54 | self.domain = self.master.domain |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 55 | # init and pre-populate interfaces |
| 56 | self.interfaces = {k: {} for k in self.nodes} |
| 57 | # Init errors class |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 58 | if errors_class: |
| 59 | self.errors = errors_class |
| 60 | else: |
| 61 | logger_cli.debug("... init error logs folder") |
| 62 | self.errors = NetworkErrors() |
| 63 | |
| 64 | # adding net data to tree |
| 65 | def _add_data(self, _list, _n, _h, _d): |
| 66 | if _n not in _list: |
| 67 | _list[_n] = {} |
| 68 | _list[_n][_h] = [_d] |
| 69 | elif _h not in _list[_n]: |
| 70 | # there is no such host, just create it |
| 71 | _list[_n][_h] = [_d] |
| 72 | else: |
| 73 | # there is such host... this is an error |
| 74 | self.errors.add_error( |
| 75 | self.errors.NET_DUPLICATE_IF, |
| 76 | host=_h, |
| 77 | dup_if=_d['name'] |
| 78 | ) |
| 79 | _list[_n][_h].append(_d) |
| 80 | |
| 81 | # TODO: refactor map creation. Build one map instead of two separate |
| 82 | def _map_network_for_host(self, host, if_class, net_list, data): |
| 83 | # filter networks for this IF IP |
| 84 | _nets = [n for n in net_list.keys() if if_class.ip in n] |
| 85 | _masks = [n.netmask for n in _nets] |
| 86 | if len(_nets) > 1: |
| 87 | # There a multiple network found for this IP, Error |
| 88 | self.errors.add_error( |
| 89 | self.errors.NET_SUBNET_INTERSECT, |
| 90 | host=host, |
| 91 | ip=str(if_class.exploded), |
| 92 | networks="; ".join([str(_n) for _n in _nets]) |
| 93 | ) |
| 94 | # check mask match |
| 95 | if len(_nets) > 0 and if_class.netmask not in _masks: |
| 96 | self.errors.add_error( |
| 97 | self.errors.NET_MASK_MISMATCH, |
| 98 | host=host, |
| 99 | if_name=data['name'], |
| 100 | if_cidr=if_class.exploded, |
| 101 | if_mapped_networks=", ".join([str(_n) for _n in _nets]) |
| 102 | ) |
| 103 | |
| 104 | if len(_nets) < 1: |
| 105 | self._add_data(net_list, if_class.network, host, data) |
| 106 | else: |
| 107 | # add all data |
| 108 | for net in _nets: |
| 109 | self._add_data(net_list, net, host, data) |
| 110 | |
| 111 | return net_list |
| 112 | |
| 113 | def _map_reclass_networks(self): |
| 114 | # class uses nodes from self.nodes dict |
| 115 | _reclass = {} |
| 116 | # Get required pillars |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 117 | self.master.get_specific_pillar_for_nodes("linux:network") |
| 118 | for node in self.master.nodes.keys(): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 119 | # check if this node |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 120 | if not self.master.is_node_available(node): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 121 | continue |
| 122 | # get the reclass value |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 123 | _pillar = \ |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 124 | self.master.nodes[node]['pillars']['linux']['network'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 125 | # we should be ready if there is no interface in reclass for a node |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 126 | # for example on APT node |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 127 | if 'interface' in _pillar: |
| 128 | _pillar = _pillar['interface'] |
| 129 | else: |
| 130 | logger_cli.info( |
| 131 | "... node '{}' skipped, no IF section in reclass".format( |
| 132 | node |
| 133 | ) |
| 134 | ) |
| 135 | continue |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 136 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 137 | # build map based on IPs and save info too |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 138 | for if_name, _dat in _pillar.items(): |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 139 | # get proper IF name |
| 140 | _if_name = if_name if 'name' not in _dat else _dat['name'] |
| 141 | # place it |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 142 | if _if_name not in self.interfaces[node]: |
| 143 | self.interfaces[node][_if_name] = deepcopy(_network_item) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 144 | self.interfaces[node][_if_name]['reclass'] = deepcopy(_dat) |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 145 | # map network if any |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 146 | if 'address' in _dat: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 147 | _if = ipaddress.IPv4Interface( |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 148 | _dat['address'] + '/' + _dat['netmask'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 149 | ) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 150 | _dat['name'] = _if_name |
| 151 | _dat['ifs'] = [_if] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 152 | |
| 153 | _reclass = self._map_network_for_host( |
| 154 | node, |
| 155 | _if, |
| 156 | _reclass, |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 157 | _dat |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 158 | ) |
| 159 | |
| 160 | return _reclass |
| 161 | |
| 162 | def _map_configured_networks(self): |
| 163 | # class uses nodes from self.nodes dict |
| 164 | _confs = {} |
| 165 | |
Alex | 92e07ce | 2019-05-31 16:00:03 -0500 | [diff] [blame] | 166 | # TODO: parse /etc/network/interfaces |
| 167 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 168 | return _confs |
| 169 | |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 170 | def _map_runtime_networks(self, result): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 171 | # class uses nodes from self.nodes dict |
| 172 | _runtime = {} |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 173 | for key in self.master.nodes.keys(): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 174 | # check if we are to work with this node |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 175 | if not self.master.is_node_available(key): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 176 | continue |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 177 | # due to much data to be passed from master, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 178 | # it is happening in order |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 179 | if key in result: |
| 180 | _text = result[key] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 181 | if '{' in _text and '}' in _text: |
| 182 | _text = _text[_text.find('{'):] |
| 183 | else: |
| 184 | raise InvalidReturnException( |
| 185 | "Non-json object returned: '{}'".format( |
| 186 | _text |
| 187 | ) |
| 188 | ) |
| 189 | _dict = json.loads(_text[_text.find('{'):]) |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 190 | self.master.nodes[key]['routes'] = _dict.pop("routes") |
| 191 | self.master.nodes[key]['networks'] = _dict |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 192 | else: |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 193 | self.master.nodes[key]['networks'] = {} |
| 194 | self.master.nodes[key]['routes'] = {} |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 195 | logger_cli.debug("... {} has {} networks".format( |
| 196 | key, |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 197 | len(self.master.nodes[key]['networks'].keys()) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 198 | )) |
| 199 | logger_cli.info("-> done collecting networks data") |
| 200 | |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 201 | logger_cli.info("-> mapping runtime network IPs") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 202 | # match interfaces by IP subnets |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 203 | for host, node_data in self.master.nodes.items(): |
| 204 | if not self.master.is_node_available(host): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 205 | continue |
| 206 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 207 | for net_name, net_data in node_data['networks'].items(): |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 208 | # cut net name |
| 209 | _i = net_name.find('@') |
| 210 | _name = net_name if _i < 0 else net_name[:_i] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 211 | # get ips and calculate subnets |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 212 | if _name in ['lo']: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 213 | # skip the localhost |
| 214 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 215 | else: |
| 216 | # add collected data to interface storage |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 217 | if _name not in self.interfaces[host]: |
| 218 | self.interfaces[host][_name] = \ |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 219 | deepcopy(_network_item) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 220 | self.interfaces[host][_name]['runtime'] = \ |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 221 | deepcopy(net_data) |
| 222 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 223 | # get data and make sure that wide mask goes first |
| 224 | _ip4s = sorted( |
| 225 | net_data['ipv4'], |
| 226 | key=lambda s: s[s.index('/'):] |
| 227 | ) |
| 228 | for _ip_str in _ip4s: |
| 229 | # create interface class |
| 230 | _if = ipaddress.IPv4Interface(_ip_str) |
| 231 | # check if this is a VIP |
| 232 | # ...all those will have /32 mask |
| 233 | net_data['vip'] = None |
| 234 | if _if.network.prefixlen == 32: |
| 235 | net_data['vip'] = str(_if.exploded) |
| 236 | if 'name' not in net_data: |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 237 | net_data['name'] = _name |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 238 | if 'ifs' not in net_data: |
| 239 | net_data['ifs'] = [_if] |
| 240 | # map it |
| 241 | _runtime = self._map_network_for_host( |
| 242 | host, |
| 243 | _if, |
| 244 | _runtime, |
| 245 | net_data |
| 246 | ) |
| 247 | else: |
| 248 | # data is already there, just add VIP |
| 249 | net_data['ifs'].append(_if) |
| 250 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 251 | def process_interface(lvl, interface, tree, res): |
| 252 | # get childs for each root |
| 253 | # tree row item (<if_name>, [<parents>], [<childs>]) |
Alex | 163aa04 | 2022-12-01 11:58:32 -0600 | [diff] [blame] | 254 | if lvl > 50 or lvl < -50: |
| 255 | logger_cli.warning( |
| 256 | "WARNING: Recoursion depth limit ({}) reached " |
| 257 | "for {}".format( |
| 258 | lvl, |
| 259 | interface |
| 260 | ) |
| 261 | ) |
| 262 | return |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 263 | if lvl not in tree: |
| 264 | # - no level - add it |
| 265 | tree[lvl] = {} |
| 266 | # there is such interface in this level? |
| 267 | if interface not in tree[lvl]: |
| 268 | # - IF not present |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 269 | _n = '' |
| 270 | if interface not in res: |
| 271 | _n = 'unknown IF' |
| 272 | _p = None |
| 273 | _c = None |
| 274 | else: |
| 275 | # -- get parents, add |
| 276 | _p = res[interface]['lower'] |
| 277 | # -- get childs, add |
| 278 | _c = res[interface]['upper'] |
| 279 | |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 280 | # if None, put empty list |
| 281 | _p = _p if _p else [] |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 282 | # if None, put empty list |
| 283 | _c = _c if _c else [] |
| 284 | tree[lvl].update({ |
| 285 | interface: { |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 286 | "note": _n, |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 287 | "parents": _p, |
| 288 | "children": _c, |
| 289 | "size": len(_p) if len(_p) > len(_c) else len(_c) |
| 290 | } |
| 291 | }) |
| 292 | for p_if in tree[lvl][interface]["parents"]: |
| 293 | # -- cycle: execute process for next parent, lvl-1 |
| 294 | process_interface(lvl-1, p_if, tree, res) |
| 295 | for c_if in tree[lvl][interface]["children"]: |
| 296 | # -- cycle: execute process for next child, lvl+1 |
| 297 | process_interface(lvl+1, c_if, tree, res) |
| 298 | else: |
| 299 | # - IF present - exit (been here already) |
| 300 | return |
| 301 | |
| 302 | def _put(cNet, cIndex, _list): |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 303 | _added = False |
| 304 | _actual_index = -1 |
| 305 | # Check list len |
| 306 | _len = len(_list) |
| 307 | if cIndex >= _len: |
| 308 | # grow list to meet index |
| 309 | _list = _list + [''] * (cIndex - _len + 1) |
| 310 | _len = len(_list) |
| 311 | |
| 312 | for _cI in range(cIndex, _len): |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 313 | # add child per index |
| 314 | # if space is free |
| 315 | if not _list[_cI]: |
| 316 | _list[_cI] = cNet |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 317 | _added = True |
| 318 | _actual_index = _cI |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 319 | break |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 320 | if not _added: |
| 321 | # grow list by one entry |
| 322 | _list = _list + [cNet] |
| 323 | _actual_index = len(_list) - 1 |
| 324 | return _actual_index, _list |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 325 | |
| 326 | # build network hierachy |
| 327 | nr = node_data['networks'] |
| 328 | # walk interface tree |
| 329 | for _ifname in node_data['networks']: |
| 330 | _tree = {} |
| 331 | _level = 0 |
| 332 | process_interface(_level, _ifname, _tree, nr) |
| 333 | # save tree for node/if |
| 334 | node_data['networks'][_ifname]['tree'] = _tree |
| 335 | |
| 336 | # debug, print built tree |
| 337 | # logger_cli.debug("# '{}'".format(_ifname)) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 338 | lvls = list(_tree.keys()) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 339 | lvls.sort() |
| 340 | n = len(lvls) |
| 341 | m = max([len(_tree[k].keys()) for k in _tree.keys()]) |
| 342 | matrix = [["" for i in range(m)] for j in range(n)] |
| 343 | x = 0 |
| 344 | while True: |
| 345 | _lv = lvls.pop(0) |
| 346 | # get all interfaces on this level |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 347 | nets = iter(_tree[_lv].keys()) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 348 | while True: |
| 349 | y = 0 |
| 350 | # get next interface |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 351 | try: |
| 352 | _net = next(nets) |
| 353 | except StopIteration: |
| 354 | break |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 355 | # all nets |
| 356 | _a = [_net] |
| 357 | # put current interface if this is only one left |
| 358 | if not _tree[_lv][_net]['children']: |
| 359 | if _net not in matrix[x]: |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 360 | _, matrix[x] = _put( |
| 361 | _net, |
| 362 | y, |
| 363 | matrix[x] |
| 364 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 365 | y += 1 |
| 366 | else: |
| 367 | # get all nets with same child |
| 368 | for _c in _tree[_lv][_net]['children']: |
| 369 | for _o_net in nets: |
| 370 | if _c in _tree[_lv][_o_net]['children']: |
| 371 | _a.append(_o_net) |
| 372 | # flush collected nets |
| 373 | for idx in range(len(_a)): |
| 374 | if _a[idx] in matrix[x]: |
| 375 | # there is such interface on this level |
| 376 | # get index |
| 377 | _nI = matrix[x].index(_a[idx]) |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 378 | _, matrix[x+1] = _put( |
| 379 | _c, |
| 380 | _nI, |
| 381 | matrix[x+1] |
| 382 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 383 | else: |
| 384 | # there is no such interface |
| 385 | # add it |
Alex | f3dbe86 | 2019-10-07 15:17:04 -0500 | [diff] [blame] | 386 | _t, matrix[x] = _put( |
| 387 | _a[idx], |
| 388 | 0, |
| 389 | matrix[x] |
| 390 | ) |
| 391 | # also, put child |
| 392 | _, matrix[x+1] = _put( |
| 393 | _c, |
| 394 | _t, |
| 395 | matrix[x+1] |
| 396 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 397 | # remove collected nets from processing |
| 398 | if _a[idx] in nets: |
| 399 | nets.remove(_a[idx]) |
| 400 | y += len(_a) |
| 401 | if not nets: |
| 402 | x += 1 |
| 403 | break |
| 404 | if not lvls: |
| 405 | break |
| 406 | |
| 407 | lines = [] |
| 408 | _columns = [len(max([i for i in li])) for li in matrix] |
| 409 | for idx_y in range(m): |
| 410 | line = "" |
| 411 | for idx_x in range(n): |
Alex | 9b2c1d1 | 2020-03-19 09:32:35 -0500 | [diff] [blame] | 412 | _len = _columns[idx_x] if _columns[idx_x] else 1 |
| 413 | _fmt = "{" + ":{}".format(_len) + "} " |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 414 | line += _fmt.format(matrix[idx_x][idx_y]) |
| 415 | lines.append(line) |
| 416 | node_data['networks'][_ifname]['matrix'] = matrix |
| 417 | node_data['networks'][_ifname]['lines'] = lines |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 418 | return _runtime |
| 419 | |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 420 | |
| 421 | class SaltNetworkMapper(NetworkMapper): |
| 422 | def __init__( |
| 423 | self, |
| 424 | config, |
| 425 | errors_class=None, |
| 426 | skip_list=None, |
| 427 | skip_list_file=None |
| 428 | ): |
| 429 | self.master = SaltNodes(config) |
| 430 | super(SaltNetworkMapper, self).__init__( |
| 431 | config, |
| 432 | errors_class=errors_class, |
| 433 | skip_list=skip_list, |
| 434 | skip_list_file=skip_list_file |
| 435 | ) |
| 436 | |
| 437 | def get_script_output(self): |
| 438 | """ |
| 439 | Get runtime networks by executing script on nodes |
| 440 | """ |
| 441 | logger_cli.info("# Mapping node runtime network data") |
| 442 | self.master.prepare_script_on_active_nodes("ifs_data.py") |
| 443 | _result = self.master.execute_script_on_active_nodes( |
| 444 | "ifs_data.py", |
| 445 | args="json" |
| 446 | ) |
| 447 | |
| 448 | return _result |
| 449 | |
| 450 | def map_networks(self): |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 451 | logger_cli.info("-> Mapping reclass networks") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 452 | self.map_network(self.RECLASS) |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 453 | logger_cli.info("-> Mapping runtime networks") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 454 | self.map_network(self.RUNTIME) |
| 455 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 456 | def map_network(self, source): |
| 457 | # maps target network using given source |
| 458 | _networks = None |
| 459 | |
| 460 | if source == self.RECLASS: |
| 461 | _networks = self._map_reclass_networks() |
| 462 | elif source == self.CONFIG: |
| 463 | _networks = self._map_configured_networks() |
| 464 | elif source == self.RUNTIME: |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 465 | _r = self.get_script_output() |
| 466 | _networks = self._map_runtime_networks(_r) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 467 | |
| 468 | self.networks[source] = _networks |
| 469 | return _networks |
| 470 | |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 471 | def create_map(self, skip_keywords=None): |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 472 | """Create all needed elements for map output |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 473 | |
| 474 | :return: none |
| 475 | """ |
| 476 | _runtime = self.networks[self.RUNTIME] |
| 477 | _reclass = self.networks[self.RECLASS] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 478 | |
| 479 | # main networks, target vars |
| 480 | _map = {} |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 481 | # No matter of proto, at least one IP will be present for the network |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 482 | # we interested in, since we are to make sure that L3 level |
| 483 | # is configured according to reclass model |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 484 | for network in _reclass: |
| 485 | # shortcuts |
| 486 | _net = str(network) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 487 | _map[_net] = {} |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 488 | if network not in _runtime: |
| 489 | # reclass has network that not found in runtime |
| 490 | self.errors.add_error( |
| 491 | self.errors.NET_NO_RUNTIME_NETWORK, |
| 492 | reclass_net=str(network) |
| 493 | ) |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 494 | logger_cli.warn( |
| 495 | "WARN: {}: {}".format( |
| 496 | " No runtime network ", str(network) |
| 497 | ) |
| 498 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 499 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 500 | # hostnames |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 501 | names = sorted(_runtime[network].keys()) |
| 502 | for hostname in names: |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 503 | _notes = [] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 504 | node = hostname.split('.')[0] |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 505 | if not self.master.is_node_available(hostname, log=False): |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 506 | logger_cli.info( |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 507 | " {0:8} {1}".format(node, "node not available") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 508 | ) |
| 509 | # add non-responsive node erorr |
| 510 | self.errors.add_error( |
| 511 | self.errors.NET_NODE_NON_RESPONSIVE, |
| 512 | host=hostname |
| 513 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 514 | _notes.append( |
| 515 | self.errors.get_error_type_text( |
| 516 | self.errors.NET_NODE_NON_RESPONSIVE |
| 517 | ) |
| 518 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 519 | continue |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 520 | # lookup interface name on node using network CIDR |
| 521 | _if_name = _runtime[network][hostname][0]["name"] |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 522 | _raw = self.interfaces[hostname][_if_name]['runtime'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 523 | # get proper reclass |
| 524 | _r = self.interfaces[hostname][_if_name]['reclass'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 525 | _if_name_suffix = "" |
| 526 | # get the proto value |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 527 | if _r: |
| 528 | _if_rc = "" |
| 529 | else: |
| 530 | self.errors.add_error( |
| 531 | self.errors.NET_NODE_UNEXPECTED_IF, |
| 532 | host=hostname, |
| 533 | if_name=_if_name |
| 534 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 535 | _notes.append( |
| 536 | self.errors.get_error_type_text( |
| 537 | self.errors.NET_NODE_UNEXPECTED_IF |
| 538 | ) |
| 539 | ) |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 540 | _if_rc = "*" |
| 541 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 542 | if "proto" in _r: |
| 543 | _proto = _r['proto'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 544 | else: |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 545 | _proto = "-" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 546 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 547 | if "type" in _r: |
| 548 | _if_name_suffix += _r["type"] |
| 549 | if "use_interfaces" in _r: |
| 550 | _if_name_suffix += "->" + ",".join(_r["use_interfaces"]) |
| 551 | |
| 552 | if _if_name_suffix: |
| 553 | _if_name_suffix = "({})".format(_if_name_suffix) |
| 554 | |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 555 | # get gate and routes if proto is static |
| 556 | if _proto == 'static': |
| 557 | # get the gateway for current net |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 558 | _routes = self.master.nodes[hostname]['routes'] |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 559 | _route = _routes[_net] if _net in _routes else None |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 560 | # get the default gateway |
| 561 | if 'default' in _routes: |
| 562 | _d_gate = ipaddress.IPv4Address( |
| 563 | _routes['default']['gateway'] |
| 564 | ) |
| 565 | else: |
| 566 | _d_gate = None |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 567 | _d_gate_str = str(_d_gate) if _d_gate else "No default!" |
| 568 | # match route with default |
| 569 | if not _route: |
| 570 | _gate = "?" |
| 571 | else: |
| 572 | _gate = _route['gateway'] if _route['gateway'] else "-" |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 573 | else: |
| 574 | # in case of manual and dhcp, no check possible |
| 575 | _gate = "-" |
| 576 | _d_gate = "-" |
Alex | 4067f00 | 2019-06-11 10:47:16 -0500 | [diff] [blame] | 577 | _d_gate_str = "-" |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 578 | # iterate through interfaces |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 579 | _a = _runtime[network][hostname] |
| 580 | for _host in _a: |
| 581 | for _if in _host['ifs']: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 582 | _ip_str = str(_if.exploded) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 583 | _gate_error = "" |
| 584 | _up_error = "" |
| 585 | _mtu_error = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 586 | |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 587 | # Match gateway |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 588 | if _proto == 'static': |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 589 | # default reclass gate |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 590 | _r_gate = "-" |
| 591 | if "gateway" in _r: |
| 592 | _r_gate = _r["gateway"] |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 593 | |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 594 | # if values not match, put * |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 595 | if _gate != _r_gate and _d_gate_str != _r_gate: |
| 596 | # if values not match, check if default match |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 597 | self.errors.add_error( |
| 598 | self.errors.NET_UNEXPECTED_GATEWAY, |
| 599 | host=hostname, |
| 600 | if_name=_if_name, |
| 601 | ip=_ip_str, |
| 602 | gateway=_gate |
| 603 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 604 | _notes.append( |
| 605 | self.errors.get_error_type_text( |
| 606 | self.errors.NET_UNEXPECTED_GATEWAY |
| 607 | ) |
| 608 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 609 | _gate_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 610 | |
| 611 | # IF status in reclass |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 612 | _e = "enabled" |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 613 | if _e not in _r: |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 614 | self.errors.add_error( |
| 615 | self.errors.NET_NO_RC_IF_STATUS, |
| 616 | host=hostname, |
| 617 | if_name=_if_name |
| 618 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 619 | _notes.append( |
| 620 | self.errors.get_error_type_text( |
| 621 | self.errors.NET_NO_RC_IF_STATUS |
| 622 | ) |
| 623 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 624 | _up_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 625 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 626 | _rc_mtu = _r['mtu'] if 'mtu' in _r else None |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 627 | _rc_mtu_s = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 628 | # check if this is a VIP address |
| 629 | # no checks needed if yes. |
| 630 | if _host['vip'] != _ip_str: |
| 631 | if _rc_mtu: |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 632 | _rc_mtu_s = str(_rc_mtu) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 633 | # if there is an MTU value, match it |
| 634 | if _host['mtu'] != _rc_mtu_s: |
| 635 | self.errors.add_error( |
| 636 | self.errors.NET_MTU_MISMATCH, |
| 637 | host=hostname, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 638 | if_name=_if_name, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 639 | if_cidr=_ip_str, |
| 640 | reclass_mtu=_rc_mtu, |
| 641 | runtime_mtu=_host['mtu'] |
| 642 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 643 | _notes.append( |
| 644 | self.errors.get_error_type_text( |
| 645 | self.errors.NET_MTU_MISMATCH |
| 646 | ) |
| 647 | ) |
Alex | b3dc859 | 2019-06-11 13:20:36 -0500 | [diff] [blame] | 648 | _rc_mtu_s = "/" + _rc_mtu_s |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 649 | _mtu_error = "*" |
| 650 | else: |
| 651 | # empty the matched value |
| 652 | _rc_mtu_s = "" |
Alex | 3b8e543 | 2019-06-11 15:21:59 -0500 | [diff] [blame] | 653 | elif _host['mtu'] != '1500' and \ |
| 654 | _proto not in ["-", "dhcp"]: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 655 | # there is no MTU value in reclass |
| 656 | # and runtime value is not default |
| 657 | self.errors.add_error( |
| 658 | self.errors.NET_MTU_EMPTY, |
| 659 | host=hostname, |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 660 | if_name=_if_name, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 661 | if_cidr=_ip_str, |
| 662 | if_mtu=_host['mtu'] |
| 663 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 664 | _notes.append( |
| 665 | self.errors.get_error_type_text( |
| 666 | self.errors.NET_MTU_EMPTY |
| 667 | ) |
| 668 | ) |
Alex | ab232e4 | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 669 | _mtu_error = "*" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 670 | else: |
| 671 | # this is a VIP |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 672 | _if_name = " "*7 |
Alex | 6b633ec | 2019-06-06 19:44:34 -0500 | [diff] [blame] | 673 | _if_name_suffix = "" |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 674 | _ip_str += " VIP" |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 675 | # Save all data |
| 676 | _values = { |
| 677 | "interface": _if_name, |
| 678 | "interface_error": _if_rc, |
| 679 | "interface_note": _if_name_suffix, |
Alex | 1839bbf | 2019-08-22 17:17:21 -0500 | [diff] [blame] | 680 | "interface_map": "\n".join(_host['lines']), |
| 681 | "interface_matrix": _host['matrix'], |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 682 | "ip_address": _ip_str, |
| 683 | "address_type": _proto, |
| 684 | "rt_mtu": _host['mtu'], |
| 685 | "rc_mtu": _rc_mtu_s, |
| 686 | "mtu_error": _mtu_error, |
| 687 | "status": _host['state'], |
| 688 | "status_error": _up_error, |
| 689 | "subnet_gateway": _gate, |
| 690 | "subnet_gateway_error": _gate_error, |
| 691 | "default_gateway": _d_gate_str, |
| 692 | "raw_data": _raw, |
| 693 | "error_note": " and ".join(_notes) |
| 694 | } |
| 695 | if node in _map[_net]: |
| 696 | # add if to host |
| 697 | _map[_net][node].append(_values) |
| 698 | else: |
| 699 | _map[_net][node] = [_values] |
| 700 | _notes = [] |
| 701 | |
| 702 | # save map |
| 703 | self.map = _map |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 704 | return |
| 705 | |
| 706 | def print_map(self): |
| 707 | """ |
| 708 | Create text report for CLI |
| 709 | |
| 710 | :return: none |
| 711 | """ |
| 712 | logger_cli.info("# Networks") |
| 713 | logger_cli.info( |
| 714 | " {0:8} {1:25} {2:25} {3:6} {4:10} {5:10} {6}/{7}".format( |
| 715 | "Host", |
| 716 | "IF", |
| 717 | "IP", |
| 718 | "Proto", |
| 719 | "MTU", |
| 720 | "State", |
| 721 | "Gate", |
| 722 | "Def.Gate" |
| 723 | ) |
| 724 | ) |
| 725 | for network in self.map.keys(): |
| 726 | logger_cli.info("-> {}".format(network)) |
| 727 | for hostname in self.map[network].keys(): |
| 728 | node = hostname.split('.')[0] |
| 729 | _n = self.map[network][hostname] |
| 730 | for _i in _n: |
| 731 | # Host IF IP Proto MTU State Gate Def.Gate |
| 732 | _text = "{:7} {:17} {:25} {:6} {:10} " \ |
| 733 | "{:10} {} / {}".format( |
| 734 | _i['interface'] + _i['interface_error'], |
| 735 | _i['interface_note'], |
| 736 | _i['ip_address'], |
| 737 | _i['address_type'], |
| 738 | _i['rt_mtu'] + _i['rc_mtu'] + _i['mtu_error'], |
| 739 | _i['status'] + _i['status_error'], |
| 740 | _i['subnet_gateway'] + |
| 741 | _i['subnet_gateway_error'], |
| 742 | _i['default_gateway'] |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 743 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 744 | logger_cli.info( |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 745 | " {0:8} {1}".format( |
| 746 | node, |
| 747 | _text |
| 748 | ) |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 749 | ) |
Alex | 836fac8 | 2019-08-22 13:36:16 -0500 | [diff] [blame] | 750 | |
| 751 | # logger_cli.info("\n# Other networks") |
| 752 | # _other = [n for n in _runtime if n not in _reclass] |
| 753 | # for network in _other: |
| 754 | # logger_cli.info("-> {}".format(str(network))) |
| 755 | # names = sorted(_runtime[network].keys()) |
| 756 | |
| 757 | # for hostname in names: |
| 758 | # for _n in _runtime[network][hostname]: |
| 759 | # _ifs = [str(ifs.ip) for ifs in _n['ifs']] |
| 760 | # _text = "{:25} {:25} {:6} {:10} {}".format( |
| 761 | # _n['name'], |
| 762 | # ", ".join(_ifs), |
| 763 | # "-", |
| 764 | # _n['mtu'], |
| 765 | # _n['state'] |
| 766 | # ) |
| 767 | # logger_cli.info( |
| 768 | # " {0:8} {1}".format(hostname.split('.')[0], _text) |
| 769 | # ) |
| 770 | # logger_cli.info("\n") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 771 | return |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 772 | |
| 773 | |
| 774 | class KubeNetworkMapper(NetworkMapper): |
| 775 | def __init__( |
| 776 | self, |
| 777 | config, |
| 778 | errors_class=None, |
| 779 | skip_list=None, |
| 780 | skip_list_file=None |
| 781 | ): |
| 782 | self.master = KubeNodes(config) |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 783 | self.daemonset = None |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 784 | super(KubeNetworkMapper, self).__init__( |
| 785 | config, |
| 786 | errors_class=errors_class, |
| 787 | skip_list=skip_list, |
| 788 | skip_list_file=skip_list_file |
| 789 | ) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 790 | |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 791 | def get_daemonset(self): |
| 792 | if not self.daemonset: |
| 793 | _d = self.master.prepare_daemonset("daemonset_template.yaml") |
| 794 | |
| 795 | # wait for daemonset, normally less than 60 sec for all |
| 796 | # but still, let us give it 10 second per pod |
| 797 | _timeout = self.master.nodes.__len__() * 10 |
| 798 | if not self.master.wait_for_daemonset(_d, timeout=_timeout): |
| 799 | raise KubeException("Daemonset deployment fail") |
| 800 | self.daemonset = _d |
| 801 | return self.daemonset |
| 802 | |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 803 | def get_script_output(self, script, _args=None): |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 804 | """ |
| 805 | Get runtime network by creating DaemonSet with Host network parameter |
| 806 | """ |
| 807 | # prepare daemonset |
| 808 | logger_cli.info("-> Preparing daemonset to get node info") |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 809 | _daemonset = self.get_daemonset() |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 810 | logger_cli.info("-> Running script on daemonset") |
| 811 | # exec script on all pods in daemonset |
Alex | b78191f | 2021-11-02 16:35:46 -0500 | [diff] [blame] | 812 | _result = self.master.execute_cmd_on_daemon_set( |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 813 | _daemonset, |
| 814 | script, |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 815 | _args=_args, |
Alex | b78191f | 2021-11-02 16:35:46 -0500 | [diff] [blame] | 816 | is_script=True |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 817 | ) |
| 818 | |
| 819 | # delete daemonset |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 820 | # TODO: handle daemonset delete properly |
| 821 | # self.master.delete_daemonset(_daemonset) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 822 | |
| 823 | return _result |
| 824 | |
| 825 | def map_networks(self): |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 826 | logger_cli.info("-> Mapping runtime networks") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 827 | self.map_network(self.RUNTIME) |
| 828 | |
| 829 | def map_network(self, source): |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 830 | # if network type is mapped - just return it |
| 831 | if source in self.networks: |
| 832 | return self.networks[source] |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 833 | # maps target network using given source |
| 834 | _networks = None |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 835 | if source == self.RUNTIME: |
| 836 | logger_cli.info("# Mapping node runtime network data") |
Alex | b212954 | 2021-11-23 15:49:42 -0600 | [diff] [blame] | 837 | _r = self.get_script_output("ifs_data.py", _args="json") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 838 | _networks = self._map_runtime_networks(_r) |
| 839 | else: |
| 840 | raise ConfigException( |
| 841 | "Network type not supported in 'Kube': '{}'".format(source) |
| 842 | ) |
| 843 | |
| 844 | self.networks[source] = _networks |
| 845 | return _networks |
| 846 | |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 847 | def create_map(self, skip_keywords=None): |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 848 | """Create all needed elements for map output |
| 849 | |
| 850 | :return: none |
| 851 | """ |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 852 | # shortcut |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 853 | _runtime = self.networks[self.RUNTIME] |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 854 | # networks to skip |
| 855 | _net_skip_list = [] |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 856 | # main networks, target vars |
| 857 | _map = {} |
| 858 | # No matter of proto, at least one IP will be present for the network |
| 859 | # we interested in, since we are to make sure that L3 level |
| 860 | # is configured according to reclass model |
| 861 | for network in _runtime: |
| 862 | # shortcuts |
| 863 | _net = str(network) |
| 864 | _map[_net] = {} |
| 865 | # hostnames |
| 866 | names = sorted(_runtime[network].keys()) |
| 867 | for hostname in names: |
| 868 | _notes = [] |
| 869 | node = hostname.split('.')[0] |
| 870 | if not self.master.is_node_available(hostname, log=False): |
| 871 | logger_cli.info( |
| 872 | " {0:8} {1}".format(node, "node not available") |
| 873 | ) |
| 874 | # add non-responsive node erorr |
| 875 | self.errors.add_error( |
| 876 | self.errors.NET_NODE_NON_RESPONSIVE, |
| 877 | host=hostname |
| 878 | ) |
| 879 | _notes.append( |
| 880 | self.errors.get_error_type_text( |
| 881 | self.errors.NET_NODE_NON_RESPONSIVE |
| 882 | ) |
| 883 | ) |
| 884 | continue |
| 885 | # lookup interface name on node using network CIDR |
| 886 | _if_name = _runtime[network][hostname][0]["name"] |
| 887 | _raw = self.interfaces[hostname][_if_name]['runtime'] |
| 888 | _if_name_suffix = "" |
| 889 | _a = _runtime[network][hostname] |
| 890 | for _host in _a: |
| 891 | for _if in _host['ifs']: |
| 892 | _ip_str = str(_if.exploded) |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 893 | # Make sure we print VIP properly |
| 894 | if _host['vip'] == _ip_str: |
| 895 | _if_name = " "*7 |
| 896 | _if_name_suffix = "" |
| 897 | _ip_str += " VIP" |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 898 | |
| 899 | # Save all data |
| 900 | _values = { |
| 901 | "interface": _if_name, |
| 902 | "interface_note": _if_name_suffix, |
| 903 | "interface_map": "\n".join(_host['lines']), |
| 904 | "interface_matrix": _host['matrix'], |
| 905 | "ip_address": _ip_str, |
| 906 | "rt_mtu": _host['mtu'], |
| 907 | "status": _host['state'], |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 908 | "type": _host['type'], |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 909 | "raw_data": _raw, |
| 910 | } |
| 911 | if node in _map[_net]: |
| 912 | # add if to host |
| 913 | _map[_net][node].append(_values) |
| 914 | else: |
| 915 | _map[_net][node] = [_values] |
| 916 | _notes = [] |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 917 | # process skips: if key substring found in interface name |
| 918 | # then skip the whole network. |
| 919 | if any([True for _w in skip_keywords if _w in _if_name]): |
| 920 | _net_skip_list.append(_net) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 921 | |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 922 | # remove skipped networks from list |
| 923 | _net_skip_list = list(set(_net_skip_list)) |
| 924 | for _net in _net_skip_list: |
| 925 | _map.pop(_net) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 926 | # save map |
| 927 | self.map = _map |
| 928 | return |
| 929 | |
| 930 | def print_map(self): |
| 931 | """ |
| 932 | Create text report for CLI |
| 933 | |
| 934 | :return: none |
| 935 | """ |
| 936 | logger_cli.info("# Networks") |
| 937 | logger_cli.info( |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 938 | " {0:47} {1:12} {2:25} {3:5} {4:4}".format( |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 939 | "Host", |
| 940 | "IF", |
| 941 | "IP", |
| 942 | "MTU", |
| 943 | "State" |
| 944 | ) |
| 945 | ) |
| 946 | for network in self.map.keys(): |
| 947 | logger_cli.info("-> {}".format(network)) |
| 948 | for hostname in self.map[network].keys(): |
| 949 | node = hostname.split('.')[0] |
| 950 | _n = self.map[network][hostname] |
| 951 | for _i in _n: |
| 952 | # Host IF IP Proto MTU State Gate Def.Gate |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 953 | _text = "{:10} {:2} {:25} {:5} {:4}".format( |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 954 | _i['interface'], |
| 955 | _i['interface_note'], |
| 956 | _i['ip_address'], |
| 957 | _i['rt_mtu'], |
| 958 | _i['status'] |
| 959 | ) |
| 960 | logger_cli.info( |
Alex | 3cdb1bd | 2021-09-10 15:51:11 -0500 | [diff] [blame] | 961 | " {0:47} {1}".format( |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 962 | node, |
| 963 | _text |
| 964 | ) |
| 965 | ) |
| 966 | return |