Skip to content

Commit

Permalink
Merge pull request #144 from drone/CI-13677
Browse files Browse the repository at this point in the history
fix: [CI-14032]: Add option to specify Amazon EC2 instance metadata a…
  • Loading branch information
vistaarjuneja authored Aug 30, 2024
2 parents 6f9bdea + bf6c82e commit 73a7d27
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/drone-autoscaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ func setupProvider(c config.Config) (autoscaler.Provider, error) {
amazon.WithVolumeThroughput(c.Amazon.VolumeThroughput),
amazon.WithIamProfileArn(c.Amazon.IamProfileArn),
amazon.WithMarketType(c.Amazon.MarketType),
amazon.WithInstanceMetadataTokens(c.Amazon.IMDSTokens),
), nil
case os.Getenv("OS_USERNAME") != "":
return openstack.New(
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type (
VolumeThroughput int64 `envconfig:"DRONE_AMAZON_VOLUME_THROUGHPUT"`
IamProfileArn string `envconfig:"DRONE_AMAZON_IAM_PROFILE_ARN"`
MarketType string `envconfig:"DRONE_AMAZON_MARKET_TYPE"`
IMDSTokens string `envconfig:"DRONE_AMAZON_IMDS_TOKENS"`
}

DigitalOcean struct {
Expand Down
8 changes: 8 additions & 0 deletions drivers/amazon/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func (p *provider) create(ctx context.Context, opts autoscaler.InstanceCreateOpt
tags := createCopy(p.tags)
tags["Name"] = opts.Name

var metadataOptions *ec2.InstanceMetadataOptionsRequest
if p.imdsTokens != "" {
metadataOptions = &ec2.InstanceMetadataOptionsRequest{
HttpTokens: aws.String(p.imdsTokens),
}
}

in := &ec2.RunInstancesInput{
KeyName: aws.String(p.key),
ImageId: aws.String(p.image),
Expand All @@ -109,6 +116,7 @@ func (p *provider) create(ctx context.Context, opts autoscaler.InstanceCreateOpt
InstanceMarketOptions: marketOptions,
IamInstanceProfile: iamProfile,
UserData: aws.String(base64.StdEncoding.EncodeToString(buf.Bytes())),
MetadataOptions: metadataOptions,
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
AssociatePublicIpAddress: aws.Bool(!p.privateIP),
Expand Down
7 changes: 7 additions & 0 deletions drivers/amazon/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func WithIamProfileArn(t string) Option {
}
}

// WithInstanceMetadataTokens returns an option to set the instance metadata service tokens requiment.
func WithInstanceMetadataTokens(t string) Option {
return func(p *provider) {
p.imdsTokens = t
}
}

// WithMarketType returns an option to set the instance market type.
func WithMarketType(t string) Option {
return func(p *provider) {
Expand Down
1 change: 1 addition & 0 deletions drivers/amazon/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type provider struct {
tags map[string]string
iamProfileArn string
spotInstance bool
imdsTokens string
}

func (p *provider) getClient() *ec2.EC2 {
Expand Down

0 comments on commit 73a7d27

Please sign in to comment.