From a8050a79990c8ce64cf16e0722843d9ddce21bd9 Mon Sep 17 00:00:00 2001 From: Prashanth Date: Tue, 11 May 2021 14:37:32 +0530 Subject: [PATCH] Avoids panics when VM type isn't found during scale from zero - Cluster autoscaler was trying to fetch VM details for AWS VMs instead of required Azure VMs for MCM (OOT) provider Azure. This lead to panics. Now, it fetches the details from the correct provider VM map. - This PR also improves error handling in such scenarios to not panic. --- .../cloudprovider/mcm/mcm_manager.go | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cluster-autoscaler/cloudprovider/mcm/mcm_manager.go b/cluster-autoscaler/cloudprovider/mcm/mcm_manager.go index b00f767934a2..dc5a1541039e 100644 --- a/cluster-autoscaler/cloudprovider/mcm/mcm_manager.go +++ b/cluster-autoscaler/cloudprovider/mcm/mcm_manager.go @@ -628,7 +628,10 @@ func (m *McmManager) GetMachineDeploymentNodeTemplate(machinedeployment *Machine if err != nil { return nil, fmt.Errorf("Unable to fetch AWSMachineClass object %s, Error: %v", machineClass.Name, err) } - awsInstance := aws.InstanceTypes[mc.Spec.MachineType] + awsInstance, exists := aws.InstanceTypes[mc.Spec.MachineType] + if !exists { + return nil, fmt.Errorf("Unable to fetch details for VM type %s", mc.Spec.MachineType) + } instance = instanceType{ InstanceType: awsInstance.InstanceType, VCPU: awsInstance.VCPU, @@ -642,7 +645,10 @@ func (m *McmManager) GetMachineDeploymentNodeTemplate(machinedeployment *Machine if err != nil { return nil, fmt.Errorf("Unable to fetch AzureMachineClass object %s, Error: %v", machineClass.Name, err) } - azureInstance := azure.InstanceTypes[mc.Spec.Properties.HardwareProfile.VMSize] + azureInstance, exists := azure.InstanceTypes[mc.Spec.Properties.HardwareProfile.VMSize] + if !exists { + return nil, fmt.Errorf("Unable to fetch details for VM type %s", mc.Spec.Properties.HardwareProfile.VMSize) + } instance = instanceType{ InstanceType: azureInstance.InstanceType, VCPU: azureInstance.VCPU, @@ -666,7 +672,10 @@ func (m *McmManager) GetMachineDeploymentNodeTemplate(machinedeployment *Machine return nil, fmt.Errorf("Unable to convert from %s to %s for %s, Error: %v", kindMachineClass, providerAWS, machinedeployment.Name, err) } - awsInstance := aws.InstanceTypes[providerSpec.MachineType] + awsInstance, exists := aws.InstanceTypes[providerSpec.MachineType] + if !exists { + return nil, fmt.Errorf("Unable to fetch details for VM type %s", providerSpec.MachineType) + } instance = instanceType{ InstanceType: awsInstance.InstanceType, VCPU: awsInstance.VCPU, @@ -681,8 +690,10 @@ func (m *McmManager) GetMachineDeploymentNodeTemplate(machinedeployment *Machine if err != nil { return nil, fmt.Errorf("Unable to convert from %s to %s for %s, Error: %v", kindMachineClass, providerAzure, machinedeployment.Name, err) } - - azureInstance := aws.InstanceTypes[providerSpec.Properties.HardwareProfile.VMSize] + azureInstance, exists := azure.InstanceTypes[providerSpec.Properties.HardwareProfile.VMSize] + if !exists { + return nil, fmt.Errorf("Unable to fetch details for VM type %s", providerSpec.Properties.HardwareProfile.VMSize) + } instance = instanceType{ InstanceType: azureInstance.InstanceType, VCPU: azureInstance.VCPU,