Skip to content

Commit

Permalink
[autoscaler][aws] Use subnets in only one VPC (#14868)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriGekhtman authored Mar 24, 2021
1 parent 52cfa1c commit 1045856
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
9 changes: 8 additions & 1 deletion python/ray/autoscaler/_private/aws/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,14 @@ def _configure_subnet(config):
"to populate the list of subnets and trying this again.".
format(config["provider"]["availability_zone"]))

subnet_ids = [s.subnet_id for s in subnets]
# Use subnets in only one VPC, so that _configure_security_groups only
# needs to create a security group in this one VPC. Otherwise, we'd need
# to set up security groups in all of the user's VPCs and set up networking
# rules to allow traffic between these groups.
# See https://github.com/ray-project/ray/pull/14868.
subnet_ids = [
s.subnet_id for s in subnets if s.vpc_id == subnets[0].vpc_id
]
if "SubnetIds" not in config["head_node"]:
_set_config_info(head_subnet_src="default")
config["head_node"]["SubnetIds"] = subnet_ids
Expand Down
61 changes: 60 additions & 1 deletion python/ray/tests/aws/test_autoscaler_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,66 @@
from ray.tests.aws.utils.constants import AUX_SUBNET, DEFAULT_SUBNET, \
DEFAULT_SG_AUX_SUBNET, DEFAULT_SG, DEFAULT_SG_DUAL_GROUP_RULES, \
DEFAULT_SG_WITH_RULES_AUX_SUBNET, AUX_SG, \
DEFAULT_SG_WITH_NAME, DEFAULT_SG_WITH_NAME_AND_RULES, CUSTOM_IN_BOUND_RULES
DEFAULT_SG_WITH_RULES, DEFAULT_SG_WITH_NAME, \
DEFAULT_SG_WITH_NAME_AND_RULES, CUSTOM_IN_BOUND_RULES


def test_use_subnets_in_only_one_vpc(iam_client_stub, ec2_client_stub):
"""
This test validates that when bootstrap_aws populates the SubnetIds field,
all of the subnets used belong to the same VPC, and that a SecurityGroup
in that VPC is correctly configured.
"""
stubs.configure_iam_role_default(iam_client_stub)
stubs.configure_key_pair_default(ec2_client_stub)

# Add a response with a thousand subnets all in different VPCs.
# After filtering, only subnet in one particular VPC should remain.
# Thus head_node.SubnetIds and worker_nodes.SubnetIds should end up as
# being length-one lists after the bootstrap_config.
stubs.describe_a_thousand_subnets_in_different_vpcs(ec2_client_stub)

# describe the subnet in use while determining its vpc
stubs.describe_subnets_echo(ec2_client_stub, DEFAULT_SUBNET)
# given no existing security groups within the VPC...
stubs.describe_no_security_groups(ec2_client_stub)
# expect to create a security group on the VPC
stubs.create_sg_echo(ec2_client_stub, DEFAULT_SG)
# expect new security group details to be retrieved after creation
stubs.describe_sgs_on_vpc(
ec2_client_stub,
[DEFAULT_SUBNET["VpcId"]],
[DEFAULT_SG],
)

# given no existing default security group inbound rules...
# expect to authorize all default inbound rules
stubs.authorize_sg_ingress(
ec2_client_stub,
DEFAULT_SG_WITH_RULES,
)

# expect another call to describe the above security group while checking
# a second time if it has ip_permissions set ("if not sg.ip_permissions")
stubs.describe_an_sg_2(
ec2_client_stub,
DEFAULT_SG_WITH_RULES,
)

# given our mocks and an example config file as input...
# expect the config to be loaded, validated, and bootstrapped successfully
config = helpers.bootstrap_aws_example_config_file("example-full.yaml")
_get_vpc_id_or_die.cache_clear()

# We've filtered down to only one subnet id -- only one of the thousand
# subnets generated by ec2.subnets.all() belongs to the right VPC.
assert config["head_node"]["SubnetIds"] == [DEFAULT_SUBNET["SubnetId"]]
assert config["worker_nodes"]["SubnetIds"] == [DEFAULT_SUBNET["SubnetId"]]
# Check that the security group has been filled correctly.
assert config["head_node"]["SecurityGroupIds"] == [DEFAULT_SG["GroupId"]]
assert config["worker_nodes"]["SecurityGroupIds"] == [
DEFAULT_SG["GroupId"]
]


def test_create_sg_different_vpc_same_rules(iam_client_stub, ec2_client_stub):
Expand Down
12 changes: 12 additions & 0 deletions python/ray/tests/aws/utils/stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ def describe_a_security_group(ec2_client_stub, security_group):
service_response={"SecurityGroups": [security_group]})


def describe_an_sg_2(ec2_client_stub, security_group):
"""Same as last function, different input param format.
A call with this input parameter format is made when sg.ip_permissions is
accessed in aws/config.py.
"""
ec2_client_stub.add_response(
"describe_security_groups",
expected_params={"GroupIds": [security_group["GroupId"]]},
service_response={"SecurityGroups": [security_group]})


def create_sg_echo(ec2_client_stub, security_group):
ec2_client_stub.add_response(
"create_security_group",
Expand Down

0 comments on commit 1045856

Please sign in to comment.