blob: fff6bb64d6220733258fed72fae0ff691e865835 [file] [log] [blame]
Alexe0c5b9e2019-04-23 18:51:23 -05001import ipaddress
2import json
Alex6b633ec2019-06-06 19:44:34 -05003from copy import deepcopy
Alexe0c5b9e2019-04-23 18:51:23 -05004
5from cfg_checker.common import logger_cli
6from cfg_checker.common.exception import InvalidReturnException
Alex1f90e7b2021-09-03 15:31:28 -05007from cfg_checker.common.exception import ConfigException
8from cfg_checker.common.exception import KubeException
Alexe0c5b9e2019-04-23 18:51:23 -05009from cfg_checker.modules.network.network_errors import NetworkErrors
Alex205546c2020-12-30 19:22:30 -060010from cfg_checker.nodes import SaltNodes, KubeNodes
Alexe0c5b9e2019-04-23 18:51:23 -050011
12# TODO: use templated approach
13# net interface structure should be the same
14_if_item = {
15 "name": "unnamed interface",
16 "mac": "",
17 "routes": {},
Alex6b633ec2019-06-06 19:44:34 -050018 "proto": "",
Alexe0c5b9e2019-04-23 18:51:23 -050019 "ip": [],
20 "parameters": {}
21}
22
23# collection of configurations
24_network_item = {
25 "runtime": {},
26 "config": {},
27 "reclass": {}
28}
29
30
31class NetworkMapper(object):
32 RECLASS = "reclass"
33 CONFIG = "config"
34 RUNTIME = "runtime"
35
Alexe9908f72020-05-19 16:04:53 -050036 def __init__(
37 self,
Alex9a4ad212020-10-01 18:04:25 -050038 config,
Alexe9908f72020-05-19 16:04:53 -050039 errors_class=None,
40 skip_list=None,
41 skip_list_file=None
42 ):
Alexe0c5b9e2019-04-23 18:51:23 -050043 logger_cli.info("# Initializing mapper")
Alex205546c2020-12-30 19:22:30 -060044 self.env_config = config
Alex6b633ec2019-06-06 19:44:34 -050045 # init networks and nodes
Alexe0c5b9e2019-04-23 18:51:23 -050046 self.networks = {}
Alex205546c2020-12-30 19:22:30 -060047 self.nodes = self.master.get_nodes(
Alexe9908f72020-05-19 16:04:53 -050048 skip_list=skip_list,
49 skip_list_file=skip_list_file
50 )
Alex205546c2020-12-30 19:22:30 -060051 self.cluster = self.master.get_info()
52 self.domain = self.master.domain
Alex6b633ec2019-06-06 19:44:34 -050053 # init and pre-populate interfaces
54 self.interfaces = {k: {} for k in self.nodes}
55 # Init errors class
Alexe0c5b9e2019-04-23 18:51:23 -050056 if errors_class:
57 self.errors = errors_class
58 else:
59 logger_cli.debug("... init error logs folder")
60 self.errors = NetworkErrors()
61
62 # adding net data to tree
63 def _add_data(self, _list, _n, _h, _d):
64 if _n not in _list:
65 _list[_n] = {}
66 _list[_n][_h] = [_d]
67 elif _h not in _list[_n]:
68 # there is no such host, just create it
69 _list[_n][_h] = [_d]
70 else:
71 # there is such host... this is an error
72 self.errors.add_error(
73 self.errors.NET_DUPLICATE_IF,
74 host=_h,
75 dup_if=_d['name']
76 )
77 _list[_n][_h].append(_d)
78
79 # TODO: refactor map creation. Build one map instead of two separate
80 def _map_network_for_host(self, host, if_class, net_list, data):
81 # filter networks for this IF IP
82 _nets = [n for n in net_list.keys() if if_class.ip in n]
83 _masks = [n.netmask for n in _nets]
84 if len(_nets) > 1:
85 # There a multiple network found for this IP, Error
86 self.errors.add_error(
87 self.errors.NET_SUBNET_INTERSECT,
88 host=host,
89 ip=str(if_class.exploded),
90 networks="; ".join([str(_n) for _n in _nets])
91 )
92 # check mask match
93 if len(_nets) > 0 and if_class.netmask not in _masks:
94 self.errors.add_error(
95 self.errors.NET_MASK_MISMATCH,
96 host=host,
97 if_name=data['name'],
98 if_cidr=if_class.exploded,
99 if_mapped_networks=", ".join([str(_n) for _n in _nets])
100 )
101
102 if len(_nets) < 1:
103 self._add_data(net_list, if_class.network, host, data)
104 else:
105 # add all data
106 for net in _nets:
107 self._add_data(net_list, net, host, data)
108
109 return net_list
110
111 def _map_reclass_networks(self):
112 # class uses nodes from self.nodes dict
113 _reclass = {}
114 # Get required pillars
Alex205546c2020-12-30 19:22:30 -0600115 self.master.get_specific_pillar_for_nodes("linux:network")
116 for node in self.master.nodes.keys():
Alexe0c5b9e2019-04-23 18:51:23 -0500117 # check if this node
Alex205546c2020-12-30 19:22:30 -0600118 if not self.master.is_node_available(node):
Alexe0c5b9e2019-04-23 18:51:23 -0500119 continue
120 # get the reclass value
Alex9a4ad212020-10-01 18:04:25 -0500121 _pillar = \
Alex205546c2020-12-30 19:22:30 -0600122 self.master.nodes[node]['pillars']['linux']['network']
Alexe0c5b9e2019-04-23 18:51:23 -0500123 # we should be ready if there is no interface in reclass for a node
Alex92e07ce2019-05-31 16:00:03 -0500124 # for example on APT node
Alexe0c5b9e2019-04-23 18:51:23 -0500125 if 'interface' in _pillar:
126 _pillar = _pillar['interface']
127 else:
128 logger_cli.info(
129 "... node '{}' skipped, no IF section in reclass".format(
130 node
131 )
132 )
133 continue
Alex92e07ce2019-05-31 16:00:03 -0500134
Alex6b633ec2019-06-06 19:44:34 -0500135 # build map based on IPs and save info too
Alex3bc95f62020-03-05 17:00:04 -0600136 for if_name, _dat in _pillar.items():
Alexb3dc8592019-06-11 13:20:36 -0500137 # get proper IF name
138 _if_name = if_name if 'name' not in _dat else _dat['name']
139 # place it
Alex6b633ec2019-06-06 19:44:34 -0500140 if _if_name not in self.interfaces[node]:
141 self.interfaces[node][_if_name] = deepcopy(_network_item)
Alexb3dc8592019-06-11 13:20:36 -0500142 self.interfaces[node][_if_name]['reclass'] = deepcopy(_dat)
Alex6b633ec2019-06-06 19:44:34 -0500143 # map network if any
Alexb3dc8592019-06-11 13:20:36 -0500144 if 'address' in _dat:
Alexe0c5b9e2019-04-23 18:51:23 -0500145 _if = ipaddress.IPv4Interface(
Alexb3dc8592019-06-11 13:20:36 -0500146 _dat['address'] + '/' + _dat['netmask']
Alexe0c5b9e2019-04-23 18:51:23 -0500147 )
Alexb3dc8592019-06-11 13:20:36 -0500148 _dat['name'] = _if_name
149 _dat['ifs'] = [_if]
Alexe0c5b9e2019-04-23 18:51:23 -0500150
151 _reclass = self._map_network_for_host(
152 node,
153 _if,
154 _reclass,
Alexb3dc8592019-06-11 13:20:36 -0500155 _dat
Alexe0c5b9e2019-04-23 18:51:23 -0500156 )
157
158 return _reclass
159
160 def _map_configured_networks(self):
161 # class uses nodes from self.nodes dict
162 _confs = {}
163
Alex92e07ce2019-05-31 16:00:03 -0500164 # TODO: parse /etc/network/interfaces
165
Alexe0c5b9e2019-04-23 18:51:23 -0500166 return _confs
167
Alex1f90e7b2021-09-03 15:31:28 -0500168 def _map_runtime_networks(self, result):
Alexe0c5b9e2019-04-23 18:51:23 -0500169 # class uses nodes from self.nodes dict
170 _runtime = {}
Alex205546c2020-12-30 19:22:30 -0600171 for key in self.master.nodes.keys():
Alexe0c5b9e2019-04-23 18:51:23 -0500172 # check if we are to work with this node
Alex205546c2020-12-30 19:22:30 -0600173 if not self.master.is_node_available(key):
Alexe0c5b9e2019-04-23 18:51:23 -0500174 continue
Alex205546c2020-12-30 19:22:30 -0600175 # due to much data to be passed from master,
Alexe0c5b9e2019-04-23 18:51:23 -0500176 # it is happening in order
Alex1f90e7b2021-09-03 15:31:28 -0500177 if key in result:
178 _text = result[key]
Alexe0c5b9e2019-04-23 18:51:23 -0500179 if '{' in _text and '}' in _text:
180 _text = _text[_text.find('{'):]
181 else:
182 raise InvalidReturnException(
183 "Non-json object returned: '{}'".format(
184 _text
185 )
186 )
187 _dict = json.loads(_text[_text.find('{'):])
Alex205546c2020-12-30 19:22:30 -0600188 self.master.nodes[key]['routes'] = _dict.pop("routes")
189 self.master.nodes[key]['networks'] = _dict
Alexe0c5b9e2019-04-23 18:51:23 -0500190 else:
Alex205546c2020-12-30 19:22:30 -0600191 self.master.nodes[key]['networks'] = {}
192 self.master.nodes[key]['routes'] = {}
Alexe0c5b9e2019-04-23 18:51:23 -0500193 logger_cli.debug("... {} has {} networks".format(
194 key,
Alex205546c2020-12-30 19:22:30 -0600195 len(self.master.nodes[key]['networks'].keys())
Alexe0c5b9e2019-04-23 18:51:23 -0500196 ))
197 logger_cli.info("-> done collecting networks data")
198
199 logger_cli.info("-> mapping IPs")
200 # match interfaces by IP subnets
Alex205546c2020-12-30 19:22:30 -0600201 for host, node_data in self.master.nodes.items():
202 if not self.master.is_node_available(host):
Alexe0c5b9e2019-04-23 18:51:23 -0500203 continue
204
Alex3bc95f62020-03-05 17:00:04 -0600205 for net_name, net_data in node_data['networks'].items():
Alexb3dc8592019-06-11 13:20:36 -0500206 # cut net name
207 _i = net_name.find('@')
208 _name = net_name if _i < 0 else net_name[:_i]
Alexe0c5b9e2019-04-23 18:51:23 -0500209 # get ips and calculate subnets
Alexb3dc8592019-06-11 13:20:36 -0500210 if _name in ['lo']:
Alexe0c5b9e2019-04-23 18:51:23 -0500211 # skip the localhost
212 continue
Alex6b633ec2019-06-06 19:44:34 -0500213 else:
214 # add collected data to interface storage
Alexb3dc8592019-06-11 13:20:36 -0500215 if _name not in self.interfaces[host]:
216 self.interfaces[host][_name] = \
Alex6b633ec2019-06-06 19:44:34 -0500217 deepcopy(_network_item)
Alexb3dc8592019-06-11 13:20:36 -0500218 self.interfaces[host][_name]['runtime'] = \
Alex6b633ec2019-06-06 19:44:34 -0500219 deepcopy(net_data)
220
Alexe0c5b9e2019-04-23 18:51:23 -0500221 # get data and make sure that wide mask goes first
222 _ip4s = sorted(
223 net_data['ipv4'],
224 key=lambda s: s[s.index('/'):]
225 )
226 for _ip_str in _ip4s:
227 # create interface class
228 _if = ipaddress.IPv4Interface(_ip_str)
229 # check if this is a VIP
230 # ...all those will have /32 mask
231 net_data['vip'] = None
232 if _if.network.prefixlen == 32:
233 net_data['vip'] = str(_if.exploded)
234 if 'name' not in net_data:
Alexb3dc8592019-06-11 13:20:36 -0500235 net_data['name'] = _name
Alexe0c5b9e2019-04-23 18:51:23 -0500236 if 'ifs' not in net_data:
237 net_data['ifs'] = [_if]
238 # map it
239 _runtime = self._map_network_for_host(
240 host,
241 _if,
242 _runtime,
243 net_data
244 )
245 else:
246 # data is already there, just add VIP
247 net_data['ifs'].append(_if)
248
Alex1839bbf2019-08-22 17:17:21 -0500249 def process_interface(lvl, interface, tree, res):
250 # get childs for each root
251 # tree row item (<if_name>, [<parents>], [<childs>])
252 if lvl not in tree:
253 # - no level - add it
254 tree[lvl] = {}
255 # there is such interface in this level?
256 if interface not in tree[lvl]:
257 # - IF not present
Alexf3dbe862019-10-07 15:17:04 -0500258 _n = ''
259 if interface not in res:
260 _n = 'unknown IF'
261 _p = None
262 _c = None
263 else:
264 # -- get parents, add
265 _p = res[interface]['lower']
266 # -- get childs, add
267 _c = res[interface]['upper']
268
Alex1839bbf2019-08-22 17:17:21 -0500269 # if None, put empty list
270 _p = _p if _p else []
Alex1839bbf2019-08-22 17:17:21 -0500271 # if None, put empty list
272 _c = _c if _c else []
273 tree[lvl].update({
274 interface: {
Alexf3dbe862019-10-07 15:17:04 -0500275 "note": _n,
Alex1839bbf2019-08-22 17:17:21 -0500276 "parents": _p,
277 "children": _c,
278 "size": len(_p) if len(_p) > len(_c) else len(_c)
279 }
280 })
281 for p_if in tree[lvl][interface]["parents"]:
282 # -- cycle: execute process for next parent, lvl-1
283 process_interface(lvl-1, p_if, tree, res)
284 for c_if in tree[lvl][interface]["children"]:
285 # -- cycle: execute process for next child, lvl+1
286 process_interface(lvl+1, c_if, tree, res)
287 else:
288 # - IF present - exit (been here already)
289 return
290
291 def _put(cNet, cIndex, _list):
Alexf3dbe862019-10-07 15:17:04 -0500292 _added = False
293 _actual_index = -1
294 # Check list len
295 _len = len(_list)
296 if cIndex >= _len:
297 # grow list to meet index
298 _list = _list + [''] * (cIndex - _len + 1)
299 _len = len(_list)
300
301 for _cI in range(cIndex, _len):
Alex1839bbf2019-08-22 17:17:21 -0500302 # add child per index
303 # if space is free
304 if not _list[_cI]:
305 _list[_cI] = cNet
Alexf3dbe862019-10-07 15:17:04 -0500306 _added = True
307 _actual_index = _cI
Alex1839bbf2019-08-22 17:17:21 -0500308 break
Alexf3dbe862019-10-07 15:17:04 -0500309 if not _added:
310 # grow list by one entry
311 _list = _list + [cNet]
312 _actual_index = len(_list) - 1
313 return _actual_index, _list
Alex1839bbf2019-08-22 17:17:21 -0500314
315 # build network hierachy
316 nr = node_data['networks']
317 # walk interface tree
318 for _ifname in node_data['networks']:
319 _tree = {}
320 _level = 0
321 process_interface(_level, _ifname, _tree, nr)
322 # save tree for node/if
323 node_data['networks'][_ifname]['tree'] = _tree
324
325 # debug, print built tree
326 # logger_cli.debug("# '{}'".format(_ifname))
Alex3bc95f62020-03-05 17:00:04 -0600327 lvls = list(_tree.keys())
Alex1839bbf2019-08-22 17:17:21 -0500328 lvls.sort()
329 n = len(lvls)
330 m = max([len(_tree[k].keys()) for k in _tree.keys()])
331 matrix = [["" for i in range(m)] for j in range(n)]
332 x = 0
333 while True:
334 _lv = lvls.pop(0)
335 # get all interfaces on this level
Alex3bc95f62020-03-05 17:00:04 -0600336 nets = iter(_tree[_lv].keys())
Alex1839bbf2019-08-22 17:17:21 -0500337 while True:
338 y = 0
339 # get next interface
Alex3bc95f62020-03-05 17:00:04 -0600340 try:
341 _net = next(nets)
342 except StopIteration:
343 break
Alex1839bbf2019-08-22 17:17:21 -0500344 # all nets
345 _a = [_net]
346 # put current interface if this is only one left
347 if not _tree[_lv][_net]['children']:
348 if _net not in matrix[x]:
Alexf3dbe862019-10-07 15:17:04 -0500349 _, matrix[x] = _put(
350 _net,
351 y,
352 matrix[x]
353 )
Alex1839bbf2019-08-22 17:17:21 -0500354 y += 1
355 else:
356 # get all nets with same child
357 for _c in _tree[_lv][_net]['children']:
358 for _o_net in nets:
359 if _c in _tree[_lv][_o_net]['children']:
360 _a.append(_o_net)
361 # flush collected nets
362 for idx in range(len(_a)):
363 if _a[idx] in matrix[x]:
364 # there is such interface on this level
365 # get index
366 _nI = matrix[x].index(_a[idx])
Alexf3dbe862019-10-07 15:17:04 -0500367 _, matrix[x+1] = _put(
368 _c,
369 _nI,
370 matrix[x+1]
371 )
Alex1839bbf2019-08-22 17:17:21 -0500372 else:
373 # there is no such interface
374 # add it
Alexf3dbe862019-10-07 15:17:04 -0500375 _t, matrix[x] = _put(
376 _a[idx],
377 0,
378 matrix[x]
379 )
380 # also, put child
381 _, matrix[x+1] = _put(
382 _c,
383 _t,
384 matrix[x+1]
385 )
Alex1839bbf2019-08-22 17:17:21 -0500386 # remove collected nets from processing
387 if _a[idx] in nets:
388 nets.remove(_a[idx])
389 y += len(_a)
390 if not nets:
391 x += 1
392 break
393 if not lvls:
394 break
395
396 lines = []
397 _columns = [len(max([i for i in li])) for li in matrix]
398 for idx_y in range(m):
399 line = ""
400 for idx_x in range(n):
Alex9b2c1d12020-03-19 09:32:35 -0500401 _len = _columns[idx_x] if _columns[idx_x] else 1
402 _fmt = "{" + ":{}".format(_len) + "} "
Alex1839bbf2019-08-22 17:17:21 -0500403 line += _fmt.format(matrix[idx_x][idx_y])
404 lines.append(line)
405 node_data['networks'][_ifname]['matrix'] = matrix
406 node_data['networks'][_ifname]['lines'] = lines
Alexe0c5b9e2019-04-23 18:51:23 -0500407 return _runtime
408
Alex1f90e7b2021-09-03 15:31:28 -0500409
410class SaltNetworkMapper(NetworkMapper):
411 def __init__(
412 self,
413 config,
414 errors_class=None,
415 skip_list=None,
416 skip_list_file=None
417 ):
418 self.master = SaltNodes(config)
419 super(SaltNetworkMapper, self).__init__(
420 config,
421 errors_class=errors_class,
422 skip_list=skip_list,
423 skip_list_file=skip_list_file
424 )
425
426 def get_script_output(self):
427 """
428 Get runtime networks by executing script on nodes
429 """
430 logger_cli.info("# Mapping node runtime network data")
431 self.master.prepare_script_on_active_nodes("ifs_data.py")
432 _result = self.master.execute_script_on_active_nodes(
433 "ifs_data.py",
434 args="json"
435 )
436
437 return _result
438
439 def map_networks(self):
440 self.map_network(self.RECLASS)
441 self.map_network(self.RUNTIME)
442
Alexe0c5b9e2019-04-23 18:51:23 -0500443 def map_network(self, source):
444 # maps target network using given source
445 _networks = None
446
447 if source == self.RECLASS:
448 _networks = self._map_reclass_networks()
449 elif source == self.CONFIG:
450 _networks = self._map_configured_networks()
451 elif source == self.RUNTIME:
Alex1f90e7b2021-09-03 15:31:28 -0500452 _r = self.get_script_output()
453 _networks = self._map_runtime_networks(_r)
Alexe0c5b9e2019-04-23 18:51:23 -0500454
455 self.networks[source] = _networks
456 return _networks
457
Alex836fac82019-08-22 13:36:16 -0500458 def create_map(self):
459 """Create all needed elements for map output
Alexe0c5b9e2019-04-23 18:51:23 -0500460
461 :return: none
462 """
463 _runtime = self.networks[self.RUNTIME]
464 _reclass = self.networks[self.RECLASS]
Alex836fac82019-08-22 13:36:16 -0500465
466 # main networks, target vars
467 _map = {}
Alex6b633ec2019-06-06 19:44:34 -0500468 # No matter of proto, at least one IP will be present for the network
Alex836fac82019-08-22 13:36:16 -0500469 # we interested in, since we are to make sure that L3 level
470 # is configured according to reclass model
Alexe0c5b9e2019-04-23 18:51:23 -0500471 for network in _reclass:
472 # shortcuts
473 _net = str(network)
Alex836fac82019-08-22 13:36:16 -0500474 _map[_net] = {}
Alexe0c5b9e2019-04-23 18:51:23 -0500475 if network not in _runtime:
476 # reclass has network that not found in runtime
477 self.errors.add_error(
478 self.errors.NET_NO_RUNTIME_NETWORK,
479 reclass_net=str(network)
480 )
Alex1839bbf2019-08-22 17:17:21 -0500481 logger_cli.warn(
482 "WARN: {}: {}".format(
483 " No runtime network ", str(network)
484 )
485 )
Alexe0c5b9e2019-04-23 18:51:23 -0500486 continue
Alex6b633ec2019-06-06 19:44:34 -0500487 # hostnames
Alexe0c5b9e2019-04-23 18:51:23 -0500488 names = sorted(_runtime[network].keys())
489 for hostname in names:
Alex836fac82019-08-22 13:36:16 -0500490 _notes = []
Alex6b633ec2019-06-06 19:44:34 -0500491 node = hostname.split('.')[0]
Alex205546c2020-12-30 19:22:30 -0600492 if not self.master.is_node_available(hostname, log=False):
Alexe0c5b9e2019-04-23 18:51:23 -0500493 logger_cli.info(
Alex6b633ec2019-06-06 19:44:34 -0500494 " {0:8} {1}".format(node, "node not available")
Alexe0c5b9e2019-04-23 18:51:23 -0500495 )
496 # add non-responsive node erorr
497 self.errors.add_error(
498 self.errors.NET_NODE_NON_RESPONSIVE,
499 host=hostname
500 )
Alex836fac82019-08-22 13:36:16 -0500501 _notes.append(
502 self.errors.get_error_type_text(
503 self.errors.NET_NODE_NON_RESPONSIVE
504 )
505 )
Alexe0c5b9e2019-04-23 18:51:23 -0500506 continue
Alex6b633ec2019-06-06 19:44:34 -0500507 # lookup interface name on node using network CIDR
508 _if_name = _runtime[network][hostname][0]["name"]
Alex836fac82019-08-22 13:36:16 -0500509 _raw = self.interfaces[hostname][_if_name]['runtime']
Alex6b633ec2019-06-06 19:44:34 -0500510 # get proper reclass
511 _r = self.interfaces[hostname][_if_name]['reclass']
Alex6b633ec2019-06-06 19:44:34 -0500512 _if_name_suffix = ""
513 # get the proto value
Alex3b8e5432019-06-11 15:21:59 -0500514 if _r:
515 _if_rc = ""
516 else:
517 self.errors.add_error(
518 self.errors.NET_NODE_UNEXPECTED_IF,
519 host=hostname,
520 if_name=_if_name
521 )
Alex836fac82019-08-22 13:36:16 -0500522 _notes.append(
523 self.errors.get_error_type_text(
524 self.errors.NET_NODE_UNEXPECTED_IF
525 )
526 )
Alex3b8e5432019-06-11 15:21:59 -0500527 _if_rc = "*"
528
Alex6b633ec2019-06-06 19:44:34 -0500529 if "proto" in _r:
530 _proto = _r['proto']
Alexe0c5b9e2019-04-23 18:51:23 -0500531 else:
Alex6b633ec2019-06-06 19:44:34 -0500532 _proto = "-"
Alexe0c5b9e2019-04-23 18:51:23 -0500533
Alex6b633ec2019-06-06 19:44:34 -0500534 if "type" in _r:
535 _if_name_suffix += _r["type"]
536 if "use_interfaces" in _r:
537 _if_name_suffix += "->" + ",".join(_r["use_interfaces"])
538
539 if _if_name_suffix:
540 _if_name_suffix = "({})".format(_if_name_suffix)
541
Alex6b633ec2019-06-06 19:44:34 -0500542 # get gate and routes if proto is static
543 if _proto == 'static':
544 # get the gateway for current net
Alex205546c2020-12-30 19:22:30 -0600545 _routes = self.master.nodes[hostname]['routes']
Alex6b633ec2019-06-06 19:44:34 -0500546 _route = _routes[_net] if _net in _routes else None
Alex6b633ec2019-06-06 19:44:34 -0500547 # get the default gateway
548 if 'default' in _routes:
549 _d_gate = ipaddress.IPv4Address(
550 _routes['default']['gateway']
551 )
552 else:
553 _d_gate = None
Alexb3dc8592019-06-11 13:20:36 -0500554 _d_gate_str = str(_d_gate) if _d_gate else "No default!"
555 # match route with default
556 if not _route:
557 _gate = "?"
558 else:
559 _gate = _route['gateway'] if _route['gateway'] else "-"
Alex6b633ec2019-06-06 19:44:34 -0500560 else:
561 # in case of manual and dhcp, no check possible
562 _gate = "-"
563 _d_gate = "-"
Alex4067f002019-06-11 10:47:16 -0500564 _d_gate_str = "-"
Alex6b633ec2019-06-06 19:44:34 -0500565 # iterate through interfaces
Alexe0c5b9e2019-04-23 18:51:23 -0500566 _a = _runtime[network][hostname]
567 for _host in _a:
568 for _if in _host['ifs']:
Alexe0c5b9e2019-04-23 18:51:23 -0500569 _ip_str = str(_if.exploded)
Alexab232e42019-06-06 19:44:34 -0500570 _gate_error = ""
571 _up_error = ""
572 _mtu_error = ""
Alexe0c5b9e2019-04-23 18:51:23 -0500573
Alexb3dc8592019-06-11 13:20:36 -0500574 # Match gateway
Alexab232e42019-06-06 19:44:34 -0500575 if _proto == 'static':
Alexb3dc8592019-06-11 13:20:36 -0500576 # default reclass gate
Alex6b633ec2019-06-06 19:44:34 -0500577 _r_gate = "-"
578 if "gateway" in _r:
579 _r_gate = _r["gateway"]
Alexb3dc8592019-06-11 13:20:36 -0500580
Alexab232e42019-06-06 19:44:34 -0500581 # if values not match, put *
Alexb3dc8592019-06-11 13:20:36 -0500582 if _gate != _r_gate and _d_gate_str != _r_gate:
583 # if values not match, check if default match
Alex3b8e5432019-06-11 15:21:59 -0500584 self.errors.add_error(
585 self.errors.NET_UNEXPECTED_GATEWAY,
586 host=hostname,
587 if_name=_if_name,
588 ip=_ip_str,
589 gateway=_gate
590 )
Alex836fac82019-08-22 13:36:16 -0500591 _notes.append(
592 self.errors.get_error_type_text(
593 self.errors.NET_UNEXPECTED_GATEWAY
594 )
595 )
Alexab232e42019-06-06 19:44:34 -0500596 _gate_error = "*"
Alexe0c5b9e2019-04-23 18:51:23 -0500597
598 # IF status in reclass
Alex6b633ec2019-06-06 19:44:34 -0500599 _e = "enabled"
Alexab232e42019-06-06 19:44:34 -0500600 if _e not in _r:
Alex3b8e5432019-06-11 15:21:59 -0500601 self.errors.add_error(
602 self.errors.NET_NO_RC_IF_STATUS,
603 host=hostname,
604 if_name=_if_name
605 )
Alex836fac82019-08-22 13:36:16 -0500606 _notes.append(
607 self.errors.get_error_type_text(
608 self.errors.NET_NO_RC_IF_STATUS
609 )
610 )
Alexab232e42019-06-06 19:44:34 -0500611 _up_error = "*"
Alexe0c5b9e2019-04-23 18:51:23 -0500612
Alexe0c5b9e2019-04-23 18:51:23 -0500613 _rc_mtu = _r['mtu'] if 'mtu' in _r else None
Alexab232e42019-06-06 19:44:34 -0500614 _rc_mtu_s = ""
Alexe0c5b9e2019-04-23 18:51:23 -0500615 # check if this is a VIP address
616 # no checks needed if yes.
617 if _host['vip'] != _ip_str:
618 if _rc_mtu:
Alex3b8e5432019-06-11 15:21:59 -0500619 _rc_mtu_s = str(_rc_mtu)
Alexe0c5b9e2019-04-23 18:51:23 -0500620 # if there is an MTU value, match it
621 if _host['mtu'] != _rc_mtu_s:
622 self.errors.add_error(
623 self.errors.NET_MTU_MISMATCH,
624 host=hostname,
Alex6b633ec2019-06-06 19:44:34 -0500625 if_name=_if_name,
Alexe0c5b9e2019-04-23 18:51:23 -0500626 if_cidr=_ip_str,
627 reclass_mtu=_rc_mtu,
628 runtime_mtu=_host['mtu']
629 )
Alex836fac82019-08-22 13:36:16 -0500630 _notes.append(
631 self.errors.get_error_type_text(
632 self.errors.NET_MTU_MISMATCH
633 )
634 )
Alexb3dc8592019-06-11 13:20:36 -0500635 _rc_mtu_s = "/" + _rc_mtu_s
Alexab232e42019-06-06 19:44:34 -0500636 _mtu_error = "*"
637 else:
638 # empty the matched value
639 _rc_mtu_s = ""
Alex3b8e5432019-06-11 15:21:59 -0500640 elif _host['mtu'] != '1500' and \
641 _proto not in ["-", "dhcp"]:
Alexe0c5b9e2019-04-23 18:51:23 -0500642 # there is no MTU value in reclass
643 # and runtime value is not default
644 self.errors.add_error(
645 self.errors.NET_MTU_EMPTY,
646 host=hostname,
Alex6b633ec2019-06-06 19:44:34 -0500647 if_name=_if_name,
Alexe0c5b9e2019-04-23 18:51:23 -0500648 if_cidr=_ip_str,
649 if_mtu=_host['mtu']
650 )
Alex836fac82019-08-22 13:36:16 -0500651 _notes.append(
652 self.errors.get_error_type_text(
653 self.errors.NET_MTU_EMPTY
654 )
655 )
Alexab232e42019-06-06 19:44:34 -0500656 _mtu_error = "*"
Alexe0c5b9e2019-04-23 18:51:23 -0500657 else:
658 # this is a VIP
Alex6b633ec2019-06-06 19:44:34 -0500659 _if_name = " "*7
Alex6b633ec2019-06-06 19:44:34 -0500660 _if_name_suffix = ""
Alexe0c5b9e2019-04-23 18:51:23 -0500661 _ip_str += " VIP"
Alex836fac82019-08-22 13:36:16 -0500662 # Save all data
663 _values = {
664 "interface": _if_name,
665 "interface_error": _if_rc,
666 "interface_note": _if_name_suffix,
Alex1839bbf2019-08-22 17:17:21 -0500667 "interface_map": "\n".join(_host['lines']),
668 "interface_matrix": _host['matrix'],
Alex836fac82019-08-22 13:36:16 -0500669 "ip_address": _ip_str,
670 "address_type": _proto,
671 "rt_mtu": _host['mtu'],
672 "rc_mtu": _rc_mtu_s,
673 "mtu_error": _mtu_error,
674 "status": _host['state'],
675 "status_error": _up_error,
676 "subnet_gateway": _gate,
677 "subnet_gateway_error": _gate_error,
678 "default_gateway": _d_gate_str,
679 "raw_data": _raw,
680 "error_note": " and ".join(_notes)
681 }
682 if node in _map[_net]:
683 # add if to host
684 _map[_net][node].append(_values)
685 else:
686 _map[_net][node] = [_values]
687 _notes = []
688
689 # save map
690 self.map = _map
Alex836fac82019-08-22 13:36:16 -0500691 return
692
693 def print_map(self):
694 """
695 Create text report for CLI
696
697 :return: none
698 """
699 logger_cli.info("# Networks")
700 logger_cli.info(
701 " {0:8} {1:25} {2:25} {3:6} {4:10} {5:10} {6}/{7}".format(
702 "Host",
703 "IF",
704 "IP",
705 "Proto",
706 "MTU",
707 "State",
708 "Gate",
709 "Def.Gate"
710 )
711 )
712 for network in self.map.keys():
713 logger_cli.info("-> {}".format(network))
714 for hostname in self.map[network].keys():
715 node = hostname.split('.')[0]
716 _n = self.map[network][hostname]
717 for _i in _n:
718 # Host IF IP Proto MTU State Gate Def.Gate
719 _text = "{:7} {:17} {:25} {:6} {:10} " \
720 "{:10} {} / {}".format(
721 _i['interface'] + _i['interface_error'],
722 _i['interface_note'],
723 _i['ip_address'],
724 _i['address_type'],
725 _i['rt_mtu'] + _i['rc_mtu'] + _i['mtu_error'],
726 _i['status'] + _i['status_error'],
727 _i['subnet_gateway'] +
728 _i['subnet_gateway_error'],
729 _i['default_gateway']
Alexe0c5b9e2019-04-23 18:51:23 -0500730 )
Alexe0c5b9e2019-04-23 18:51:23 -0500731 logger_cli.info(
Alex836fac82019-08-22 13:36:16 -0500732 " {0:8} {1}".format(
733 node,
734 _text
735 )
Alexe0c5b9e2019-04-23 18:51:23 -0500736 )
Alex836fac82019-08-22 13:36:16 -0500737
738 # logger_cli.info("\n# Other networks")
739 # _other = [n for n in _runtime if n not in _reclass]
740 # for network in _other:
741 # logger_cli.info("-> {}".format(str(network)))
742 # names = sorted(_runtime[network].keys())
743
744 # for hostname in names:
745 # for _n in _runtime[network][hostname]:
746 # _ifs = [str(ifs.ip) for ifs in _n['ifs']]
747 # _text = "{:25} {:25} {:6} {:10} {}".format(
748 # _n['name'],
749 # ", ".join(_ifs),
750 # "-",
751 # _n['mtu'],
752 # _n['state']
753 # )
754 # logger_cli.info(
755 # " {0:8} {1}".format(hostname.split('.')[0], _text)
756 # )
757 # logger_cli.info("\n")
Alex1f90e7b2021-09-03 15:31:28 -0500758 return
Alex205546c2020-12-30 19:22:30 -0600759
760
761class KubeNetworkMapper(NetworkMapper):
762 def __init__(
763 self,
764 config,
765 errors_class=None,
766 skip_list=None,
767 skip_list_file=None
768 ):
769 self.master = KubeNodes(config)
770 super(KubeNetworkMapper, self).__init__(
771 config,
772 errors_class=errors_class,
773 skip_list=skip_list,
774 skip_list_file=skip_list_file
775 )
Alex1f90e7b2021-09-03 15:31:28 -0500776
777 def get_script_output(self, script, args=None):
778 """
779 Get runtime network by creating DaemonSet with Host network parameter
780 """
781 # prepare daemonset
782 logger_cli.info("-> Preparing daemonset to get node info")
783 _daemonset = self.master.prepare_daemonset(
784 "daemonset_template.yaml",
785 config_map=script
786 )
787
788 # wait for daemonset, normally less than 60 sec for all
789 # but still, let us give it 10 second per pod
790 _timeout = self.master.nodes.__len__() * 10
791 if not self.master.wait_for_daemonset(_daemonset, timeout=_timeout):
792 raise KubeException("Daemonset deployment fail")
793 logger_cli.info("-> Running script on daemonset")
794 # exec script on all pods in daemonset
795 _result = self.master.execute_script_on_daemon_set(
796 _daemonset,
797 script,
798 args=args
799 )
800
801 # delete daemonset
802 self.master.delete_daemonset(_daemonset)
803
804 return _result
805
806 def map_networks(self):
807 self.map_network(self.RUNTIME)
808
809 def map_network(self, source):
810 # maps target network using given source
811 _networks = None
812
813 if source == self.RUNTIME:
814 logger_cli.info("# Mapping node runtime network data")
815 _r = self.get_script_output("ifs_data.py", args="json")
816 _networks = self._map_runtime_networks(_r)
817 else:
818 raise ConfigException(
819 "Network type not supported in 'Kube': '{}'".format(source)
820 )
821
822 self.networks[source] = _networks
823 return _networks
824
825 def create_map(self):
826 """Create all needed elements for map output
827
828 :return: none
829 """
830 _runtime = self.networks[self.RUNTIME]
831
832 # main networks, target vars
833 _map = {}
834 # No matter of proto, at least one IP will be present for the network
835 # we interested in, since we are to make sure that L3 level
836 # is configured according to reclass model
837 for network in _runtime:
838 # shortcuts
839 _net = str(network)
840 _map[_net] = {}
841 # hostnames
842 names = sorted(_runtime[network].keys())
843 for hostname in names:
844 _notes = []
845 node = hostname.split('.')[0]
846 if not self.master.is_node_available(hostname, log=False):
847 logger_cli.info(
848 " {0:8} {1}".format(node, "node not available")
849 )
850 # add non-responsive node erorr
851 self.errors.add_error(
852 self.errors.NET_NODE_NON_RESPONSIVE,
853 host=hostname
854 )
855 _notes.append(
856 self.errors.get_error_type_text(
857 self.errors.NET_NODE_NON_RESPONSIVE
858 )
859 )
860 continue
861 # lookup interface name on node using network CIDR
862 _if_name = _runtime[network][hostname][0]["name"]
863 _raw = self.interfaces[hostname][_if_name]['runtime']
864 _if_name_suffix = ""
865 _a = _runtime[network][hostname]
866 for _host in _a:
867 for _if in _host['ifs']:
868 _ip_str = str(_if.exploded)
869
870 # Save all data
871 _values = {
872 "interface": _if_name,
873 "interface_note": _if_name_suffix,
874 "interface_map": "\n".join(_host['lines']),
875 "interface_matrix": _host['matrix'],
876 "ip_address": _ip_str,
877 "rt_mtu": _host['mtu'],
878 "status": _host['state'],
879 "raw_data": _raw,
880 }
881 if node in _map[_net]:
882 # add if to host
883 _map[_net][node].append(_values)
884 else:
885 _map[_net][node] = [_values]
886 _notes = []
887
888 # save map
889 self.map = _map
890 return
891
892 def print_map(self):
893 """
894 Create text report for CLI
895
896 :return: none
897 """
898 logger_cli.info("# Networks")
899 logger_cli.info(
900 " {0:8} {1:25} {2:25} {3:10} {4:10}".format(
901 "Host",
902 "IF",
903 "IP",
904 "MTU",
905 "State"
906 )
907 )
908 for network in self.map.keys():
909 logger_cli.info("-> {}".format(network))
910 for hostname in self.map[network].keys():
911 node = hostname.split('.')[0]
912 _n = self.map[network][hostname]
913 for _i in _n:
914 # Host IF IP Proto MTU State Gate Def.Gate
915 _text = "{:7} {:17} {:25} {:5} {:10}".format(
916 _i['interface'],
917 _i['interface_note'],
918 _i['ip_address'],
919 _i['rt_mtu'],
920 _i['status']
921 )
922 logger_cli.info(
923 " {0:8} {1}".format(
924 node,
925 _text
926 )
927 )
928 return