mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-19 18:02:01 +00:00
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:
commit
a69c4cb5f1
@ -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.
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user