Skip to content

Commit

Permalink
Move powerState to azure_util, change default to powerStateUnknown
Browse files Browse the repository at this point in the history
* renames all PowerState* consts to vmPowerState*
* moves vmPowerState* consts and helper functions to azure_util.go
* changes default vmPowerState to vmPowerStateUnknown instead of vmPowerStateStopped when a power state is not set.
  • Loading branch information
domenicbozzuto committed Jul 5, 2023
1 parent 0ee3abf commit dbff9be
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 50 deletions.
52 changes: 5 additions & 47 deletions cluster-autoscaler/cloudprovider/azure/azure_scale_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
)

// PowerStates reflect the operational state of a VM
// From https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.management.compute.powerstate?view=azure-java-stable
const (
PowerStateStarting = "PowerState/starting"
PowerStateRunning = "PowerState/running"
PowerStateStopping = "PowerState/stopping"
PowerStateStopped = "PowerState/stopped"
PowerStateDeallocating = "PowerState/deallocating"
PowerStateDeallocated = "PowerState/deallocated"
PowerStateUnknown = "PowerState/unknown"
)

var (
defaultVmssInstancesRefreshPeriod = 5 * time.Minute
vmssContextTimeout = 3 * time.Minute
Expand Down Expand Up @@ -624,17 +612,17 @@ func buildInstanceCache(vmList interface{}) []cloudprovider.Instance {
switch vms := vmList.(type) {
case []compute.VirtualMachineScaleSetVM:
for _, vm := range vms {
powerState := PowerStateRunning
powerState := vmPowerStateRunning
if vm.InstanceView != nil && vm.InstanceView.Statuses != nil {
powerState = powerStateFromStatuses(*vm.InstanceView.Statuses)
powerState = vmPowerStateFromStatuses(*vm.InstanceView.Statuses)
}
addInstanceToCache(&instances, vm.ID, vm.ProvisioningState, powerState)
}
case []compute.VirtualMachine:
for _, vm := range vms {
powerState := PowerStateRunning
powerState := vmPowerStateRunning
if vm.InstanceView != nil && vm.InstanceView.Statuses != nil {
powerState = powerStateFromStatuses(*vm.InstanceView.Statuses)
powerState = vmPowerStateFromStatuses(*vm.InstanceView.Statuses)
}
addInstanceToCache(&instances, vm.ID, vm.ProvisioningState, powerState)
}
Expand Down Expand Up @@ -704,7 +692,7 @@ func instanceStatusFromProvisioningStateAndPowerState(resourceId string, provisi
// Per https://learn.microsoft.com/en-us/azure/virtual-machines/states-billing#provisioning-states,
// ProvisioningState represents the most recent provisioning state, therefore only report
// InstanceCreating errors when the power state indicates the instance has not yet started running
if !isRunningPowerState(powerState) {
if !isRunningVmPowerState(powerState) {
klog.V(4).Infof("VM %s reports failed provisioning state with non-running power state: %s", resourceId, powerState)
status.State = cloudprovider.InstanceCreating
status.ErrorInfo = &cloudprovider.InstanceErrorInfo{
Expand All @@ -723,36 +711,6 @@ func instanceStatusFromProvisioningStateAndPowerState(resourceId string, provisi
return status
}

func powerStateFromStatuses(statuses []compute.InstanceViewStatus) string {
for _, status := range statuses {
if status.Code == nil || !isKnownPowerState(*status.Code) {
continue
}
return *status.Code
}

// PowerState is not set if the VM is still creating (or has failed creation),
// so the absence of a PowerState is treated the same as a VM that is stopped
return PowerStateStopped
}

func isRunningPowerState(powerState string) bool {
return powerState == PowerStateRunning || powerState == PowerStateStarting
}

func isKnownPowerState(powerState string) bool {
knownPowerStates := map[string]bool{
PowerStateStarting: true,
PowerStateRunning: true,
PowerStateStopping: true,
PowerStateStopped: true,
PowerStateDeallocating: true,
PowerStateDeallocated: true,
PowerStateUnknown: true,
}
return knownPowerStates[powerState]
}

func (scaleSet *ScaleSet) invalidateInstanceCache() {
scaleSet.instanceMutex.Lock()
// Set the instanceCache as outdated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ func TestIncreaseSizeOnVMProvisioningFailed(t *testing.T) {
}{
"out of resources when no power state exists": {},
"out of resources when VM is stopped": {
statuses: []compute.InstanceViewStatus{{Code: to.StringPtr(PowerStateStopped)}},
statuses: []compute.InstanceViewStatus{{Code: to.StringPtr(vmPowerStateStopped)}},
},
"out of resources when VM reports unknown power state": {
"out of resources when VM reports invalid power state": {
statuses: []compute.InstanceViewStatus{{Code: to.StringPtr("PowerState/invalid")}},
},
"instance running when power state is running": {
expectInstanceRunning: true,
statuses: []compute.InstanceViewStatus{{Code: to.StringPtr(PowerStateRunning)}},
statuses: []compute.InstanceViewStatus{{Code: to.StringPtr(vmPowerStateRunning)}},
},
"instance running if instance view cannot be retrieved": {
expectInstanceRunning: true,
Expand Down
39 changes: 39 additions & 0 deletions cluster-autoscaler/cloudprovider/azure/azure_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ const (
nodeTaintTagName = "k8s.io_cluster-autoscaler_node-template_taint_"
nodeResourcesTagName = "k8s.io_cluster-autoscaler_node-template_resources_"
nodeOptionsTagName = "k8s.io_cluster-autoscaler_node-template_autoscaling-options_"

// PowerStates reflect the operational state of a VM
// From https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.management.compute.powerstate?view=azure-java-stable
vmPowerStateStarting = "PowerState/starting"
vmPowerStateRunning = "PowerState/running"
vmPowerStateStopping = "PowerState/stopping"
vmPowerStateStopped = "PowerState/stopped"
vmPowerStateDeallocating = "PowerState/deallocating"
vmPowerStateDeallocated = "PowerState/deallocated"
vmPowerStateUnknown = "PowerState/unknown"
)

var (
Expand Down Expand Up @@ -608,3 +618,32 @@ func isAzureRequestsThrottled(rerr *retry.Error) bool {

return rerr.HTTPStatusCode == http.StatusTooManyRequests
}

func isRunningVmPowerState(powerState string) bool {
return powerState == vmPowerStateRunning || powerState == vmPowerStateStarting
}

func isKnownVmPowerState(powerState string) bool {
knownPowerStates := map[string]bool{
vmPowerStateStarting: true,
vmPowerStateRunning: true,
vmPowerStateStopping: true,
vmPowerStateStopped: true,
vmPowerStateDeallocating: true,
vmPowerStateDeallocated: true,
vmPowerStateUnknown: true,
}
return knownPowerStates[powerState]
}

func vmPowerStateFromStatuses(statuses []compute.InstanceViewStatus) string {
for _, status := range statuses {
if status.Code == nil || !isKnownVmPowerState(*status.Code) {
continue
}
return *status.Code
}

// PowerState is not set if the VM is still creating (or has failed creation)
return vmPowerStateUnknown
}

0 comments on commit dbff9be

Please sign in to comment.