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

feat: allow customizing spot allocation strategy #481

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions .web-docs/components/builder/ebs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,12 @@ JSON example:
criteria provided in `source_ami_filter`; this pins the AMI returned by the
filter, but will cause Packer to fail if the `source_ami` does not exist.

- `spot_allocation_strategy` (string) - One of `price-capacity-optimized`, `capacity-optimized`, `diversified` or `lowest-price`.
The strategy that determines how to allocate the target Spot Instance capacity
across the Spot Instance pools specified by the EC2 Fleet launch configuration.
If this option is not set/set to invalid value, Packer will use default option provided by the SDK (currently `lowest-price`).
For more information, see [Amazon EC2 User Guide] (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html)

- `spot_instance_types` ([]string) - a list of acceptable instance
types to run your build on. We will request a spot instance using the max
price of spot_price and the allocation strategy of "lowest price".
Expand Down
6 changes: 6 additions & 0 deletions .web-docs/components/builder/ebssurrogate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,12 @@ JSON example:
criteria provided in `source_ami_filter`; this pins the AMI returned by the
filter, but will cause Packer to fail if the `source_ami` does not exist.

- `spot_allocation_strategy` (string) - One of `price-capacity-optimized`, `capacity-optimized`, `diversified` or `lowest-price`.
The strategy that determines how to allocate the target Spot Instance capacity
across the Spot Instance pools specified by the EC2 Fleet launch configuration.
If this option is not set/set to invalid value, Packer will use default option provided by the SDK (currently `lowest-price`).
For more information, see [Amazon EC2 User Guide] (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html)

- `spot_instance_types` ([]string) - a list of acceptable instance
types to run your build on. We will request a spot instance using the max
price of spot_price and the allocation strategy of "lowest price".
Expand Down
6 changes: 6 additions & 0 deletions .web-docs/components/builder/ebsvolume/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,12 @@ https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concept
criteria provided in `source_ami_filter`; this pins the AMI returned by the
filter, but will cause Packer to fail if the `source_ami` does not exist.

- `spot_allocation_strategy` (string) - One of `price-capacity-optimized`, `capacity-optimized`, `diversified` or `lowest-price`.
The strategy that determines how to allocate the target Spot Instance capacity
across the Spot Instance pools specified by the EC2 Fleet launch configuration.
If this option is not set/set to invalid value, Packer will use default option provided by the SDK (currently `lowest-price`).
For more information, see [Amazon EC2 User Guide] (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html)

- `spot_instance_types` ([]string) - a list of acceptable instance
types to run your build on. We will request a spot instance using the max
price of spot_price and the allocation strategy of "lowest price".
Expand Down
6 changes: 6 additions & 0 deletions .web-docs/components/builder/instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,12 @@ JSON example:
criteria provided in `source_ami_filter`; this pins the AMI returned by the
filter, but will cause Packer to fail if the `source_ami` does not exist.

- `spot_allocation_strategy` (string) - One of `price-capacity-optimized`, `capacity-optimized`, `diversified` or `lowest-price`.
The strategy that determines how to allocate the target Spot Instance capacity
across the Spot Instance pools specified by the EC2 Fleet launch configuration.
If this option is not set/set to invalid value, Packer will use default option provided by the SDK (currently `lowest-price`).
For more information, see [Amazon EC2 User Guide] (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html)

- `spot_instance_types` ([]string) - a list of acceptable instance
types to run your build on. We will request a spot instance using the max
price of spot_price and the allocation strategy of "lowest price".
Expand Down
6 changes: 6 additions & 0 deletions builder/common/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ type RunConfig struct {
// criteria provided in `source_ami_filter`; this pins the AMI returned by the
// filter, but will cause Packer to fail if the `source_ami` does not exist.
SourceAmiFilter AmiFilterOptions `mapstructure:"source_ami_filter" required:"false"`
// One of `price-capacity-optimized`, `capacity-optimized`, `diversified` or `lowest-price`.
// The strategy that determines how to allocate the target Spot Instance capacity
// across the Spot Instance pools specified by the EC2 Fleet launch configuration.
// If this option is not set/set to invalid value, Packer will use default option provided by the SDK (currently `lowest-price`).
// For more information, see [Amazon EC2 User Guide] (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html)
vucong2409 marked this conversation as resolved.
Show resolved Hide resolved
SpotAllocationStrategy string `mapstructure:"spot_allocation_strategy" required:"false"`
// a list of acceptable instance
// types to run your build on. We will request a spot instance using the max
// price of spot_price and the allocation strategy of "lowest price".
Expand Down
8 changes: 8 additions & 0 deletions builder/common/step_run_spot_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/ioutil"
"log"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -47,6 +48,7 @@ type StepRunSpotInstance struct {
InstanceType string
Region string
SourceAMI string
SpotAllocationStrategy string
SpotPrice string
SpotTags map[string]string
SpotInstanceTypes []string
Expand Down Expand Up @@ -383,6 +385,12 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
Type: aws.String("instant"),
}

if slices.Contains(ec2.SpotAllocationStrategy_Values(), s.SpotAllocationStrategy) {
vucong2409 marked this conversation as resolved.
Show resolved Hide resolved
createFleetInput.SpotOptions = &ec2.SpotOptionsRequest{
AllocationStrategy: aws.String(s.SpotAllocationStrategy),
}
}

fleetTags, err := TagMap(s.FleetTags).EC2Tags(s.Ctx, s.Region, state)
if err != nil {
err := fmt.Errorf("Error generating fleet tags: %s", err)
Expand Down
1 change: 1 addition & 0 deletions builder/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
SpotTags: b.config.SpotTags,
Tags: b.config.RunTags,
SpotInstanceTypes: b.config.SpotInstanceTypes,
SpotAllocationStrategy: b.config.RunConfig.SpotAllocationStrategy,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this setting should be replicated to other builders, assuming this is compatible with them.
Otherwise I would suggest either:

  1. moving that argument to builder/ebs/config.go so it is not common
  2. checking that it is not set in the configs for the builders that aren't compatible with it, this can be done in the Prepare function for each builder.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if the ebs builder's the only one to support spot instances, I would suggest going with approach 1

UserData: b.config.UserData,
UserDataFile: b.config.UserDataFile,
VolumeTags: b.config.VolumeRunTags,
Expand Down
2 changes: 2 additions & 0 deletions builder/ebs/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions builder/ebssurrogate/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions builder/ebsvolume/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions builder/instance/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs-partials/builder/common/RunConfig-not-required.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@
criteria provided in `source_ami_filter`; this pins the AMI returned by the
filter, but will cause Packer to fail if the `source_ami` does not exist.

- `spot_allocation_strategy` (string) - One of `price-capacity-optimized`, `capacity-optimized`, `diversified` or `lowest-price`.
The strategy that determines how to allocate the target Spot Instance capacity
across the Spot Instance pools specified by the EC2 Fleet launch configuration.
If this option is not set/set to invalid value, Packer will use default option provided by the SDK (currently `lowest-price`).
For more information, see [Amazon EC2 User Guide] (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-allocation-strategy.html)

- `spot_instance_types` ([]string) - a list of acceptable instance
types to run your build on. We will request a spot instance using the max
price of spot_price and the allocation strategy of "lowest price".
Expand Down