mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-13 11:25:19 +00:00
Rename GetNodeAddresses to GetNodeIPs, return net.IP
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"net"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
netutils "k8s.io/utils/net"
|
||||
)
|
||||
|
||||
@@ -91,16 +90,17 @@ func (npa *NodePortAddresses) MatchAll() bool {
|
||||
return npa.matchAll
|
||||
}
|
||||
|
||||
// GetNodeAddresses return all matched node IP addresses for npa's CIDRs. If no matching
|
||||
// GetNodeIPs return all matched node IP addresses for npa's CIDRs. If no matching
|
||||
// IPs are found, it returns an empty list.
|
||||
// NetworkInterfacer is injected for test purpose.
|
||||
func (npa *NodePortAddresses) GetNodeAddresses(nw NetworkInterfacer) (sets.Set[string], error) {
|
||||
func (npa *NodePortAddresses) GetNodeIPs(nw NetworkInterfacer) ([]net.IP, error) {
|
||||
addrs, err := nw.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error listing all interfaceAddrs from host, error: %v", err)
|
||||
}
|
||||
|
||||
uniqueAddressList := sets.New[string]()
|
||||
// Use a map to dedup matches
|
||||
addresses := make(map[string]net.IP)
|
||||
for _, cidr := range npa.cidrs {
|
||||
for _, addr := range addrs {
|
||||
var ip net.IP
|
||||
@@ -115,12 +115,17 @@ func (npa *NodePortAddresses) GetNodeAddresses(nw NetworkInterfacer) (sets.Set[s
|
||||
}
|
||||
|
||||
if cidr.Contains(ip) {
|
||||
uniqueAddressList.Insert(ip.String())
|
||||
addresses[ip.String()] = ip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return uniqueAddressList, nil
|
||||
ips := make([]net.IP, 0, len(addresses))
|
||||
for _, ip := range addresses {
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
|
||||
return ips, nil
|
||||
}
|
||||
|
||||
// ContainsIPv4Loopback returns true if npa's CIDRs contain an IPv4 loopback address.
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
@@ -31,7 +32,24 @@ type InterfaceAddrsPair struct {
|
||||
addrs []net.Addr
|
||||
}
|
||||
|
||||
func TestGetNodeAddresses(t *testing.T) {
|
||||
func checkNodeIPs(expected sets.Set[string], actual []net.IP) error {
|
||||
notFound := expected.Clone()
|
||||
extra := sets.New[string]()
|
||||
for _, ip := range actual {
|
||||
str := ip.String()
|
||||
if notFound.Has(str) {
|
||||
notFound.Delete(str)
|
||||
} else {
|
||||
extra.Insert(str)
|
||||
}
|
||||
}
|
||||
if len(notFound) != 0 || len(extra) != 0 {
|
||||
return fmt.Errorf("not found: %v, extra: %v", notFound.UnsortedList(), extra.UnsortedList())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGetNodeIPs(t *testing.T) {
|
||||
type expectation struct {
|
||||
matchAll bool
|
||||
ips sets.Set[string]
|
||||
@@ -367,16 +385,18 @@ func TestGetNodeAddresses(t *testing.T) {
|
||||
t.Errorf("unexpected MatchAll(%s), expected: %v", family, tc.expected[family].matchAll)
|
||||
}
|
||||
|
||||
addrList, err := npa.GetNodeAddresses(nw)
|
||||
ips, err := npa.GetNodeIPs(nw)
|
||||
expectedIPs := tc.expected[family].ips
|
||||
|
||||
// The fake InterfaceAddrs() never returns an error, so
|
||||
// the only error GetNodeAddresses will return is "no
|
||||
// the only error GetNodeIPs will return is "no
|
||||
// addresses found".
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !addrList.Equal(tc.expected[family].ips) {
|
||||
t.Errorf("unexpected mismatch for %s, expected: %v, got: %v", family, tc.expected[family].ips, addrList)
|
||||
err = checkNodeIPs(expectedIPs, ips)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected mismatch for %s: %v", family, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user