def filter_nodes_by_blacklist( nodes: Sequence[V1Node], blacklist: DeployBlacklist, whitelist: DeployWhitelist, ) -> Sequence[V1Node]: """Takes an input list of nodes and filters them based on the given blacklist. The blacklist is in the form of: [["location_type", "location]] Where the list inside is something like ["region", "uswest1-prod"] :returns: The list of nodes after the filter """ if whitelist: whitelist = (maybe_add_yelp_prefix(whitelist[0]), whitelist[1]) blacklist = [(maybe_add_yelp_prefix(entry[0]), entry[1]) for entry in blacklist] return [ node for node in nodes if host_passes_whitelist( node.metadata.labels, whitelist, ) and host_passes_blacklist( node.metadata.labels, blacklist, ) ]
def test_host_passes_blacklist_passes(): slave_attributes = { 'fake_attribute': 'fake_value_1', } blacklist = [["fake_attribute", "No what we have here"], ['foo', 'bar']] actual = long_running_service_tools.host_passes_blacklist(host_attributes=slave_attributes, blacklist=blacklist) assert actual is True
def test_host_passes_blacklist_blocks_blacklisted_locations(): slave_attributes = { 'fake_attribute': 'fake_value_1', } blacklist = [["fake_attribute", "fake_value_1"]] actual = long_running_service_tools.host_passes_blacklist(host_attributes=slave_attributes, blacklist=blacklist) assert actual is False
def test_host_passes_blacklist_passes(): slave_attributes = {"fake_attribute": "fake_value_1"} blacklist = [("fake_attribute", "No what we have here"), ("foo", "bar")] actual = long_running_service_tools.host_passes_blacklist( host_attributes=slave_attributes, blacklist=blacklist ) assert actual is True
def filter_mesos_slaves_by_blacklist(slaves, blacklist: DeployBlacklist, whitelist: DeployWhitelist): """Takes an input list of slaves and filters them based on the given blacklist. The blacklist is in the form of: [["location_type", "location]] Where the list inside is something like ["region", "uswest1-prod"] :returns: The list of mesos slaves after the filter """ filtered_slaves = [] for slave in slaves: if host_passes_blacklist(slave["attributes"], blacklist) and host_passes_whitelist( slave["attributes"], whitelist): filtered_slaves.append(slave) return filtered_slaves