in pkg/cloudprovider/aws/instance.go [108:149]
func (p *InstanceProvider) launchInstances(ctx context.Context, constraints *v1alpha1.Constraints, instanceTypes []cloudprovider.InstanceType, quantity int) ([]*string, error) {
capacityType := p.getCapacityType(constraints, instanceTypes)
// Get Launch Template Configs, which may differ due to GPU or Architecture requirements
launchTemplateConfigs, err := p.getLaunchTemplateConfigs(ctx, constraints, instanceTypes, capacityType)
if err != nil {
return nil, fmt.Errorf("getting launch template configs, %w", err)
}
// Create fleet
createFleetInput := &ec2.CreateFleetInput{
Type: aws.String(ec2.FleetTypeInstant),
LaunchTemplateConfigs: launchTemplateConfigs,
TargetCapacitySpecification: &ec2.TargetCapacitySpecificationRequest{
DefaultTargetCapacityType: aws.String(capacityType),
TotalTargetCapacity: aws.Int64(int64(quantity)),
},
TagSpecifications: []*ec2.TagSpecification{
{
ResourceType: aws.String(ec2.ResourceTypeInstance),
Tags: v1alpha1.MergeTags(ctx, constraints.Tags, map[string]string{fmt.Sprintf("kubernetes.io/cluster/%s", injection.GetOptions(ctx).ClusterName): "owned"}),
},
},
}
if capacityType == v1alpha1.CapacityTypeSpot {
createFleetInput.SpotOptions = &ec2.SpotOptionsRequest{AllocationStrategy: aws.String(ec2.SpotAllocationStrategyCapacityOptimizedPrioritized)}
} else {
createFleetInput.OnDemandOptions = &ec2.OnDemandOptionsRequest{AllocationStrategy: aws.String(ec2.FleetOnDemandAllocationStrategyLowestPrice)}
}
createFleetOutput, err := p.ec2api.CreateFleetWithContext(ctx, createFleetInput)
if err != nil {
return nil, fmt.Errorf("creating fleet %w", err)
}
p.updateUnavailableOfferingsCache(ctx, createFleetOutput.Errors, capacityType)
instanceIds := combineFleetInstances(*createFleetOutput)
if len(instanceIds) == 0 {
return nil, combineFleetErrors(createFleetOutput.Errors)
} else if len(instanceIds) != quantity {
logging.FromContext(ctx).Errorf("Failed to launch %d EC2 instances out of the %d EC2 instances requested: %s",
quantity-len(instanceIds), quantity, combineFleetErrors(createFleetOutput.Errors).Error())
}
return instanceIds, nil
}