Skip to content

Commit 257d11d

Browse files
authored
Merge pull request kubernetes#91889 from anguslees/aws-ipsort
aws: Fix address sorting of multiple interfaces
2 parents 42bf886 + 5a18f58 commit 257d11d

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
@@ -1422,7 +1422,9 @@ func (c *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.No
14221422
// We want the IPs to end up in order by interface (in particular, we want eth0's
14231423
// IPs first), but macs isn't necessarily sorted in that order so we have to
14241424
// explicitly order by device-number (device-number == the "0" in "eth0").
1425-
macIPs := make(map[int]string)
1425+
1426+
var macIDs []string
1427+
macDevNum := make(map[string]int)
14261428
for _, macID := range strings.Split(macs, "\n") {
14271429
if macID == "" {
14281430
continue
@@ -1437,18 +1439,22 @@ func (c *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.No
14371439
klog.Warningf("Bad device-number %q for interface %s\n", numStr, macID)
14381440
continue
14391441
}
1442+
macIDs = append(macIDs, macID)
1443+
macDevNum[macID] = num
1444+
}
1445+
1446+
// Sort macIDs by interface device-number
1447+
sort.Slice(macIDs, func(i, j int) bool {
1448+
return macDevNum[macIDs[i]] < macDevNum[macIDs[j]]
1449+
})
1450+
1451+
for _, macID := range macIDs {
14401452
ipPath := path.Join("network/interfaces/macs/", macID, "local-ipv4s")
1441-
macIPs[num], err = c.metadata.GetMetadata(ipPath)
1453+
internalIPs, err := c.metadata.GetMetadata(ipPath)
14421454
if err != nil {
14431455
return nil, fmt.Errorf("error querying AWS metadata for %q: %q", ipPath, err)
14441456
}
1445-
}
14461457

1447-
for i := 0; i < len(macIPs); i++ {
1448-
internalIPs := macIPs[i]
1449-
if internalIPs == "" {
1450-
continue
1451-
}
14521458
for _, internalIP := range strings.Split(internalIPs, "\n") {
14531459
if internalIP == "" {
14541460
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)