@@ -71,6 +71,10 @@ func newEgressIPWatcher(localIP string, oc *ovsController) *egressIPWatcher {
71
71
}
72
72
73
73
func (eip * egressIPWatcher ) Start (networkClient networkclient.Interface , iptables * NodeIPTables ) error {
74
+ if err := eip .findEgressLink (); err != nil {
75
+ return fmt .Errorf ("Could not find egress network interface: %v" , err )
76
+ }
77
+
74
78
eip .iptables = iptables
75
79
eip .networkClient = networkClient
76
80
@@ -79,6 +83,30 @@ func (eip *egressIPWatcher) Start(networkClient networkclient.Interface, iptable
79
83
return nil
80
84
}
81
85
86
+ func (eip * egressIPWatcher ) findEgressLink () error {
87
+ links , err := netlink .LinkList ()
88
+ if err != nil {
89
+ return err
90
+ }
91
+ for _ , link := range links {
92
+ addrs , err := netlink .AddrList (link , syscall .AF_INET )
93
+ if err != nil {
94
+ glog .Warningf ("Could not get addresses of interface %q while trying to find egress interface: %v" , link .Attrs ().Name , err )
95
+ continue
96
+ }
97
+
98
+ for _ , addr := range addrs {
99
+ if addr .IP .String () == eip .localIP {
100
+ eip .localEgressLink = link
101
+ eip .localEgressIPMaskLen , _ = addr .Mask .Size ()
102
+ return nil
103
+ }
104
+ }
105
+ }
106
+
107
+ return fmt .Errorf ("could not find network interface with the address %q" , eip .localIP )
108
+ }
109
+
82
110
func ipToHex (ip string ) string {
83
111
bytes := net .ParseIP (ip )
84
112
if bytes == nil {
@@ -254,33 +282,6 @@ func (eip *egressIPWatcher) claimEgressIP(egressIP, egressHex string) error {
254
282
return nil
255
283
}
256
284
257
- if eip .localEgressLink == nil {
258
- links , err := netlink .LinkList ()
259
- if err != nil {
260
- return fmt .Errorf ("could not get list of network interfaces while adding egress IP: %v" , err )
261
- }
262
- linkLoop:
263
- for _ , link := range links {
264
- addrs , err := netlink .AddrList (link , syscall .AF_INET )
265
- if err != nil {
266
- glog .Warningf ("Could not get addresses of interface %q while trying to find egress interface: %v" , link .Attrs ().Name , err )
267
- continue
268
- }
269
-
270
- for _ , addr := range addrs {
271
- if addr .IP .String () == eip .localIP {
272
- eip .localEgressLink = link
273
- eip .localEgressIPMaskLen , _ = addr .Mask .Size ()
274
- break linkLoop
275
- }
276
- }
277
- }
278
-
279
- if eip .localEgressLink == nil {
280
- return fmt .Errorf ("could not find network interface with the address %q while adding egress IP" , eip .localIP )
281
- }
282
- }
283
-
284
285
egressIPNet := fmt .Sprintf ("%s/%d" , egressIP , eip .localEgressIPMaskLen )
285
286
addr , err := netlink .ParseAddr (egressIPNet )
286
287
if err != nil {
@@ -308,10 +309,6 @@ func (eip *egressIPWatcher) releaseEgressIP(egressIP, egressHex string) error {
308
309
return nil
309
310
}
310
311
311
- if eip .localEgressLink == nil {
312
- return nil
313
- }
314
-
315
312
egressIPNet := fmt .Sprintf ("%s/%d" , egressIP , eip .localEgressIPMaskLen )
316
313
addr , err := netlink .ParseAddr (egressIPNet )
317
314
if err != nil {
0 commit comments