mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Avoiding double parsing of ip/cidr strings and logging bad ips/cidrs
This commit is contained in:
parent
f11c4e9c8c
commit
293d4b7c48
@ -260,32 +260,50 @@ func MapIPsByIPFamily(ipStrings []string) map[v1.IPFamily][]string {
|
|||||||
ipFamilyMap := map[v1.IPFamily][]string{}
|
ipFamilyMap := map[v1.IPFamily][]string{}
|
||||||
for _, ip := range ipStrings {
|
for _, ip := range ipStrings {
|
||||||
// Handle only the valid IPs
|
// Handle only the valid IPs
|
||||||
if net.ParseIP(ip) != nil {
|
if ipFamily, err := getIPFamilyFromIP(ip); err == nil {
|
||||||
ipFamily := getIPFamilyFromIP(ip)
|
|
||||||
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], ip)
|
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], ip)
|
||||||
|
} else {
|
||||||
|
klog.Errorf("Skipping invalid IP: %s", ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ipFamilyMap
|
return ipFamilyMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// MapCIDRsByIPFamily maps a slice of IPs to their respective IP families (v4 or v6)
|
// MapCIDRsByIPFamily maps a slice of IPs to their respective IP families (v4 or v6)
|
||||||
func MapCIDRsByIPFamily(ipStrings []string) map[v1.IPFamily][]string {
|
func MapCIDRsByIPFamily(cidrStrings []string) map[v1.IPFamily][]string {
|
||||||
ipFamilyMap := map[v1.IPFamily][]string{}
|
ipFamilyMap := map[v1.IPFamily][]string{}
|
||||||
for _, cidr := range ipStrings {
|
for _, cidr := range cidrStrings {
|
||||||
// Handle only the valid CIDRs
|
// Handle only the valid CIDRs
|
||||||
if _, _, err := net.ParseCIDR(cidr); err == nil {
|
if ipFamily, err := getIPFamilyFromCIDR(cidr); err == nil {
|
||||||
ipFamily := getIPFamilyFromCIDR(cidr)
|
|
||||||
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], cidr)
|
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], cidr)
|
||||||
|
} else {
|
||||||
|
klog.Errorf("Skipping invalid cidr: %s", cidr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ipFamilyMap
|
return ipFamilyMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIPFamilyFromIP(ip string) v1.IPFamily {
|
func getIPFamilyFromIP(ipStr string) (v1.IPFamily, error) {
|
||||||
if utilnet.IsIPv6String(ip) {
|
netIP := net.ParseIP(ipStr)
|
||||||
return v1.IPv6Protocol
|
if netIP == nil {
|
||||||
|
return "", ErrAddressNotAllowed
|
||||||
}
|
}
|
||||||
return v1.IPv4Protocol
|
|
||||||
|
if utilnet.IsIPv6(netIP) {
|
||||||
|
return v1.IPv6Protocol, nil
|
||||||
|
}
|
||||||
|
return v1.IPv4Protocol, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIPFamilyFromCIDR(cidrStr string) (v1.IPFamily, error) {
|
||||||
|
_, netCIDR, err := net.ParseCIDR(cidrStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", ErrAddressNotAllowed
|
||||||
|
}
|
||||||
|
if utilnet.IsIPv6CIDR(netCIDR) {
|
||||||
|
return v1.IPv6Protocol, nil
|
||||||
|
}
|
||||||
|
return v1.IPv4Protocol, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// OtherIPFamily returns the other ip family
|
// OtherIPFamily returns the other ip family
|
||||||
@ -297,13 +315,6 @@ func OtherIPFamily(ipFamily v1.IPFamily) v1.IPFamily {
|
|||||||
return v1.IPv6Protocol
|
return v1.IPv6Protocol
|
||||||
}
|
}
|
||||||
|
|
||||||
func getIPFamilyFromCIDR(ip string) v1.IPFamily {
|
|
||||||
if utilnet.IsIPv6CIDRString(ip) {
|
|
||||||
return v1.IPv6Protocol
|
|
||||||
}
|
|
||||||
return v1.IPv4Protocol
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendPortIfNeeded appends the given port to IP address unless it is already in
|
// AppendPortIfNeeded appends the given port to IP address unless it is already in
|
||||||
// "ipv4:port" or "[ipv6]:port" format.
|
// "ipv4:port" or "[ipv6]:port" format.
|
||||||
func AppendPortIfNeeded(addr string, port int32) string {
|
func AppendPortIfNeeded(addr string, port int32) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user