Skip to content

Commit 608bc56

Browse files
committed
Rename network functions
1 parent 48113ff commit 608bc56

File tree

6 files changed

+33
-33
lines changed

6 files changed

+33
-33
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.16
55
require (
66
github.com/fsnotify/fsnotify v1.4.9
77
github.com/godbus/dbus/v5 v5.0.4
8-
github.com/spf13/viper v1.8.0
8+
github.com/spf13/viper v1.8.1
99
github.com/vishvananda/netlink v1.1.0
10-
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22
10+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
1111
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmq
217217
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
218218
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
219219
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
220-
github.com/spf13/viper v1.8.0 h1:QRwDgoG8xX+kp69di68D+YYTCWfYEckbZRfUlEIAal0=
221-
github.com/spf13/viper v1.8.0/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
220+
github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44=
221+
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
222222
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
223223
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
224224
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -394,8 +394,8 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w
394394
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
395395
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
396396
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
397-
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio=
398-
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
397+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
398+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
399399
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
400400
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
401401
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

pkg/network/network.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,48 +47,49 @@ func ConfigureNetwork(link string, n *Network) error {
4747

4848
gw, err := GetIpv4Gateway(index)
4949
if err != nil {
50-
log.Warnf("Failed to find default gateway on link='%s' ifindex='%d' gw='%s'", link, index, err)
51-
} else {
52-
rt := Route{
53-
IfIndex: index,
54-
Gw: gw,
55-
Table: conf.ROUTE_TABLE_BASE + index,
56-
}
57-
58-
if err = rt.addRoute(); err != nil {
59-
log.Warnf("Failed to add default gateway on link='%s' ifindex='%d' gw='%s' table='%d: %+v", link, index, gw, rt.Table, err)
60-
return err
61-
}
50+
log.Warnf("Failed to find gateway on link='%s' ifindex='%d' gw='%s'", link, index, err)
51+
return err
52+
}
6253

63-
n.RoutesByIndex[index] = &rt
54+
rt := Route{
55+
IfIndex: index,
56+
Gw: gw,
57+
Table: conf.ROUTE_TABLE_BASE + index,
58+
}
6459

65-
log.Debugf("Successfully added default gateway='%s' on link='%s' ifindex='%d' table='%d", gw, link, index, rt.Table)
60+
if err = rt.RouteAdd(); err != nil {
61+
log.Warnf("Failed to add default gateway on link='%s' ifindex='%d' gw='%s' table='%d: %+v", link, index, gw, rt.Table, err)
62+
return err
6663
}
6764

65+
n.RoutesByIndex[index] = &rt
66+
67+
log.Debugf("Successfully added default gateway='%s' on link='%s' ifindex='%d' table='%d", gw, link, index, rt.Table)
68+
6869
existingAddresses, err := getIPv4AddressesByLink(link)
6970
if err != nil {
7071
log.Errorf("Failed to fetch Ip addresses of link='%s' ifindex='%d': %+v", link, err)
7172
return err
7273
}
7374

7475
for address := range existingAddresses {
75-
if err := n.addOneAddressRule(address, link, index); err != nil {
76+
if err := n.oneAddressRuleAdd(address, link, index); err != nil {
7677
continue
7778
}
7879
}
7980

8081
return nil
8182
}
8283

83-
func (n *Network) addOneAddressRule(address string, link string, index int) error {
84+
func (n *Network) oneAddressRuleAdd(address string, link string, index int) error {
8485
addr := strings.TrimSuffix(strings.SplitAfter(address, "/")[0], "/")
8586

8687
from := &RoutingRule{
8788
From: addr,
8889
Table: conf.ROUTE_TABLE_BASE + index,
8990
}
9091

91-
if err := from.addRoutingPolicyRule(); err != nil {
92+
if err := from.RoutingPolicyRuleAdd(); err != nil {
9293
return err
9394
}
9495

@@ -101,7 +102,7 @@ func (n *Network) addOneAddressRule(address string, link string, index int) erro
101102
Table: conf.ROUTE_TABLE_BASE + index,
102103
}
103104

104-
if err := to.addRoutingPolicyRule(); err != nil {
105+
if err := to.RoutingPolicyRuleAdd(); err != nil {
105106
return err
106107
}
107108

pkg/network/route.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func GetIpv4Gateway(ifIndex int) (string, error) {
8585

8686
return gw, nil
8787
}
88-
func (route *Route) addRoute() error {
88+
func (route *Route) RouteAdd() error {
8989
rt := netlink.Route{
9090
LinkIndex: route.IfIndex,
9191
Gw: net.ParseIP(route.Gw).To4(),
@@ -99,7 +99,7 @@ func (route *Route) addRoute() error {
9999
return nil
100100
}
101101

102-
func (route *Route) removeRoute() error {
102+
func (route *Route) RouteRemove() error {
103103
rt := netlink.Route{
104104
LinkIndex: route.IfIndex,
105105
Gw: net.ParseIP(route.Gw).To4(),

pkg/network/routing_policy_rule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type RoutingRule struct {
1515
Table int
1616
}
1717

18-
func (rule *RoutingRule) addRoutingPolicyRule() error {
18+
func (rule *RoutingRule) RoutingPolicyRuleAdd() error {
1919
links, err := netlink.LinkList()
2020
if err != nil {
2121
return nil
@@ -55,7 +55,7 @@ func (rule *RoutingRule) addRoutingPolicyRule() error {
5555
return nil
5656
}
5757

58-
func (rule *RoutingRule) removeRoutingPolicyRule() error {
58+
func (rule *RoutingRule) RoutingPolicyRuleRemove() error {
5959
r := netlink.NewRule()
6060
r.Table = rule.Table
6161

pkg/network/watch_network.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (n *Network) watchAddresses() {
5757
if updates.NewAddr {
5858
log.Infof("IP address='%s' added to link ifindex='%d'", ip, updates.LinkIndex)
5959

60-
n.addOneAddressRule(ip, n.LinksByIndex[updates.LinkIndex], updates.LinkIndex)
60+
n.oneAddressRuleAdd(ip, n.LinksByIndex[updates.LinkIndex], updates.LinkIndex)
6161
} else {
6262
log.Infof("IP address='%s' removed from link ifindex='%d'", ip, updates.LinkIndex)
6363

@@ -67,7 +67,6 @@ func (n *Network) watchAddresses() {
6767
}
6868
}
6969
}
70-
7170
}
7271

7372
func (n *Network) watchLinks() {
@@ -127,13 +126,13 @@ func (n *Network) dropConfiguration(ifIndex int, address string) {
127126

128127
rule, ok := n.RoutingRulesByAddressFrom[address]
129128
if ok {
130-
rule.removeRoutingPolicyRule()
129+
rule.RoutingPolicyRuleRemove()
131130
delete(n.RoutingRulesByAddressFrom, address)
132131
}
133132

134133
rule, ok = n.RoutingRulesByAddressTo[address]
135134
if ok {
136-
rule.removeRoutingPolicyRule()
135+
rule.RoutingPolicyRuleRemove()
137136
delete(n.RoutingRulesByAddressTo, address)
138137
}
139138

@@ -144,7 +143,7 @@ func (n *Network) dropConfiguration(ifIndex int, address string) {
144143

145144
log.Debugf("Dropping GW='%s' link='%s' ifindex='%d' Table='%d'", rt.Gw, n.LinksByIndex[ifIndex], ifIndex, rt.Table)
146145

147-
rt.removeRoute()
146+
rt.RouteRemove()
148147
delete(n.RoutesByIndex, ifIndex)
149148
}
150149
}

0 commit comments

Comments
 (0)