Skip to content
Open
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
19 changes: 19 additions & 0 deletions builder/common/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ type RunConfig struct {
// Provide the EC2 Capacity Reservation Group ARN that will be used by
// Packer.
CapacityReservationGroupArn string `mapstructure:"capacity_reservation_group_arn" required:"false"`
// The market type to use when launching instances with capacity reservations.
// Valid values are: `interruptible-capacity-reservation`, `capacity-block`, or empty string (for on-demand).
// Only set this when using interruptible capacity reservations or capacity blocks.
// Leave empty for standard on-demand capacity reservations. Defaults to empty string.
CapacityReservationMarketType string `mapstructure:"capacity_reservation_market_type" required:"false"`

// Packer normally stops the build instance after all provisioners have
// run. For Windows instances, it is sometimes desirable to [run
Expand Down Expand Up @@ -924,6 +929,20 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
errs = append(errs, fmt.Errorf(`capacity_reservation_preference only accepts 'none' or 'open' values`))
}

// Validate capacity_reservation_market_type
if c.CapacityReservationMarketType != "" {
switch c.CapacityReservationMarketType {
case "interruptible-capacity-reservation", "capacity-block":
// Valid market types
default:
errs = append(errs, fmt.Errorf(`capacity_reservation_market_type only accepts 'interruptible-capacity-reservation' or 'capacity-block' values`))
}
// Market type should only be used with a specific capacity reservation
if c.CapacityReservationId == "" && c.CapacityReservationGroupArn == "" {
errs = append(errs, fmt.Errorf(`capacity_reservation_market_type requires either capacity_reservation_id or capacity_reservation_group_arn to be set`))
}
}

var tenancy string
tenancies := []string{c.Placement.Tenancy, c.Tenancy}

Expand Down
11 changes: 11 additions & 0 deletions builder/common/step_run_source_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type StepRunSourceInstance struct {
CapacityReservationPreference string
CapacityReservationId string
CapacityReservationGroupArn string
CapacityReservationMarketType string
Comm *communicator.Config
Ctx interpolate.Context
Debug bool
Expand Down Expand Up @@ -258,6 +259,9 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
}

if s.CapacityReservationId != "" || s.CapacityReservationGroupArn != "" {
if runOpts.CapacityReservationSpecification == nil {
runOpts.CapacityReservationSpecification = &ec2.CapacityReservationSpecification{}
}
runOpts.CapacityReservationSpecification.CapacityReservationTarget = &ec2.CapacityReservationTarget{}

if s.CapacityReservationId != "" {
Expand All @@ -267,6 +271,13 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
if s.CapacityReservationGroupArn != "" {
runOpts.CapacityReservationSpecification.CapacityReservationTarget.CapacityReservationResourceGroupArn = aws.String(s.CapacityReservationGroupArn)
}

// Set market type if specified for interruptible capacity reservations or capacity blocks
if s.CapacityReservationMarketType != "" {
runOpts.InstanceMarketOptions = &ec2.InstanceMarketOptionsRequest{
MarketType: aws.String(s.CapacityReservationMarketType),
}
}
}

if s.HostResourceGroupArn != "" {
Expand Down
1 change: 1 addition & 0 deletions builder/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
CapacityReservationPreference: b.config.CapacityReservationPreference,
CapacityReservationId: b.config.CapacityReservationId,
CapacityReservationGroupArn: b.config.CapacityReservationGroupArn,
CapacityReservationMarketType: b.config.CapacityReservationMarketType,
Comm: &b.config.RunConfig.Comm,
Ctx: b.config.ctx,
Debug: b.config.PackerDebug,
Expand Down
136 changes: 69 additions & 67 deletions builder/ebs/builder.hcl2spec.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions builder/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
CapacityReservationPreference: b.config.CapacityReservationPreference,
CapacityReservationId: b.config.CapacityReservationId,
CapacityReservationGroupArn: b.config.CapacityReservationGroupArn,
CapacityReservationMarketType: b.config.CapacityReservationMarketType,
Comm: &b.config.RunConfig.Comm,
Ctx: b.config.ctx,
Debug: b.config.PackerDebug,
Expand Down
80 changes: 41 additions & 39 deletions builder/ebssurrogate/builder.hcl2spec.go

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

1 change: 1 addition & 0 deletions builder/ebsvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
CapacityReservationPreference: b.config.CapacityReservationPreference,
CapacityReservationId: b.config.CapacityReservationId,
CapacityReservationGroupArn: b.config.CapacityReservationGroupArn,
CapacityReservationMarketType: b.config.CapacityReservationMarketType,
Comm: &b.config.RunConfig.Comm,
Ctx: b.config.ctx,
Debug: b.config.PackerDebug,
Expand Down
Loading