forked from k3s-io/k3s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iface.go
57 lines (50 loc) · 1.33 KB
/
iface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package netutil
import (
"fmt"
"net"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func GetIPFromInterface(ifaceName string) string {
ip, err := getIPFromInterface(ifaceName)
if err != nil {
logrus.Warn(errors.Wrap(err, "unable to get global unicast ip from interface name"))
} else {
logrus.Infof("found ip %s from iface %s", ip, ifaceName)
}
return ip
}
func getIPFromInterface(ifaceName string) (string, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return "", err
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
if iface.Flags&net.FlagUp == 0 {
return "", fmt.Errorf("the interface %s is not up", ifaceName)
}
globalUnicasts := []string{}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
return "", errors.Wrapf(err, "unable to parse CIDR for interface %s", iface.Name)
}
// skipping if not ipv4
if ip.To4() == nil {
continue
}
if ip.IsGlobalUnicast() {
globalUnicasts = append(globalUnicasts, ip.String())
}
}
if len(globalUnicasts) > 1 {
return "", fmt.Errorf("multiple global unicast addresses defined for %s, please set ip from one of %v", ifaceName, globalUnicasts)
}
if len(globalUnicasts) == 1 {
return globalUnicasts[0], nil
}
return "", fmt.Errorf("can't find ip for interface %s", ifaceName)
}