From bf13a7e598ce4867b115c3bec468781047c7639c Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Fri, 9 Apr 2021 15:38:42 +0800 Subject: [PATCH] Avoid caching the VMSS instances whose network profile is nil --- .../azure/azure_vmss_cache.go | 5 +++ .../azure/azure_vmss_test.go | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_cache.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_cache.go index 5abfec1936e91..9dcf785f5ad9a 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_cache.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_cache.go @@ -193,6 +193,11 @@ func (ss *scaleSet) newVMSSVirtualMachinesCache(resourceGroupName, vmssName, cac } computerName := strings.ToLower(*vm.OsProfile.ComputerName) + if vm.NetworkProfile == nil || vm.NetworkProfile.NetworkInterfaces == nil { + klog.Warningf("skip caching vmssVM %s since its network profile hasn't initialized yet (probably still under creating)", computerName) + continue + } + vmssVMCacheEntry := &vmssVirtualMachinesEntry{ resourceGroup: resourceGroupName, vmssName: vmssName, diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go index 21a472738c194..d0887eac0b045 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go @@ -1385,16 +1385,25 @@ func TestGetAgentPoolScaleSets(t *testing.T) { { VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000000")}, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + }, }, }, { VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000001")}, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + }, }, }, { VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000002")}, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + }, }, }, } @@ -1462,6 +1471,20 @@ func TestGetVMSetNames(t *testing.T) { }, expectedErr: fmt.Errorf("scale set (vmss-1) - not found"), }, + { + description: "GetVMSetNames should report an error if vm's network profile is nil", + service: &v1.Service{ + ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{ServiceAnnotationLoadBalancerMode: "vmss"}}, + }, + nodes: []*v1.Node{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "vmss-vm-000003", + }, + }, + }, + expectedErr: fmt.Errorf("instance not found"), + }, { description: "GetVMSetNames should return the correct vmss names", service: &v1.Service{ @@ -1495,16 +1518,30 @@ func TestGetVMSetNames(t *testing.T) { { VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000000")}, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + }, }, }, { VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000001")}, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + }, }, }, { VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000002")}, + NetworkProfile: &compute.NetworkProfile{ + NetworkInterfaces: &[]compute.NetworkInterfaceReference{}, + }, + }, + }, + { + VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{ + OsProfile: &compute.OSProfile{ComputerName: to.StringPtr("vmss-vm-000003")}, }, }, }