Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit 2bfa1f5

Browse files
committed
network: For each addresses in the interface create routing policy rules
1 parent e7593af commit 2bfa1f5

File tree

1 file changed

+46
-58
lines changed

1 file changed

+46
-58
lines changed

provider/provider_network.go

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ import (
1414
)
1515

1616
func (m *Environment) configureNetwork(link *network.Link, newAddresses map[string]bool) error {
17-
existingAddresses, err := network.GetIPv4Addresses(link.Name)
18-
if err != nil {
19-
log.Errorf("Failed to fetch Ip addresses of link='%s' ifindex='%d': %+v", link.Name, link.Ifindex, err)
20-
return err
21-
}
22-
2317
if len(m.AddressesByMAC[link.Mac]) > 0 {
2418
earlierAddresses := m.AddressesByMAC[link.Mac]
2519

@@ -46,70 +40,66 @@ func (m *Environment) configureNetwork(link *network.Link, newAddresses map[stri
4640
}
4741

4842
for i := range newAddresses {
49-
_, ok := existingAddresses[i]
50-
if !ok {
51-
if link.OperState == "down" {
52-
if err := network.LinkSetOperStateUp(link.Ifindex); err != nil {
53-
log.Errorf("Failed to bring up the link='%s' ifindex='%d': %+v", link.Name, link.Ifindex, err)
54-
return err
55-
}
56-
57-
log.Debugf("Successfully brought up the link='%s' ifindex='%d'", link.Name, link.Ifindex)
43+
if link.OperState == "down" {
44+
if err := network.LinkSetOperStateUp(link.Ifindex); err != nil {
45+
log.Errorf("Failed to bring up the link='%s' ifindex='%d': %+v", link.Name, link.Ifindex, err)
46+
return err
5847
}
5948

60-
var mtu int
61-
switch m.Kind {
62-
case cloud.GCP:
63-
mtu, err = m.gcp.ParseLinkMTUFromMetadataByMac(link.Mac)
64-
if err != nil || mtu == 0 {
65-
log.Warningf("Failed to parse MTU link='%s' ifindex='%d': %+v", err)
66-
}
67-
}
49+
log.Debugf("Successfully brought up the link='%s' ifindex='%d'", link.Name, link.Ifindex)
50+
}
6851

69-
if mtu != 0 && link.MTU != mtu {
70-
if err := network.LinkSetMtu(link.Ifindex, mtu); err != nil {
71-
log.Warningf("Failed to set MTU link='%s' ifindex='%d': %+v", err)
72-
} else {
73-
log.Infof("Successfully MTU set to '%d' link='%s' ifindex='%d'", mtu, link.Name, link.Ifindex)
74-
}
52+
var mtu int
53+
switch m.Kind {
54+
case cloud.GCP:
55+
mtu, err := m.gcp.ParseLinkMTUFromMetadataByMac(link.Mac)
56+
if err != nil || mtu == 0 {
57+
log.Warningf("Failed to parse MTU link='%s' ifindex='%d': %+v", err)
7558
}
59+
}
7660

77-
if err := network.AddressSet(link.Name, i); err != nil {
78-
log.Errorf("Failed to add address='%s' to link='%s' ifindex='%d': %+v", i, link.Name, link.Ifindex, err)
79-
continue
61+
if mtu != 0 && link.MTU != mtu {
62+
if err := network.LinkSetMtu(link.Ifindex, mtu); err != nil {
63+
log.Warningf("Failed to set MTU link='%s' ifindex='%d': %+v", err)
64+
} else {
65+
log.Infof("Successfully MTU set to '%d' link='%s' ifindex='%d'", mtu, link.Name, link.Ifindex)
8066
}
67+
}
8168

82-
log.Infof("Successfully added address='%s on link='%s' ifindex='%d'", i, link.Name, link.Ifindex)
69+
if err := network.AddressSet(link.Name, i); err != nil {
70+
log.Errorf("Failed to add address='%s' to link='%s' ifindex='%d': %+v", i, link.Name, link.Ifindex, err)
71+
continue
72+
}
8373

84-
// https://docs.microsoft.com/en-us/azure/virtual-network/virtual-network-multiple-ip-addresses-portal#add
85-
// echo 150 custom >> /etc/iproute2/rt_tables
86-
// ip rule add from 10.0.0.5 lookup custom
87-
// ip route add default via 10.0.0.1 dev eth2 table custom
74+
log.Infof("Successfully added address='%s on link='%s' ifindex='%d'", i, link.Name, link.Ifindex)
8875

89-
// https://aws.amazon.com/premiumsupport/knowledge-center/ec2-ubuntu-secondary-network-interface/
90-
// Gateway configuration
91-
// #ip route add default via 172.31.16.1 dev eth1 table 1000
76+
// https://docs.microsoft.com/en-us/azure/virtual-network/virtual-network-multiple-ip-addresses-portal#add
77+
// echo 150 custom >> /etc/iproute2/rt_tables
78+
// ip rule add from 10.0.0.5 lookup custom
79+
// ip route add default via 10.0.0.1 dev eth2 table custom
9280

93-
// Routes and rules
94-
// ip route add 172.31.21.115 dev eth1 table 1000
95-
// ip rule add from 172.31.21.115 lookup 1000
81+
// https://aws.amazon.com/premiumsupport/knowledge-center/ec2-ubuntu-secondary-network-interface/
82+
// Gateway configuration
83+
// #ip route add default via 172.31.16.1 dev eth1 table 1000
9684

97-
// https://cloud.google.com/vpc/docs/create-use-multiple-interfaces
98-
// sudo ifconfig eth1 192.168.0.2 netmask 255.255.255.255 broadcast 192.168.0.2 mtu 1430
99-
// echo "1 rt1" | sudo tee -a /etc/iproute2/rt_tables
100-
// sudo ip route add 192.168.0.1 src 192.168.0.2 dev eth1 table rt1
101-
// sudo ip route add default via 192.168.0.1 dev eth1 table rt1
102-
// sudo ip rule add from 192.168.0.2/32 table rt1
103-
// sudo ip rule add to 192.168.0.2/32 table rt1
85+
// Routes and rules
86+
// ip route add 172.31.21.115 dev eth1 table 1000
87+
// ip rule add from 172.31.21.115 lookup 1000
10488

105-
if err := m.configureRoute(link); err != nil {
106-
continue
107-
}
89+
// https://cloud.google.com/vpc/docs/create-use-multiple-interfaces
90+
// sudo ifconfig eth1 192.168.0.2 netmask 255.255.255.255 broadcast 192.168.0.2 mtu 1430
91+
// echo "1 rt1" | sudo tee -a /etc/iproute2/rt_tables
92+
// sudo ip route add 192.168.0.1 src 192.168.0.2 dev eth1 table rt1
93+
// sudo ip route add default via 192.168.0.1 dev eth1 table rt1
94+
// sudo ip rule add from 192.168.0.2/32 table rt1
95+
// sudo ip rule add to 192.168.0.2/32 table rt1
10896

109-
if err := m.configureRoutingPolicyRule(link, i); err != nil {
110-
continue
111-
}
97+
if err := m.configureRoute(link); err != nil {
98+
continue
99+
}
112100

101+
if err := m.configureRoutingPolicyRule(link, i); err != nil {
102+
continue
113103
}
114104
}
115105
delete(m.AddressesByMAC, link.Mac)
@@ -211,7 +201,6 @@ func (m *Environment) isRulesByTableEmpty(table int) bool {
211201
}
212202

213203
func (m *Environment) removeRoutingPolicyRule(address string, link *network.Link) error {
214-
215204
log.Debugf("Removing routing policy rules for address='%s' link='%s'", address, link.Name)
216205

217206
rule, ok := m.RoutingRulesByAddressFrom[address]
@@ -240,7 +229,6 @@ func (m *Environment) removeRoutingPolicyRule(address string, link *network.Link
240229
if ok {
241230

242231
if m.isRulesByTableEmpty(rt.Table) {
243-
244232
log.Debugf("Dropping GW='%s' link='%s' ifindex='%d' Table='%d'", rt.Gw, link.Name, link.Ifindex, rt.Table)
245233

246234
network.RouteRemove(rt)

0 commit comments

Comments
 (0)