mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
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.
This commit is contained in:
parent
b93e9d9395
commit
5a18f58dbd
@ -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
|
// 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
|
// 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").
|
// 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") {
|
for _, macID := range strings.Split(macs, "\n") {
|
||||||
if macID == "" {
|
if macID == "" {
|
||||||
continue
|
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)
|
klog.Warningf("Bad device-number %q for interface %s\n", numStr, macID)
|
||||||
continue
|
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")
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error querying AWS metadata for %q: %q", ipPath, err)
|
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") {
|
for _, internalIP := range strings.Split(internalIPs, "\n") {
|
||||||
if internalIP == "" {
|
if internalIP == "" {
|
||||||
continue
|
continue
|
||||||
|
@ -358,7 +358,12 @@ func (m *FakeMetadata) GetMetadata(key string) (string, error) {
|
|||||||
if len(keySplit) == 5 && keySplit[4] == "device-number" {
|
if len(keySplit) == 5 && keySplit[4] == "device-number" {
|
||||||
for i, macElem := range m.aws.networkInterfacesMacs {
|
for i, macElem := range m.aws.networkInterfacesMacs {
|
||||||
if macParam == macElem {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user