Skip to content

Commit 5a18f58

Browse files
committed
aws: Fix address sorting of multiple interfaces
It is common to have "holes" in the network interfaces, eg after attaching eth1+eth2, and then removing eth1. Make the sorting code from kubernetes#80747 robust to this situation.
1 parent b93e9d9 commit 5a18f58

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

staging/src/k8s.io/legacy-cloud-providers/aws/aws.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,9 @@ func (c *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.No
14151415
// We want the IPs to end up in order by interface (in particular, we want eth0's
14161416
// IPs first), but macs isn't necessarily sorted in that order so we have to
14171417
// explicitly order by device-number (device-number == the "0" in "eth0").
1418-
macIPs := make(map[int]string)
1418+
1419+
var macIDs []string
1420+
macDevNum := make(map[string]int)
14191421
for _, macID := range strings.Split(macs, "\n") {
14201422
if macID == "" {
14211423
continue
@@ -1430,18 +1432,22 @@ func (c *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.No
14301432
klog.Warningf("Bad device-number %q for interface %s\n", numStr, macID)
14311433
continue
14321434
}
1435+
macIDs = append(macIDs, macID)
1436+
macDevNum[macID] = num
1437+
}
1438+
1439+
// Sort macIDs by interface device-number
1440+
sort.Slice(macIDs, func(i, j int) bool {
1441+
return macDevNum[macIDs[i]] < macDevNum[macIDs[j]]
1442+
})
1443+
1444+
for _, macID := range macIDs {
14331445
ipPath := path.Join("network/interfaces/macs/", macID, "local-ipv4s")
1434-
macIPs[num], err = c.metadata.GetMetadata(ipPath)
1446+
internalIPs, err := c.metadata.GetMetadata(ipPath)
14351447
if err != nil {
14361448
return nil, fmt.Errorf("error querying AWS metadata for %q: %q", ipPath, err)
14371449
}
1438-
}
14391450

1440-
for i := 0; i < len(macIPs); i++ {
1441-
internalIPs := macIPs[i]
1442-
if internalIPs == "" {
1443-
continue
1444-
}
14451451
for _, internalIP := range strings.Split(internalIPs, "\n") {
14461452
if internalIP == "" {
14471453
continue

staging/src/k8s.io/legacy-cloud-providers/aws/aws_fakes.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,12 @@ func (m *FakeMetadata) GetMetadata(key string) (string, error) {
358358
if len(keySplit) == 5 && keySplit[4] == "device-number" {
359359
for i, macElem := range m.aws.networkInterfacesMacs {
360360
if macParam == macElem {
361-
return fmt.Sprintf("%d\n", i), nil
361+
n := i
362+
if n > 0 {
363+
// Introduce an artificial gap, just to test eg: [eth0, eth2]
364+
n++
365+
}
366+
return fmt.Sprintf("%d\n", n), nil
362367
}
363368
}
364369
}

0 commit comments

Comments
 (0)