Skip to content

Commit a69c4cb

Browse files
authored
Merge pull request kubernetes#94355 from feiskyer/fix-94354
Ensure getPrimaryInterfaceID not panic when network interfaces for Azure VMSS are null
2 parents f19118e + 479bb35 commit a69c4cb

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,21 @@ func (ss *scaleSet) GetPrivateIPsByNodeName(nodeName string) ([]string, error) {
534534

535535
// This returns the full identifier of the primary NIC for the given VM.
536536
func (ss *scaleSet) getPrimaryInterfaceID(machine compute.VirtualMachineScaleSetVM) (string, error) {
537+
if machine.NetworkProfile == nil || machine.NetworkProfile.NetworkInterfaces == nil {
538+
return "", fmt.Errorf("failed to find the network interfaces for vm %s", to.String(machine.Name))
539+
}
540+
537541
if len(*machine.NetworkProfile.NetworkInterfaces) == 1 {
538542
return *(*machine.NetworkProfile.NetworkInterfaces)[0].ID, nil
539543
}
540544

541545
for _, ref := range *machine.NetworkProfile.NetworkInterfaces {
542-
if *ref.Primary {
546+
if to.Bool(ref.Primary) {
543547
return *ref.ID, nil
544548
}
545549
}
546550

547-
return "", fmt.Errorf("failed to find a primary nic for the vm. vmname=%q", *machine.Name)
551+
return "", fmt.Errorf("failed to find a primary nic for the vm. vmname=%q", to.String(machine.Name))
548552
}
549553

550554
// getVmssMachineID returns the full identifier of a vmss virtual machine.

staging/src/k8s.io/legacy-cloud-providers/azure/azure_vmss_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,11 @@ func TestGetPrimaryInterfaceID(t *testing.T) {
949949
},
950950
expectedErr: fmt.Errorf("failed to find a primary nic for the vm. vmname=\"vm\""),
951951
},
952+
{
953+
description: "GetPrimaryInterfaceID should report an error if there's no network interface on the VMSS VM",
954+
existedInterfaces: []compute.NetworkInterfaceReference{},
955+
expectedErr: fmt.Errorf("failed to find the network interfaces for vm vm"),
956+
},
952957
}
953958

954959
for _, test := range testCases {
@@ -963,6 +968,9 @@ func TestGetPrimaryInterfaceID(t *testing.T) {
963968
},
964969
},
965970
}
971+
if len(test.existedInterfaces) == 0 {
972+
vm.VirtualMachineScaleSetVMProperties.NetworkProfile = nil
973+
}
966974

967975
id, err := ss.getPrimaryInterfaceID(vm)
968976
assert.Equal(t, test.expectedErr, err, test.description+", but an error occurs")

0 commit comments

Comments
 (0)