Skip to content

Commit

Permalink
Merge pull request cloudfoundry#105 from tlwr/main
Browse files Browse the repository at this point in the history
CPI tags work for spot instances
  • Loading branch information
mrosecrance authored Jul 15, 2020
2 parents ee1fa1f + c90ddaf commit f67db6e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/bosh_aws_cpi/lib/cloud/aws/spot_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def initialize(ec2)
end

def create(launch_specification, spot_bid_price)
launch_spec_without_tag_specs = launch_specification.reject { |k,_| k == :tag_specifications }.to_h

spot_request_spec = {
spot_price: "#{spot_bid_price}",
instance_count: 1,
launch_specification: launch_specification
launch_specification: launch_spec_without_tag_specs
}
unless launch_specification[:security_groups].nil?
message = 'Cannot use security group names when creating spot instances'
Expand All @@ -36,7 +38,22 @@ def create(launch_specification, spot_bid_price)
raise Bosh::Clouds::VMCreationFailed.new(false), message
end

wait_for_spot_instance
instance = wait_for_spot_instance

if launch_specification.key? :tag_specifications
tags = launch_specification[:tag_specifications]
.flat_map { |ts| ts[:tags] }
.map { |t| [t[:key], t[:value]] }
.to_h

begin
TagManager.create_tags(instance, tags)
rescue Aws::EC2::Errors::TagLimitExceeded => e
logger.error("could not tag #{instance.id}: #{e.message}")
end
end

instance
end

private
Expand Down
25 changes: 25 additions & 0 deletions src/bosh_aws_cpi/spec/unit/spot_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,30 @@
expect(error.ok_to_retry).to eq false
}
end

context 'and tag_specifications are present' do
let(:fake_instance_params_with_tags) do
{
tag_specifications: [{ tags: { key: 'tag', value: 'tag_value'} }]
}.merge(fake_instance_params)
end

let(:expected_tags) do
{ 'tag' => 'tag_value' }
end

before do
expect(aws_client).to receive(:describe_spot_instance_requests).and_return(describe_spot_instance_requests_result)

expect(spot_instance_requests[0]).to receive(:state).and_return('active')
expect(spot_instance_requests[0]).to receive(:instance_id).and_return('i-12345678')

expect(Bosh::AwsCloud::TagManager).to receive(:create_tags).with(instance, expected_tags)
end

it 'should not pass tag specifications to the ec2 client' do
expect(spot_manager.create(fake_instance_params_with_tags, 0.24)).to be(instance)
end
end
end
end

0 comments on commit f67db6e

Please sign in to comment.