Merge pull request #94355 from feiskyer/fix-94354

Ensure getPrimaryInterfaceID not panic when network interfaces for Azure VMSS are null
This commit is contained in:
Kubernetes Prow Robot 2020-09-01 19:41:43 -07:00 committed by GitHub
commit a69c4cb5f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -534,17 +534,21 @@ func (ss *scaleSet) GetPrivateIPsByNodeName(nodeName string) ([]string, error) {
// This returns the full identifier of the primary NIC for the given VM.
func (ss *scaleSet) getPrimaryInterfaceID(machine compute.VirtualMachineScaleSetVM) (string, error) {
if machine.NetworkProfile == nil || machine.NetworkProfile.NetworkInterfaces == nil {
return "", fmt.Errorf("failed to find the network interfaces for vm %s", to.String(machine.Name))
}
if len(*machine.NetworkProfile.NetworkInterfaces) == 1 {
return *(*machine.NetworkProfile.NetworkInterfaces)[0].ID, nil
}
for _, ref := range *machine.NetworkProfile.NetworkInterfaces {
if *ref.Primary {
if to.Bool(ref.Primary) {
return *ref.ID, nil
}
}
return "", fmt.Errorf("failed to find a primary nic for the vm. vmname=%q", *machine.Name)
return "", fmt.Errorf("failed to find a primary nic for the vm. vmname=%q", to.String(machine.Name))
}
// getVmssMachineID returns the full identifier of a vmss virtual machine.

View File

@ -949,6 +949,11 @@ func TestGetPrimaryInterfaceID(t *testing.T) {
},
expectedErr: fmt.Errorf("failed to find a primary nic for the vm. vmname=\"vm\""),
},
{
description: "GetPrimaryInterfaceID should report an error if there's no network interface on the VMSS VM",
existedInterfaces: []compute.NetworkInterfaceReference{},
expectedErr: fmt.Errorf("failed to find the network interfaces for vm vm"),
},
}
for _, test := range testCases {
@ -963,6 +968,9 @@ func TestGetPrimaryInterfaceID(t *testing.T) {
},
},
}
if len(test.existedInterfaces) == 0 {
vm.VirtualMachineScaleSetVMProperties.NetworkProfile = nil
}
id, err := ss.getPrimaryInterfaceID(vm)
assert.Equal(t, test.expectedErr, err, test.description+", but an error occurs")