From 44911090210f3c389d9a75e1d34913782eca6f8f Mon Sep 17 00:00:00 2001
From: Ruben <328418-RubenNL@users.noreply.gitlab.com>
Date: Tue, 23 Jan 2024 20:46:52 +0000
Subject: [PATCH] AWS Credit Specification support

---
 docs/drivers/aws.md            |  2 ++
 drivers/amazonec2/amazonec2.go | 11 +++++++++++
 2 files changed, 13 insertions(+)

diff --git a/docs/drivers/aws.md b/docs/drivers/aws.md
index 512b0ba7..27b5cf98 100644
--- a/docs/drivers/aws.md
+++ b/docs/drivers/aws.md
@@ -74,6 +74,7 @@ You can use environment variables:
 -   `--amazonec2-use-ebs-optimized-instance`: Create an EBS Optimized Instance, instance type must support it.
 -   `--amazonec2-ssh-keypath`: Path to Private Key file to use for instance. Matching public key with .pub extension should exist
 -   `--amazonec2-retries`:  Set retry count for recoverable failures (use -1 to disable)
+-   `--amazonec2-credit-specification`: The credit option for CPU usage of the instance. Valid values: `standard`, `unlimited`. Defaults to [what AWS sets for the instance type](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/burstable-performance-instances-unlimited-mode.html). Only supported on [burstable instance types](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/burstable-performance-instances.html#burstable-instance-types)
 
 
 #### Environment variables and default values:
@@ -106,6 +107,7 @@ You can use environment variables:
 | `--amazonec2-use-ebs-optimized-instance` | -                       | `false`          |
 | `--amazonec2-ssh-keypath`                | `AWS_SSH_KEYPATH`       | -                |
 | `--amazonec2-retries`                    | -                       | `5`              |
+| `--amazonec2-credit-specification`       | -                       | -                |
 
 ## Default AMIs
 
diff --git a/drivers/amazonec2/amazonec2.go b/drivers/amazonec2/amazonec2.go
index 046cf2d8..0c7bacb6 100644
--- a/drivers/amazonec2/amazonec2.go
+++ b/drivers/amazonec2/amazonec2.go
@@ -118,6 +118,7 @@ type Driver struct {
 	UserDataFile                  string
 	MetadataTokenSetting          string
 	MetadataTokenResponseHopLimit int64
+	CreditSpecification           string
 }
 
 type clientFactory interface {
@@ -309,6 +310,10 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
 			Usage: "The number of network hops that the metadata token can travel",
 			Value: defaultMetadataTokenResponseHopLimit,
 		},
+		mcnflag.StringFlag{
+			Name:  "amazonec2-credit-specification",
+			Usage: "The credit option for CPU usage (unlimited or standard)",
+		},
 	}
 }
 
@@ -414,6 +419,7 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
 	d.MetadataTokenSetting = flags.String("amazonec2-metadata-token")
 	d.MetadataTokenResponseHopLimit = int64(flags.Int("amazonec2-metadata-token-response-hop-limit"))
 	d.DisableSSL = flags.Bool("amazonec2-insecure-transport")
+	d.CreditSpecification = strings.TrimSpace(flags.String("amazonec2-credit-specification"))
 
 	if d.DisableSSL && d.Endpoint == "" {
 		return errorDisableSSLWithoutCustomEndpoint
@@ -696,6 +702,11 @@ func (d *Driver) innerCreate() error {
 		BlockDeviceMappings: []*ec2.BlockDeviceMapping{bdm},
 		UserData:            &userdata,
 	}
+	if d.CreditSpecification != "" {
+		req.CreditSpecification = &ec2.CreditSpecificationRequest{
+			CpuCredits: &d.CreditSpecification,
+		}
+	}
 	if d.RequestSpotInstance {
 		req.InstanceMarketOptions = &ec2.InstanceMarketOptionsRequest{
 			MarketType: aws.String("spot"),
-- 
GitLab