Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for updating AWS routing table entries #37

Merged
merged 2 commits into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/bosh_aws_cpi/lib/cloud/aws/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ def exists?
@aws_instance.exists? && @aws_instance.status != :terminated
end

def update_routing_tables(route_definitions)
if route_definitions.length > 0
@logger.debug("Associating instance with destinations in the routing tables")
tables = @aws_instance.vpc.route_tables
route_definitions.each do |definition|
@logger.debug("Finding routing table '#{definition["table_id"]}'")
table = tables[definition["table_id"]]
@logger.debug("Sending traffic for '#{definition["destination"]}' to '#{@aws_instance.id}' in '#{definition["table_id"]}'")

existing = false
table.routes.each do |route|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs say routes is a normal array, so could probably shorten this to: existing = table.routes.any ...

if definition["destination"] == route.destination_cidr_block
existing = true
end
end
if existing
table.replace_route(definition["destination"], { :instance => @aws_instance })
else
table.create_route(definition["destination"], { :instance => @aws_instance })
end
end
end
end

def attach_to_load_balancers(load_balancer_ids)
load_balancer_ids.each do |load_balancer_id|
lb = @elb.load_balancers[load_balancer_id]
Expand Down
1 change: 1 addition & 0 deletions src/bosh_aws_cpi/lib/cloud/aws/instance_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def create(agent_id, stemcell_id, resource_pool, networks_spec, disk_locality, e
# attach to a load balancer, the instance must be running.
instance.wait_for_running
instance.attach_to_load_balancers(resource_pool['elbs'] || [])
instance.update_routing_tables(resource_pool['advertised_instance_routes'] || [])
rescue => e
@logger.warn("Failed to configure instance '#{instance.id}': #{e.inspect}")
begin
Expand Down
26 changes: 26 additions & 0 deletions src/bosh_aws_cpi/spec/unit/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,32 @@ module Bosh::AwsCloud
end
end

describe '#update_routing_tables' do
let(:fake_vpc) { instance_double('AWS::EC2::VPC') }
let(:fake_tables) { instance_double('AWS::EC2::RouteTableCollection') }
let(:fake_table) { instance_double('AWS::EC2::RouteTable') }
let(:fake_route) { instance_double('AWS::EC2::Route') }
let(:fake_routes) {[ fake_route ]}

before do
allow(aws_instance).to receive(:vpc).and_return(fake_vpc)
allow(fake_vpc).to receive(:route_tables).and_return(fake_tables)
allow(fake_tables).to receive(:[]).with('r-12345').and_return(fake_table)
allow(fake_table).to receive(:routes).and_return(fake_routes)
allow(fake_route).to receive(:destination_cidr_block).and_return("10.0.0.0/16")
end
it 'updates the routing table entry with the instance ID when finding an existing route' do
destination = "10.0.0.0/16"
expect(fake_table).to receive(:replace_route).with(destination, { :instance => aws_instance })
instance.update_routing_tables [{ "table_id" => "r-12345", "destination" => destination }]
end
it 'creates a routing table entry with the instance ID when the route does not exist' do
destination = "10.5.0.0/16"
expect(fake_table).to receive(:create_route).with(destination, { :instance => aws_instance })
instance.update_routing_tables [{ "table_id" => "r-12345", "destination" => destination }]
end
end

describe '#attach_to_load_balancers' do
before { allow(elb).to receive(:load_balancers).and_return(load_balancers) }
let(:load_balancers) { { 'fake-lb1-id' => load_balancer1, 'fake-lb2-id' => load_balancer2 } }
Expand Down