sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/converters/spotinstances.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package converters
    18  
    19  import (
    20  	"strconv"
    21  
    22  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    23  	"k8s.io/utils/ptr"
    24  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    25  )
    26  
    27  // GetSpotVMOptions takes the spot vm options
    28  // and returns the individual vm priority, eviction policy and billing profile.
    29  func GetSpotVMOptions(spotVMOptions *infrav1.SpotVMOptions, diffDiskSettings *infrav1.DiffDiskSettings) (*armcompute.VirtualMachinePriorityTypes, *armcompute.VirtualMachineEvictionPolicyTypes, *armcompute.BillingProfile, error) {
    30  	// Spot VM not requested, return zero values to apply defaults
    31  	if spotVMOptions == nil {
    32  		return nil, nil, nil, nil
    33  	}
    34  	var billingProfile *armcompute.BillingProfile
    35  	if spotVMOptions.MaxPrice != nil {
    36  		maxPrice, err := strconv.ParseFloat(spotVMOptions.MaxPrice.AsDec().String(), 64)
    37  		if err != nil {
    38  			return nil, nil, nil, err
    39  		}
    40  		billingProfile = &armcompute.BillingProfile{
    41  			MaxPrice: &maxPrice,
    42  		}
    43  	}
    44  
    45  	// Set the spot vm eviction policy if provided.
    46  	var evictionPolicy *armcompute.VirtualMachineEvictionPolicyTypes
    47  	if spotVMOptions.EvictionPolicy != nil {
    48  		evictionPolicy = ptr.To(armcompute.VirtualMachineEvictionPolicyTypes(*spotVMOptions.EvictionPolicy))
    49  	}
    50  
    51  	return ptr.To(armcompute.VirtualMachinePriorityTypesSpot), evictionPolicy, billingProfile, nil
    52  }