feat: add CIDR support for ip setting (#43223)

issue: #26538

---------

Signed-off-by: yusheng.ma <yusheng.ma@zilliz.com>
Signed-off-by: yusheng.ma <yushengma@bytedance.com>
Co-authored-by: yusheng.ma <yushengma@bytedance.com>
This commit is contained in:
presburger 2025-08-11 14:17:42 +08:00 committed by GitHub
parent 77f2fb562f
commit ebd517c514
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -59,6 +59,22 @@ func GetIP(ip string) string {
if len(ip) == 0 {
return GetLocalIP()
}
// Support setting CIDR in the IP field to match interfaces based on CIDR. For example: 192.168.0.0/16
_, ipnet, err := net.ParseCIDR(ip)
if err == nil {
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, addr := range addrs {
addrip, ok := addr.(*net.IPNet)
if ok && ipnet.Contains(addrip.IP) {
return addrip.IP.String()
}
}
}
panic(errors.New(`Network port does not have an IP address that falls within the given CIDR range`))
}
netIP := net.ParseIP(ip)
// not a valid ip addr
if netIP == nil {