Skip to content

Commit 8e8d4be

Browse files
author
Thirumalesh Aaraveti
committed
Add spotMarketRequest on marketType spot
1 parent 777e8de commit 8e8d4be

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

exp/api/v1beta2/awsmachinepool_webhook.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"fmt"
2021
"time"
2122

2223
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -201,6 +202,18 @@ func (r *AWSMachinePool) validateInstanceMarketType() field.ErrorList {
201202
if r.Spec.AWSLaunchTemplate.MarketType == v1beta2.MarketTypeCapacityBlock && r.Spec.AWSLaunchTemplate.CapacityReservationID == nil {
202203
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.awsLaunchTemplate.capacityReservationID"), "is required when CapacityBlock is provided"))
203204
}
205+
switch r.Spec.AWSLaunchTemplate.MarketType {
206+
case "", v1beta2.MarketTypeOnDemand, v1beta2.MarketTypeSpot, v1beta2.MarketTypeCapacityBlock:
207+
default:
208+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.awsLaunchTemplate.MarketType"), r.Spec.AWSLaunchTemplate.MarketType, fmt.Sprintf("Valid values are: %s, %s, %s and omitted", v1beta2.MarketTypeOnDemand, v1beta2.MarketTypeSpot, v1beta2.MarketTypeCapacityBlock)))
209+
}
210+
if r.Spec.AWSLaunchTemplate.MarketType == v1beta2.MarketTypeSpot && r.Spec.AWSLaunchTemplate.CapacityReservationID != nil {
211+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.awsLaunchTemplate.marketType"), "setting marketType to Spot and CapacityReservationID cannot be used together"))
212+
}
213+
214+
if r.Spec.AWSLaunchTemplate.CapacityReservationID != nil && r.Spec.AWSLaunchTemplate.SpotMarketOptions != nil {
215+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.awsLaunchTemplate.spotMarketOptions"), "setting spotMarketOptions and CapacityReservationID cannot be used together"))
216+
}
204217

205218
return allErrs
206219
}

exp/api/v1beta2/awsmachinepool_webhook_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,53 @@ func TestAWSMachinePoolValidateCreate(t *testing.T) {
174174
},
175175
wantErr: true,
176176
},
177+
{
178+
name: "with invalid MarketType provided",
179+
pool: &AWSMachinePool{
180+
Spec: AWSMachinePoolSpec{
181+
AWSLaunchTemplate: AWSLaunchTemplate{
182+
MarketType: "invalid",
183+
},
184+
},
185+
},
186+
wantErr: true,
187+
},
188+
{
189+
name: "with MarketType empty value provided",
190+
pool: &AWSMachinePool{
191+
Spec: AWSMachinePoolSpec{
192+
AWSLaunchTemplate: AWSLaunchTemplate{
193+
MarketType: "",
194+
},
195+
},
196+
},
197+
wantErr: false,
198+
},
199+
{
200+
name: "with MarketType Spot and CapacityReservationID value provided",
201+
pool: &AWSMachinePool{
202+
Spec: AWSMachinePoolSpec{
203+
AWSLaunchTemplate: AWSLaunchTemplate{
204+
MarketType: infrav1.MarketTypeSpot,
205+
CapacityReservationID: aws.String("cr-123"),
206+
},
207+
},
208+
},
209+
wantErr: true,
210+
},
211+
{
212+
name: "with CapacityReservationID and SpotMarketOptions value provided",
213+
pool: &AWSMachinePool{
214+
Spec: AWSMachinePoolSpec{
215+
AWSLaunchTemplate: AWSLaunchTemplate{
216+
SpotMarketOptions: &infrav1.SpotMarketOptions{},
217+
CapacityReservationID: aws.String("cr-123"),
218+
},
219+
},
220+
},
221+
wantErr: true,
222+
},
223+
177224
{
178225
name: "invalid, MarketType set to MarketTypeCapacityBlock and spotMarketOptions are specified",
179226
pool: &AWSMachinePool{

pkg/cloud/services/ec2/instances.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,10 @@ func getInstanceMarketOptionsRequest(i *infrav1.Instance) (*ec2.InstanceMarketOp
11541154
return nil, errors.New("can't create spot capacity-blocks, remove spot market request")
11551155
}
11561156

1157+
if (i.MarketType == infrav1.MarketTypeSpot || i.SpotMarketOptions != nil) && i.CapacityReservationID != nil {
1158+
return nil, errors.New("can't create spot capacity-blocks, remove capacityReservationID or remove spot requests")
1159+
}
1160+
11571161
// Infer MarketType if not explicitly set
11581162
if i.SpotMarketOptions != nil && i.MarketType == "" {
11591163
i.MarketType = infrav1.MarketTypeSpot
@@ -1163,6 +1167,10 @@ func getInstanceMarketOptionsRequest(i *infrav1.Instance) (*ec2.InstanceMarketOp
11631167
i.MarketType = infrav1.MarketTypeOnDemand
11641168
}
11651169

1170+
if i.MarketType == infrav1.MarketTypeSpot && i.SpotMarketOptions == nil {
1171+
i.SpotMarketOptions = &infrav1.SpotMarketOptions{}
1172+
}
1173+
11661174
switch i.MarketType {
11671175
case infrav1.MarketTypeCapacityBlock:
11681176
if i.CapacityReservationID == nil {

pkg/cloud/services/ec2/instances_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5770,6 +5770,35 @@ func TestGetInstanceMarketOptionsRequest(t *testing.T) {
57705770
},
57715771
expectedError: nil,
57725772
},
5773+
{
5774+
name: "with an marketType Spot options specified",
5775+
instance: &infrav1.Instance{
5776+
MarketType: infrav1.MarketTypeSpot,
5777+
},
5778+
expectedRequest: &ec2.InstanceMarketOptionsRequest{
5779+
MarketType: aws.String(ec2.MarketTypeSpot),
5780+
SpotOptions: &ec2.SpotMarketOptions{
5781+
InstanceInterruptionBehavior: aws.String(ec2.InstanceInterruptionBehaviorTerminate),
5782+
SpotInstanceType: aws.String(ec2.SpotInstanceTypeOneTime),
5783+
},
5784+
},
5785+
},
5786+
{
5787+
name: "with an marketType Spot and CapacityRerservationID specified",
5788+
instance: &infrav1.Instance{
5789+
MarketType: infrav1.MarketTypeSpot,
5790+
CapacityReservationID: mockCapacityReservationID,
5791+
},
5792+
expectedError: errors.Errorf("can't create spot capacity-blocks, remove capacityReservationID or remove spot requests"),
5793+
},
5794+
{
5795+
name: "with an spotMarketOptions and CapacityRerservationID specified",
5796+
instance: &infrav1.Instance{
5797+
SpotMarketOptions: &infrav1.SpotMarketOptions{},
5798+
CapacityReservationID: mockCapacityReservationID,
5799+
},
5800+
expectedError: errors.Errorf("can't create spot capacity-blocks, remove capacityReservationID or remove spot requests"),
5801+
},
57735802
{
57745803
name: "with an empty MaxPrice specified",
57755804
instance: &infrav1.Instance{

0 commit comments

Comments
 (0)