mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-24 03:18:57 +00:00
Merge pull request #101429 from Nordix/issues-93858
Kube-proxy/ipvs; Use go "net" lib to get nodeIPs
This commit is contained in:
@@ -252,6 +252,27 @@ func GetNodeAddresses(cidrs []string, nw NetworkInterfacer) (sets.String, error)
|
||||
return uniqueAddressList, nil
|
||||
}
|
||||
|
||||
// AddressSet validates the addresses in the slice using the "isValid" function.
|
||||
// Addresses that pass the validation are returned as a string Set.
|
||||
func AddressSet(isValid func(ip net.IP) bool, addrs []net.Addr) sets.String {
|
||||
ips := sets.NewString()
|
||||
for _, a := range addrs {
|
||||
var ip net.IP
|
||||
switch v := a.(type) {
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
default:
|
||||
continue
|
||||
}
|
||||
if isValid(ip) {
|
||||
ips.Insert(ip.String())
|
||||
}
|
||||
}
|
||||
return ips
|
||||
}
|
||||
|
||||
// LogAndEmitIncorrectIPVersionEvent logs and emits incorrect IP version event.
|
||||
func LogAndEmitIncorrectIPVersionEvent(recorder events.EventRecorder, fieldName, fieldValue, svcNamespace, svcName string, svcUID types.UID) {
|
||||
errMsg := fmt.Sprintf("%s in %s has incorrect IP version", fieldValue, fieldName)
|
||||
|
@@ -1283,3 +1283,119 @@ func randSeq() string {
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func mustParseIPAddr(str string) net.Addr {
|
||||
a, err := net.ResolveIPAddr("ip", str)
|
||||
if err != nil {
|
||||
panic("mustParseIPAddr")
|
||||
}
|
||||
return a
|
||||
}
|
||||
func mustParseIPNet(str string) net.Addr {
|
||||
_, n, err := netutils.ParseCIDRSloppy(str)
|
||||
if err != nil {
|
||||
panic("mustParseIPNet")
|
||||
}
|
||||
return n
|
||||
}
|
||||
func mustParseUnix(str string) net.Addr {
|
||||
n, err := net.ResolveUnixAddr("unix", str)
|
||||
if err != nil {
|
||||
panic("mustParseUnix")
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
type cidrValidator struct {
|
||||
cidr *net.IPNet
|
||||
}
|
||||
|
||||
func (v *cidrValidator) isValid(ip net.IP) bool {
|
||||
return v.cidr.Contains(ip)
|
||||
}
|
||||
func newCidrValidator(cidr string) func(ip net.IP) bool {
|
||||
_, n, err := netutils.ParseCIDRSloppy(cidr)
|
||||
if err != nil {
|
||||
panic("mustParseIPNet")
|
||||
}
|
||||
obj := cidrValidator{n}
|
||||
return obj.isValid
|
||||
}
|
||||
|
||||
func TestAddressSet(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
validator func(ip net.IP) bool
|
||||
input []net.Addr
|
||||
expected sets.String
|
||||
}{
|
||||
{
|
||||
"Empty",
|
||||
func(ip net.IP) bool { return false },
|
||||
nil,
|
||||
sets.NewString(),
|
||||
},
|
||||
{
|
||||
"Reject IPAddr x 2",
|
||||
func(ip net.IP) bool { return false },
|
||||
[]net.Addr{
|
||||
mustParseIPAddr("8.8.8.8"),
|
||||
mustParseIPAddr("1000::"),
|
||||
},
|
||||
sets.NewString(),
|
||||
},
|
||||
{
|
||||
"Accept IPAddr x 2",
|
||||
func(ip net.IP) bool { return true },
|
||||
[]net.Addr{
|
||||
mustParseIPAddr("8.8.8.8"),
|
||||
mustParseIPAddr("1000::"),
|
||||
},
|
||||
sets.NewString("8.8.8.8", "1000::"),
|
||||
},
|
||||
{
|
||||
"Accept IPNet x 2",
|
||||
func(ip net.IP) bool { return true },
|
||||
[]net.Addr{
|
||||
mustParseIPNet("8.8.8.8/32"),
|
||||
mustParseIPNet("1000::/128"),
|
||||
},
|
||||
sets.NewString("8.8.8.8", "1000::"),
|
||||
},
|
||||
{
|
||||
"Accept Unix x 2",
|
||||
func(ip net.IP) bool { return true },
|
||||
[]net.Addr{
|
||||
mustParseUnix("/tmp/sock1"),
|
||||
mustParseUnix("/tmp/sock2"),
|
||||
},
|
||||
sets.NewString(),
|
||||
},
|
||||
{
|
||||
"Cidr IPv4",
|
||||
newCidrValidator("192.168.1.0/24"),
|
||||
[]net.Addr{
|
||||
mustParseIPAddr("8.8.8.8"),
|
||||
mustParseIPAddr("1000::"),
|
||||
mustParseIPAddr("192.168.1.1"),
|
||||
},
|
||||
sets.NewString("192.168.1.1"),
|
||||
},
|
||||
{
|
||||
"Cidr IPv6",
|
||||
newCidrValidator("1000::/64"),
|
||||
[]net.Addr{
|
||||
mustParseIPAddr("8.8.8.8"),
|
||||
mustParseIPAddr("1000::"),
|
||||
mustParseIPAddr("192.168.1.1"),
|
||||
},
|
||||
sets.NewString("1000::"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
if !tc.expected.Equal(AddressSet(tc.validator, tc.input)) {
|
||||
t.Errorf("%s", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user