forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_network_resolver.go
57 lines (45 loc) · 1.32 KB
/
default_network_resolver.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 net
import (
gonet "net"
boship "github.com/cloudfoundry/bosh-agent/platform/net/ip"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type defaultNetworkResolver struct {
routesSearcher RoutesSearcher
ipResolver boship.Resolver
}
func NewDefaultNetworkResolver(
routesSearcher RoutesSearcher,
ipResolver boship.Resolver,
) boshsettings.DefaultNetworkResolver {
return defaultNetworkResolver{
routesSearcher: routesSearcher,
ipResolver: ipResolver,
}
}
func (r defaultNetworkResolver) GetDefaultNetwork() (boshsettings.Network, error) {
network := boshsettings.Network{}
routes, err := r.routesSearcher.SearchRoutes()
if err != nil {
return network, bosherr.WrapError(err, "Searching routes")
}
if len(routes) == 0 {
return network, bosherr.Error("No routes found")
}
for _, route := range routes {
if !route.IsDefault() {
continue
}
ip, err := r.ipResolver.GetPrimaryIPv4(route.InterfaceName)
if err != nil {
return network, bosherr.WrapErrorf(err, "Getting primary IPv4 for interface '%s'", route.InterfaceName)
}
return boshsettings.Network{
IP: ip.IP.String(),
Netmask: gonet.IP(ip.Mask).String(),
Gateway: route.Gateway,
}, nil
}
return network, bosherr.Error("Failed to find default route")
}