Skip to content

Commit

Permalink
Set SpotMarketOptions in launchInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Mar 23, 2020
1 parent cb85b1e commit cd210b3
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions pkg/actuators/machine/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,15 @@ func launchInstance(machine *machinev1.Machine, machineProviderConfig *awsprovid
ImageId: amiID,
InstanceType: aws.String(machineProviderConfig.InstanceType),
// Only a single instance of the AWS instance allowed
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: machineProviderConfig.KeyName,
IamInstanceProfile: iamInstanceProfile,
TagSpecifications: []*ec2.TagSpecification{tagInstance, tagVolume},
NetworkInterfaces: networkInterfaces,
UserData: &userDataEnc,
Placement: placement,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: machineProviderConfig.KeyName,
IamInstanceProfile: iamInstanceProfile,
TagSpecifications: []*ec2.TagSpecification{tagInstance, tagVolume},
NetworkInterfaces: networkInterfaces,
UserData: &userDataEnc,
Placement: placement,
InstanceMarketOptions: getInstanceMarketOptionsRequest(machineProviderConfig),
}

if len(blockDeviceMappings) > 0 {
Expand Down Expand Up @@ -374,3 +375,33 @@ func (il instanceList) Less(i, j int) bool {
func sortInstances(instances []*ec2.Instance) {
sort.Sort(instanceList(instances))
}

func getInstanceMarketOptionsRequest(providerConfig *awsproviderv1.AWSMachineProviderConfig) *ec2.InstanceMarketOptionsRequest {
if providerConfig.SpotMarketOptions == nil {
// Instance is not a Spot instance
return nil
}

// Set required values for Spot instances
spotOptions := &ec2.SpotMarketOptions{}
// The following two options ensure that:
// - If an instance is interrupted, it is terminated rather than hibernating or stopping
// - No replacement instance will be created if the instance is interrupted
// - If the spot request cannot immediately be fulfilled, it will not be created
// This behaviour should satisfy the 1:1 mapping of Machines to Instances as
// assumed by the machine API.
spotOptions.SetInstanceInterruptionBehavior(ec2.InstanceInterruptionBehaviorTerminate)
spotOptions.SetSpotInstanceType(ec2.SpotInstanceTypeOneTime)

// Set the MaxPrice if specified by the providerConfig
maxPrice := providerConfig.SpotMarketOptions.MaxPrice
if maxPrice != nil && *maxPrice != "" {
spotOptions.SetMaxPrice(*maxPrice)
}

instanceMarketOptionsRequest := &ec2.InstanceMarketOptionsRequest{}
instanceMarketOptionsRequest.SetMarketType(ec2.MarketTypeSpot)
instanceMarketOptionsRequest.SetSpotOptions(spotOptions)

return instanceMarketOptionsRequest
}

0 comments on commit cd210b3

Please sign in to comment.