Skip to content

Commit cdc3ea7

Browse files
axbarsanCecile Robert-Michon
authored andcommitted
Use apimachinery 'resource.Quantity' type for the AzureMachinePool.SpotVMOptions.MaxPrice property
By using a quantity type, which is a fixed-point representation of a number, we are able to not lose precision when serializing floats. Also, when applying CRs directly, we have the liberty to use stringified floats, as well as integers or the quantity serialization format.
1 parent 1a1269a commit cdc3ea7

File tree

7 files changed

+44
-8
lines changed

7 files changed

+44
-8
lines changed

api/v1alpha3/azuremachine_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha3
1818

1919
import (
2020
v1 "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/api/resource"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2324
"sigs.k8s.io/cluster-api/errors"
@@ -119,8 +120,7 @@ type AzureMachineSpec struct {
119120
type SpotVMOptions struct {
120121
// MaxPrice defines the maximum price the user is willing to pay for Spot VM instances
121122
// +optional
122-
// +kubebuilder:validation:Type=number
123-
MaxPrice *string `json:"maxPrice,omitempty"`
123+
MaxPrice *resource.Quantity `json:"maxPrice,omitempty"`
124124
}
125125

126126
// AzureMachineStatus defines the observed state of AzureMachine

api/v1alpha3/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/converters/spotinstances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func GetSpotVMOptions(spotVMOptions *infrav1.SpotVMOptions) (compute.VirtualMach
3232
}
3333
var billingProfile *compute.BillingProfile
3434
if spotVMOptions.MaxPrice != nil {
35-
maxPrice, err := strconv.ParseFloat(*spotVMOptions.MaxPrice, 64)
35+
maxPrice, err := strconv.ParseFloat(spotVMOptions.MaxPrice.AsDec().String(), 64)
3636
if err != nil {
3737
return "", "", nil, err
3838
}

cloud/services/scalesets/scalesets_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/Azure/go-autorest/autorest/to"
2828
"github.com/golang/mock/gomock"
2929
. "github.com/onsi/gomega"
30+
"k8s.io/apimachinery/pkg/api/resource"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/client-go/kubernetes/scheme"
3233
"k8s.io/klog/klogr"
@@ -331,6 +332,29 @@ func TestReconcileVMSS(t *testing.T) {
331332
setupCreatingSucceededExpectations(s, m, putFuture)
332333
},
333334
},
335+
{
336+
name: "should start creating a vmss with spot vm and a maximum price",
337+
expectedError: "failed to get VMSS my-vmss after create or update: failed to get result from future: operation type PUT on Azure resource my-rg/my-vmss is not done",
338+
expect: func(g *WithT, s *mock_scalesets.MockScaleSetScopeMockRecorder, m *mock_scalesets.MockClientMockRecorder) {
339+
spec := newDefaultVMSSSpec()
340+
maxPrice := resource.MustParse("0.001")
341+
spec.SpotVMOptions = &infrav1.SpotVMOptions{
342+
MaxPrice: &maxPrice,
343+
}
344+
s.ScaleSetSpec().Return(spec).AnyTimes()
345+
setupDefaultVMSSStartCreatingExpectations(s, m)
346+
vmss := newDefaultVMSS()
347+
vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.Priority = compute.Spot
348+
vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.BillingProfile = &compute.BillingProfile{
349+
MaxPrice: to.Float64Ptr(0.001),
350+
}
351+
vmss.VirtualMachineScaleSetProperties.VirtualMachineProfile.EvictionPolicy = compute.Deallocate
352+
vmss = setHashOnVMSS(g, vmss)
353+
m.CreateOrUpdateAsync(gomockinternal.AContext(), defaultResourceGroup, defaultVMSSName, gomockinternal.DiffEq(vmss)).
354+
Return(putFuture, nil)
355+
setupCreatingSucceededExpectations(s, m, putFuture)
356+
},
357+
},
334358
{
335359
name: "should start creating a vmss with encryption",
336360
expectedError: "failed to get VMSS my-vmss after create or update: failed to get result from future: operation type PUT on Azure resource my-rg/my-vmss is not done",

config/crd/bases/exp.infrastructure.cluster.x-k8s.io_azuremachinepools.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,13 @@ spec:
312312
should use a Spot VM
313313
properties:
314314
maxPrice:
315+
anyOf:
316+
- type: integer
317+
- type: string
315318
description: MaxPrice defines the maximum price the user is
316319
willing to pay for Spot VM instances
317-
type: number
320+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
321+
x-kubernetes-int-or-string: true
318322
type: object
319323
sshPublicKey:
320324
description: SSHPublicKey is the SSH public key string base64

config/crd/bases/infrastructure.cluster.x-k8s.io_azuremachines.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,13 @@ spec:
482482
should use a Spot VM
483483
properties:
484484
maxPrice:
485+
anyOf:
486+
- type: integer
487+
- type: string
485488
description: MaxPrice defines the maximum price the user is willing
486489
to pay for Spot VM instances
487-
type: number
490+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
491+
x-kubernetes-int-or-string: true
488492
type: object
489493
sshPublicKey:
490494
type: string

config/crd/bases/infrastructure.cluster.x-k8s.io_azuremachinetemplates.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,13 @@ spec:
427427
Machine should use a Spot VM
428428
properties:
429429
maxPrice:
430+
anyOf:
431+
- type: integer
432+
- type: string
430433
description: MaxPrice defines the maximum price the user
431434
is willing to pay for Spot VM instances
432-
type: number
435+
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
436+
x-kubernetes-int-or-string: true
433437
type: object
434438
sshPublicKey:
435439
type: string

0 commit comments

Comments
 (0)