Merge pull request #91889 from anguslees/aws-ipsort

aws: Fix address sorting of multiple interfaces
This commit is contained in:
Kubernetes Prow Robot 2020-06-11 00:53:10 -07:00 committed by GitHub
commit 257d11dc35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -1422,7 +1422,9 @@ func (c *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.No
// We want the IPs to end up in order by interface (in particular, we want eth0's
// IPs first), but macs isn't necessarily sorted in that order so we have to
// explicitly order by device-number (device-number == the "0" in "eth0").
macIPs := make(map[int]string)
var macIDs []string
macDevNum := make(map[string]int)
for _, macID := range strings.Split(macs, "\n") {
if macID == "" {
continue
@ -1437,18 +1439,22 @@ func (c *Cloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.No
klog.Warningf("Bad device-number %q for interface %s\n", numStr, macID)
continue
}
macIDs = append(macIDs, macID)
macDevNum[macID] = num
}
// Sort macIDs by interface device-number
sort.Slice(macIDs, func(i, j int) bool {
return macDevNum[macIDs[i]] < macDevNum[macIDs[j]]
})
for _, macID := range macIDs {
ipPath := path.Join("network/interfaces/macs/", macID, "local-ipv4s")
macIPs[num], err = c.metadata.GetMetadata(ipPath)
internalIPs, err := c.metadata.GetMetadata(ipPath)
if err != nil {
return nil, fmt.Errorf("error querying AWS metadata for %q: %q", ipPath, err)
}
}
for i := 0; i < len(macIPs); i++ {
internalIPs := macIPs[i]
if internalIPs == "" {
continue
}
for _, internalIP := range strings.Split(internalIPs, "\n") {
if internalIP == "" {
continue

View File

@ -358,7 +358,12 @@ func (m *FakeMetadata) GetMetadata(key string) (string, error) {
if len(keySplit) == 5 && keySplit[4] == "device-number" {
for i, macElem := range m.aws.networkInterfacesMacs {
if macParam == macElem {
return fmt.Sprintf("%d\n", i), nil
n := i
if n > 0 {
// Introduce an artificial gap, just to test eg: [eth0, eth2]
n++
}
return fmt.Sprintf("%d\n", n), nil
}
}
}