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

CPI tags work for spot instances #105

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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