def test_virtual_route_without_dev_no_track_not_supported(self): with mock.patch.object( keepalived, '_is_keepalived_use_no_track_supported', return_value=False): route = keepalived.KeepalivedVirtualRoute('50.0.0.0/8', '1.2.3.4') self.assertEqual('50.0.0.0/8 via 1.2.3.4', route.build_config())
def test_write_check_script(self): conf_dir = '/etc/ha_confs/qrouter-x' ts = keepalived.KeepalivedTrackScript(VRRP_INTERVAL, conf_dir, VRRP_ID) ts.routes = [ keepalived.KeepalivedVirtualRoute('12.0.0.0/24', '10.0.0.1'), keepalived.KeepalivedVirtualRoute('2001:db8::1', '2001:db8::1'), ] with mock.patch.object(keepalived, 'file_utils') as patched_utils: ts.write_check_script() patched_utils.replace_file.assert_called_with( os.path.join(conf_dir, 'ha_check_script_1.sh'), """#!/bin/bash -eu ping -c 1 -w 1 10.0.0.1 1>/dev/null || exit 1 ping6 -c 1 -w 1 2001:db8::1 1>/dev/null || exit 1""", 0o520 )
def _get_instance_routes(cls): routes = keepalived.KeepalivedInstanceRoutes() default_gw_eth0 = keepalived.KeepalivedVirtualRoute( '0.0.0.0/0', '1.0.0.254', 'eth0') default_gw_eth1 = keepalived.KeepalivedVirtualRoute( '::/0', 'fe80::3e97:eff:fe26:3bfa/64', 'eth1') routes.gateway_routes = [default_gw_eth0, default_gw_eth1] extra_routes = [ keepalived.KeepalivedVirtualRoute('10.0.0.0/8', '1.0.0.1'), keepalived.KeepalivedVirtualRoute('20.0.0.0/8', '2.0.0.2')] routes.extra_routes = extra_routes extra_subnets = [ keepalived.KeepalivedVirtualRoute( '30.0.0.0/8', None, 'eth0', scope='link')] routes.extra_subnets = extra_subnets return routes
def routes_updated(self, old_routes, new_routes): instance = self._get_keepalived_instance() instance.virtual_routes.extra_routes = [ keepalived.KeepalivedVirtualRoute( route['destination'], route['nexthop']) for route in new_routes] super(HaRouter, self).routes_updated(old_routes, new_routes)
def _get_config(self): config = keepalived.KeepalivedConf() group1 = keepalived.KeepalivedGroup(1) group2 = keepalived.KeepalivedGroup(2) group1.set_notify('master', '/tmp/script.sh') instance1 = keepalived.KeepalivedInstance('MASTER', 'eth0', 1, advert_int=5) instance1.set_authentication('AH', 'pass123') instance1.track_interfaces.append("eth0") vip_address1 = keepalived.KeepalivedVipAddress('192.168.1.0/24', 'eth1') vip_address2 = keepalived.KeepalivedVipAddress('192.168.2.0/24', 'eth2') vip_address3 = keepalived.KeepalivedVipAddress('192.168.3.0/24', 'eth2') vip_address_ex = keepalived.KeepalivedVipAddress( '192.168.55.0/24', 'eth10') instance1.vips.append(vip_address1) instance1.vips.append(vip_address2) instance1.vips.append(vip_address3) instance1.vips.append(vip_address_ex) virtual_route = keepalived.KeepalivedVirtualRoute( "0.0.0.0/0", "192.168.1.1", "eth1") instance1.virtual_routes.append(virtual_route) group1.add_instance(instance1) instance2 = keepalived.KeepalivedInstance('MASTER', 'eth4', 2, mcast_src_ip='224.0.0.1') instance2.track_interfaces.append("eth4") vip_address1 = keepalived.KeepalivedVipAddress('192.168.3.0/24', 'eth6') instance2.vips.append(vip_address1) instance2.vips.append(vip_address2) instance2.vips.append(vip_address_ex) group2.add_instance(instance2) config.add_group(group1) config.add_instance(instance1) config.add_group(group2) config.add_instance(instance2) return config
def test_virtual_route_with_dev_no_track_not_supported(self): with mock.patch.object( keepalived, '_is_keepalived_use_no_track_supported', return_value=False): route = keepalived.KeepalivedVirtualRoute( n_consts.IPv4_ANY, '1.2.3.4', 'eth0') self.assertEqual('0.0.0.0/0 via 1.2.3.4 dev eth0', route.build_config())
def test_get_config_str(self): ts = keepalived.KeepalivedTrackScript( VRRP_INTERVAL, '/etc/ha_confs/qrouter-x', VRRP_ID) ts.routes = [ keepalived.KeepalivedVirtualRoute('12.0.0.0/24', '10.0.0.1'), ] self.assertEqual(''' track_script { ha_health_check_1 }''', ts.get_config_str())
def _add_extra_subnet_onlink_routes(self, ex_gw_port, interface_name): extra_subnets = ex_gw_port.get('extra_subnets', []) instance = self._get_keepalived_instance() onlink_route_cidrs = set(s['cidr'] for s in extra_subnets) instance.virtual_routes.extra_subnets = [ keepalived.KeepalivedVirtualRoute( onlink_route_cidr, None, interface_name, scope='link') for onlink_route_cidr in onlink_route_cidrs]
def routes_updated(self): new_routes = self.router['routes'] instance = self._get_keepalived_instance() instance.virtual_routes.extra_routes = [ keepalived.KeepalivedVirtualRoute( route['destination'], route['nexthop']) for route in new_routes] self.routes = new_routes
def routes_updated(self, old_routes, new_routes): instance = self._get_keepalived_instance() instance.virtual_routes.extra_routes = [ keepalived.KeepalivedVirtualRoute( route['destination'], route['nexthop']) for route in new_routes] if self.router.get('distributed', False): super(HaRouter, self).routes_updated(old_routes, new_routes) self.keepalived_manager.get_process().reload_cfg()
def _add_default_gw_virtual_route(self, ri, ex_gw_port, interface_name): gw_ip = ex_gw_port['subnet']['gateway_ip'] if gw_ip: instance = ri.keepalived_manager.config.get_instance(ri.ha_vr_id) instance.virtual_routes = ( [route for route in instance.virtual_routes if route.destination != '0.0.0.0/0']) instance.virtual_routes.append( keepalived.KeepalivedVirtualRoute( '0.0.0.0/0', gw_ip, interface_name))
def _process_virtual_routes(self, ri, new_routes): instance = ri.keepalived_manager.config.get_instance(ri.ha_vr_id) # Filter out all of the old routes while keeping only the default route instance.virtual_routes = [route for route in instance.virtual_routes if route.destination == '0.0.0.0/0'] for route in new_routes: instance.virtual_routes.append(keepalived.KeepalivedVirtualRoute( route['destination'], route['nexthop']))
def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): gateway_ips = self._get_external_gw_ips(ex_gw_port) default_gw_rts = [] instance = self._get_keepalived_instance() for subnet in ex_gw_port.get('subnets', []): is_gateway_not_in_subnet = (subnet['gateway_ip'] and not ipam_utils.check_subnet_ip( subnet['cidr'], subnet['gateway_ip'])) if is_gateway_not_in_subnet: default_gw_rts.append(keepalived.KeepalivedVirtualRoute( subnet['gateway_ip'], None, interface_name, scope='link')) for gw_ip in gateway_ips: # TODO(Carl) This is repeated everywhere. A method would # be nice. default_gw = n_consts.IP_ANY[netaddr.IPAddress(gw_ip).version] default_gw_rts.append(keepalived.KeepalivedVirtualRoute( default_gw, gw_ip, interface_name)) instance.virtual_routes.gateway_routes = default_gw_rts
def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): gw_ip = ex_gw_port['subnet']['gateway_ip'] if gw_ip: # TODO(Carl) This is repeated everywhere. A method would be nice. instance = self._get_keepalived_instance() instance.virtual_routes = ( [route for route in instance.virtual_routes if route.destination != '0.0.0.0/0']) instance.virtual_routes.append( keepalived.KeepalivedVirtualRoute( '0.0.0.0/0', gw_ip, interface_name))
def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): default_gw_rts = [] gateway_ips = self._get_external_gw_ips(ex_gw_port) for gw_ip in gateway_ips: # TODO(Carl) This is repeated everywhere. A method would # be nice. default_gw = n_consts.IP_ANY[netaddr.IPAddress(gw_ip).version] instance = self._get_keepalived_instance() default_gw_rts.append(keepalived.KeepalivedVirtualRoute( default_gw, gw_ip, interface_name)) instance.virtual_routes.gateway_routes = default_gw_rts
def _add_default_gw_virtual_route(self, ri, ex_gw_port, interface_name): gw_ip = ex_gw_port['subnet']['gateway_ip'] if gw_ip: default_gw = ('0.0.0.0/0' if netaddr.IPAddress(gw_ip).version == 4 else '::/0') instance = ri.keepalived_manager.config.get_instance(ri.ha_vr_id) instance.virtual_routes = ( [route for route in instance.virtual_routes if route.destination != default_gw]) instance.virtual_routes.append( keepalived.KeepalivedVirtualRoute( default_gw, gw_ip, interface_name))
def test_get_script_str(self): ts = keepalived.KeepalivedTrackScript( VRRP_INTERVAL, '/etc/ha_confs/qrouter-x', VRRP_ID) ts.routes = [ keepalived.KeepalivedVirtualRoute('12.0.0.0/24', '10.0.0.1'), ] ts.vips = [ keepalived.KeepalivedVipAddress('192.168.0.3/18', 'ha-xxx'), ] self.assertEqual("""#!/bin/bash -eu ip a | grep 192.168.0.3 || exit 0 ping -c 1 -w 1 10.0.0.1 1>/dev/null || exit 1""", ts._get_script_str())
def _get_config(self): config = keepalived.KeepalivedConf() instance1 = keepalived.KeepalivedInstance('MASTER', 'eth0', 1, ['169.254.192.0/18'], advert_int=5) instance1.set_authentication('AH', 'pass123') instance1.track_interfaces.append("eth0") vip_address1 = keepalived.KeepalivedVipAddress('192.168.1.0/24', 'eth1', track=False) vip_address2 = keepalived.KeepalivedVipAddress('192.168.2.0/24', 'eth2', track=False) vip_address3 = keepalived.KeepalivedVipAddress('192.168.3.0/24', 'eth2', track=False) vip_address_ex = keepalived.KeepalivedVipAddress('192.168.55.0/24', 'eth10', track=False) instance1.vips.append(vip_address1) instance1.vips.append(vip_address2) instance1.vips.append(vip_address3) instance1.vips.append(vip_address_ex) virtual_route = keepalived.KeepalivedVirtualRoute( n_consts.IPv4_ANY, "192.168.1.1", "eth1") instance1.virtual_routes.gateway_routes = [virtual_route] instance2 = keepalived.KeepalivedInstance('MASTER', 'eth4', 2, ['169.254.192.0/18'], mcast_src_ip='224.0.0.1') instance2.track_interfaces.append("eth4") vip_address1 = keepalived.KeepalivedVipAddress('192.168.3.0/24', 'eth6', track=False) instance2.vips.append(vip_address1) instance2.vips.append(vip_address2) instance2.vips.append(vip_address_ex) config.add_instance(instance1) config.add_instance(instance2) return config
def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): gw_ip = ex_gw_port['subnet']['gateway_ip'] if gw_ip: # TODO(Carl) This is repeated everywhere. A method would be nice. default_gw = (n_consts.IPv4_ANY if netaddr.IPAddress(gw_ip).version == 4 else n_consts.IPv6_ANY) instance = self._get_keepalived_instance() instance.virtual_routes = ([ route for route in instance.virtual_routes if route.destination != default_gw ]) instance.virtual_routes.append( keepalived.KeepalivedVirtualRoute(default_gw, gw_ip, interface_name))
def routes_updated(self): new_routes = self.router['routes'] instance = self._get_keepalived_instance() # Filter out all of the old routes while keeping only the default route instance.virtual_routes = [route for route in instance.virtual_routes if route.destination == '0.0.0.0/0'] for route in new_routes: instance.virtual_routes.append(keepalived.KeepalivedVirtualRoute( route['destination'], route['nexthop'])) self.routes = new_routes
def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): subnets = ex_gw_port.get('subnets', []) default_gw_rts = [] for subnet in subnets: gw_ip = subnet['gateway_ip'] if gw_ip: # TODO(Carl) This is repeated everywhere. A method would # be nice. default_gw = (n_consts.IPv4_ANY if netaddr.IPAddress(gw_ip).version == 4 else n_consts.IPv6_ANY) instance = self._get_keepalived_instance() default_gw_rts.append(keepalived.KeepalivedVirtualRoute( default_gw, gw_ip, interface_name)) instance.virtual_routes.gateway_routes = default_gw_rts
def _add_default_gw_virtual_route(self, ex_gw_port, interface_name): default_gw_rts = [] gateway_ips, enable_ra_on_gw = self._get_external_gw_ips(ex_gw_port) for gw_ip in gateway_ips: # TODO(Carl) This is repeated everywhere. A method would # be nice. default_gw = (n_consts.IPv4_ANY if netaddr.IPAddress(gw_ip).version == 4 else n_consts.IPv6_ANY) instance = self._get_keepalived_instance() default_gw_rts.append(keepalived.KeepalivedVirtualRoute( default_gw, gw_ip, interface_name)) instance.virtual_routes.gateway_routes = default_gw_rts if enable_ra_on_gw: self.driver.configure_ipv6_ra(self.ns_name, interface_name)
def configure(self): config = keepalived.KeepalivedConf() instance1 = keepalived.KeepalivedInstance('MASTER', self.ha_port, 1, ['169.254.192.0/18'], advert_int=5) instance1.track_interfaces.append(self.ha_port) # Configure keepalived with an IPv6 address (gw_vip) on gw_port. vip_addr1 = keepalived.KeepalivedVipAddress(self.gw_vip, self.gw_port) instance1.vips.append(vip_addr1) # Configure keepalived with an IPv6 default route on gw_port. gateway_route = keepalived.KeepalivedVirtualRoute( n_consts.IPv6_ANY, self.default_gw, self.gw_port) instance1.virtual_routes.gateway_routes = [gateway_route] config.add_instance(instance1) self.config = config
def test_virtual_route_without_dev_without_no_track(self): cfg.CONF.set_override('keepalived_use_no_track', False) route = keepalived.KeepalivedVirtualRoute('50.0.0.0/8', '1.2.3.4') self.assertEqual('50.0.0.0/8 via 1.2.3.4', route.build_config())
def test_virtual_route_with_dev(self): route = keepalived.KeepalivedVirtualRoute(n_consts.IPv4_ANY, '1.2.3.4', 'eth0') self.assertEqual('0.0.0.0/0 via 1.2.3.4 dev eth0', route.build_config())
def test_virtual_route_with_dev_without_no_track(self): cfg.CONF.set_override('keepalived_use_no_track', False) route = keepalived.KeepalivedVirtualRoute(n_consts.IPv4_ANY, '1.2.3.4', 'eth0') self.assertEqual('0.0.0.0/0 via 1.2.3.4 dev eth0', route.build_config())
def test_virtual_route_without_dev(self): route = keepalived.KeepalivedVirtualRoute('50.0.0.0/8', '1.2.3.4') self.assertEqual('50.0.0.0/8 via 1.2.3.4', route.build_config())