From 62bdc0bbe4197a4915368721e91d5f543079b151 Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Thu, 24 Nov 2022 12:32:37 +0100 Subject: [PATCH] WIP: enable EC2 Spot usage in AWS Change-Id: I9868d014991d78e7b2421439403ae1371b33524c --- doc/source/aws.rst | 8 ++++++++ nodepool/driver/aws/adapter.py | 10 ++++++++++ nodepool/driver/aws/config.py | 2 ++ 3 files changed, 20 insertions(+) diff --git a/doc/source/aws.rst b/doc/source/aws.rst index 1dae2fd1d..d907e135a 100644 --- a/doc/source/aws.rst +++ b/doc/source/aws.rst @@ -66,6 +66,7 @@ Selecting the ``aws`` driver adds the following options to the cloud-image: debian9 instance-type: t3.large key-name: zuul + use-spot: True tags: key1: value1 key2: value2 @@ -717,3 +718,10 @@ Selecting the ``aws`` driver adds the following options to the .. _`VM Import/Export service role`: https://docs.aws.amazon.com/vm-import/latest/userguide/vmie_prereqs.html#vmimport-role .. _`instance quotas`: https://us-west-1.console.aws.amazon.com/servicequotas/home/services/ec2/quotas .. _`AWS RegisterImage API documentation`: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RegisterImage.html + + .. attr:: use-spot + :type: bool + :default: False + + When set to True, nodepool will try to launch an EC2 Spot + instance, instead of an On-Demand instance. \ No newline at end of file diff --git a/nodepool/driver/aws/adapter.py b/nodepool/driver/aws/adapter.py index bce81df6a..b109cb321 100644 --- a/nodepool/driver/aws/adapter.py +++ b/nodepool/driver/aws/adapter.py @@ -834,6 +834,16 @@ class AwsAdapter(statemachine.Adapter): del mapping['Ebs']['Encrypted'] args['BlockDeviceMappings'] = [mapping] + # enable EC2 Spot + if label.use_spot: + args['InstanceMarketOptions'] = { + 'MarketType': 'spot', + 'SpotOptions': { + 'SpotInstanceType': 'one-time', + 'InstanceInterruptionBehavior': 'terminate' + } + } + with self.rate_limiter(log.debug, "Created instance"): log.debug(f"Creating VM {hostname}") instances = self.ec2.create_instances(**args) diff --git a/nodepool/driver/aws/config.py b/nodepool/driver/aws/config.py index 2d281ead7..2756b2101 100644 --- a/nodepool/driver/aws/config.py +++ b/nodepool/driver/aws/config.py @@ -177,6 +177,7 @@ class AwsLabel(ConfigValue): self.tags = label.get('tags', {}) self.dynamic_tags = label.get('dynamic-tags', {}) self.host_key_checking = self.pool.host_key_checking + self.use_spot = bool(label.get('use-spot', False)) @staticmethod def getSchema(): @@ -198,6 +199,7 @@ class AwsLabel(ConfigValue): }, 'tags': dict, 'dynamic-tags': dict, + 'use-spot': bool }