From e7cc501625dce9b99ff14c27b57a90a1d2f5f5c1 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Thu, 6 Jul 2023 17:18:49 +0200 Subject: [PATCH] Update types --- api/v1beta1/azuremanagedmachinepool_types.go | 3 ++- api/v1beta1/zz_generated.deepcopy.go | 4 ++-- azure/services/agentpools/spec.go | 13 ++++++++--- azure/services/agentpools/spec_test.go | 23 ++++++++++++++++++- ...ter.x-k8s.io_azuremanagedmachinepools.yaml | 5 ++++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/api/v1beta1/azuremanagedmachinepool_types.go b/api/v1beta1/azuremanagedmachinepool_types.go index 0af7c61c304..1ea93267d02 100644 --- a/api/v1beta1/azuremanagedmachinepool_types.go +++ b/api/v1beta1/azuremanagedmachinepool_types.go @@ -17,6 +17,7 @@ limitations under the License. package v1beta1 import ( + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" capierrors "sigs.k8s.io/cluster-api/errors" @@ -529,7 +530,7 @@ type AzureManagedMachinePoolSpec struct { // SpotMaxPrice defines max price to pay for spot instance. // Possible values are any decimal value greater than zero or -1 which indicates the willingness to pay any on-demand price. // +optional - SpotMaxPrice *float64 `json:"spotMaxPrice,omitempty"` + SpotMaxPrice *resource.Quantity `json:"spotMaxPrice,omitempty"` // KubeletConfig specifies the kubelet configurations for nodes. // Immutable. diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index a0c972fdfc8..b9907ea65e6 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -1409,8 +1409,8 @@ func (in *AzureManagedMachinePoolSpec) DeepCopyInto(out *AzureManagedMachinePool } if in.SpotMaxPrice != nil { in, out := &in.SpotMaxPrice, &out.SpotMaxPrice - *out = new(float64) - **out = **in + x := (*in).DeepCopy() + *out = &x } if in.KubeletConfig != nil { in, out := &in.KubeletConfig, &out.KubeletConfig diff --git a/azure/services/agentpools/spec.go b/azure/services/agentpools/spec.go index 88903d3b7d7..560d46f23fd 100644 --- a/azure/services/agentpools/spec.go +++ b/azure/services/agentpools/spec.go @@ -19,6 +19,7 @@ package agentpools import ( "context" "fmt" + "k8s.io/apimachinery/pkg/api/resource" "time" "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice" @@ -133,7 +134,7 @@ type AgentPoolSpec struct { ScaleDownMode *string `json:"scaleDownMode,omitempty"` // SpotMaxPrice defines max price to pay for spot instance. Allowed values are any decimal value greater than zero or -1 which indicates the willingness to pay any on-demand price. - SpotMaxPrice *float64 `json:"spotMaxPrice,omitempty"` + SpotMaxPrice *resource.Quantity `json:"spotMaxPrice,omitempty"` // KubeletConfig specifies the kubelet configurations for nodes. KubeletConfig *KubeletConfig `json:"kubeletConfig,omitempty"` @@ -216,7 +217,6 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p NodeLabels: s.NodeLabels, NodeTaints: &s.NodeTaints, ScaleDownMode: containerservice.ScaleDownMode(pointer.StringDeref(s.ScaleDownMode, "")), - SpotMaxPrice: s.SpotMaxPrice, Tags: converters.TagsToMap(s.AdditionalTags), }, } @@ -224,6 +224,10 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p normalizedProfile.NodeTaints = nil } + if s.SpotMaxPrice != nil { + normalizedProfile.SpotMaxPrice = pointer.Float64(s.SpotMaxPrice.AsApproximateFloat64()) + } + if s.KubeletConfig != nil { normalizedProfile.KubeletConfig = &containerservice.KubeletConfig{ CPUManagerPolicy: s.KubeletConfig.CPUManagerPolicy, @@ -365,7 +369,6 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p OsType: containerservice.OSType(pointer.StringDeref(s.OSType, "")), ScaleSetPriority: containerservice.ScaleSetPriority(pointer.StringDeref(s.ScaleSetPriority, "")), ScaleDownMode: containerservice.ScaleDownMode(pointer.StringDeref(s.ScaleDownMode, "")), - SpotMaxPrice: s.SpotMaxPrice, Type: containerservice.AgentPoolTypeVirtualMachineScaleSets, VMSize: sku, VnetSubnetID: vnetSubnetID, @@ -376,6 +379,10 @@ func (s *AgentPoolSpec) Parameters(ctx context.Context, existing interface{}) (p }, } + if s.SpotMaxPrice != nil { + agentPool.SpotMaxPrice = pointer.Float64(s.SpotMaxPrice.AsApproximateFloat64()) + } + return agentPool, nil } diff --git a/azure/services/agentpools/spec_test.go b/azure/services/agentpools/spec_test.go index 9eb26700e24..acb9088c7e8 100644 --- a/azure/services/agentpools/spec_test.go +++ b/azure/services/agentpools/spec_test.go @@ -18,6 +18,7 @@ package agentpools import ( "context" + "k8s.io/apimachinery/pkg/api/resource" "reflect" "testing" "time" @@ -76,6 +77,13 @@ func withAutoscaling(enabled bool) func(*AgentPoolSpec) { } } +func withSpotMaxPrice(spotMaxPrice string) func(*AgentPoolSpec) { + quantity := resource.MustParse(spotMaxPrice) + return func(pool *AgentPoolSpec) { + pool.SpotMaxPrice = &quantity + } +} + func sdkFakeAgentPool(changes ...func(*containerservice.AgentPool)) containerservice.AgentPool { pool := containerservice.AgentPool{ ManagedClusterAgentPoolProfileProperties: &containerservice.ManagedClusterAgentPoolProfileProperties{ @@ -241,12 +249,25 @@ func TestParameters(t *testing.T) { name: "parameters with an existing agent pool and update needed on spot max price", spec: fakeAgentPool(), existing: sdkFakeAgentPool( - sdkWithSpotMaxPrice(120), + sdkWithSpotMaxPrice(123.456), sdkWithProvisioningState("Succeeded"), ), expected: sdkFakeAgentPool(), expectedError: nil, }, + { + name: "parameters with an existing agent pool and update needed on spot max price", + spec: fakeAgentPool( + withSpotMaxPrice("789.123"), + ), + existing: sdkFakeAgentPool( + sdkWithProvisioningState("Succeeded"), + ), + expected: sdkFakeAgentPool( + sdkWithSpotMaxPrice(789.123), + ), + expectedError: nil, + }, { name: "parameters with an existing agent pool and update needed on max count", spec: fakeAgentPool(), diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_azuremanagedmachinepools.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_azuremanagedmachinepools.yaml index 88fd25a11ce..9f47834fb24 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_azuremanagedmachinepools.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_azuremanagedmachinepools.yaml @@ -528,9 +528,14 @@ spec: description: SKU is the size of the VMs in the node pool. Immutable. type: string spotMaxPrice: + anyOf: + - type: integer + - type: string description: SpotMaxPrice defines max price to pay for spot instance. Possible values are any decimal value greater than zero or -1 which indicates the willingness to pay any on-demand price. + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true subnetName: description: SubnetName specifies the Subnet where the MachinePool will be placed Immutable.