def _build_envoy_location_dict_for_backends( envoy_host: str, registration: str, tasks: Sequence[MarathonTask], location: str, should_return_individual_backends: bool, ) -> MutableMapping[str, Any]: backends = envoy_tools.get_backends( registration, envoy_host=envoy_host, envoy_admin_port=settings.system_paasta_config.get_envoy_admin_port(), ) sorted_envoy_backends = sorted( [backend[0] for backend in backends], key=lambda backend: backend["eds_health_status"], ) casper_proxied_backends = { (backend["address"], backend["port_value"]) for backend, is_casper_proxied_backend in backends if is_casper_proxied_backend } matched_envoy_backends_and_tasks = envoy_tools.match_backends_and_tasks( sorted_envoy_backends, tasks, ) return envoy_tools.build_envoy_location_dict( location, matched_envoy_backends_and_tasks, should_return_individual_backends, casper_proxied_backends, )
def test_get_backends(): testdir = os.path.dirname(os.path.realpath(__file__)) testdata = os.path.join(testdir, "envoy_admin_clusters_snapshot.txt") with open(testdata, "r") as fd: mock_envoy_admin_clusters_data = json.load(fd) mock_response = mock.Mock() mock_response.json.return_value = mock_envoy_admin_clusters_data mock_get = mock.Mock(return_value=(mock_response)) hosts = { "10.46.6.90": ("host2.two.com", None, None), "10.46.6.88": ("host3.three.com", None, None), "10.46.6.103": ("host4.four.com", None, None), } with mock.patch.object(requests.Session, "get", mock_get): with mock.patch( "socket.gethostbyaddr", side_effect=lambda x: hosts[x], autospec=True, ): expected = { "service1.main": [ ( { "address": "10.46.6.88", "port_value": 13833, "hostname": "host3", "eds_health_status": "HEALTHY", "weight": 1, }, False, ), ( { "address": "10.46.6.90", "port_value": 13833, "hostname": "host2", "eds_health_status": "HEALTHY", "weight": 1, }, False, ), ] } assert expected == get_backends("service1.main", "host", 123, "something")
def _build_envoy_location_dict( envoy_host: str, envoy_admin_port: int, envoy_admin_endpoint_format: str, registration: str, pods: Iterable[V1Pod], location: str, should_return_individual_backends: bool, ) -> MutableMapping[str, Any]: backends = envoy_tools.get_backends( registration, envoy_host=envoy_host, envoy_admin_port=envoy_admin_port, envoy_admin_endpoint_format=envoy_admin_endpoint_format, ) sorted_envoy_backends = sorted( [ backend[0] for _, service_backends in backends.items() for backend in service_backends ], key=lambda backend: backend["eds_health_status"], ) casper_proxied_backends = { (backend["address"], backend["port_value"]) for _, service_backends in backends.items() for backend, is_casper_proxied_backend in service_backends if is_casper_proxied_backend } matched_envoy_backends_and_pods = envoy_tools.match_backends_and_pods( sorted_envoy_backends, pods, ) return envoy_tools.build_envoy_location_dict( location, matched_envoy_backends_and_pods, should_return_individual_backends, casper_proxied_backends, )