From 5a18f58dbd68887295f427502b91fd9bba3e24bc Mon Sep 17 00:00:00 2001 From: Angus Lees Date: Mon, 8 Jun 2020 15:12:35 +1000 Subject: [PATCH] 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 #80747 robust to this situation. --- .../k8s.io/legacy-cloud-providers/aws/aws.go | 22 ++++++++++++------- .../legacy-cloud-providers/aws/aws_fakes.go | 7 +++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go index 89b70c6aa8e..d764fd0e9e0 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go @@ -1415,7 +1415,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 @@ -1430,18 +1432,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 diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_fakes.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_fakes.go index f0be7a92897..0113c55554f 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_fakes.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_fakes.go @@ -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 } } }