Skip to content

Commit cc734e5

Browse files
committed
fix(ip rule): use NewRule() for all rule creations
It has proven to be tricky to insert new rules without calling the designated NewRule() function from the netlink library. Usually attempts will fail with an operation not supported message. This improves the reliability of rule insertion.
1 parent 7c5fb3c commit cc734e5

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

pkg/controllers/proxy/linux_networking.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,12 @@ func (ln *linuxNetworking) setupRoutesForExternalIPForDSR(serviceInfoMap service
566566
"error please report because something has gone very wrong) due to: %v", err)
567567
}
568568

569-
nRule := &netlink.Rule{
570-
Priority: defaultDSRPolicyRulePriority,
571-
Src: defaultPrefixCIDR,
572-
Table: externalIPRouteTableID,
573-
}
569+
nRule := netlink.NewRule()
570+
nRule.Family = nFamily
571+
nRule.Priority = defaultDSRPolicyRulePriority
572+
nRule.Src = defaultPrefixCIDR
573+
nRule.Table = externalIPRouteTableID
574+
574575
rules, err := netlink.RuleListFiltered(nFamily, nRule,
575576
netlink.RT_FILTER_TABLE|netlink.RT_FILTER_SRC|netlink.RT_FILTER_PRIORITY)
576577
if err != nil {
@@ -581,10 +582,10 @@ func (ln *linuxNetworking) setupRoutesForExternalIPForDSR(serviceInfoMap service
581582
if len(rules) < 1 {
582583
err = netlink.RuleAdd(nRule)
583584
if err != nil {
584-
klog.Infof("Failed to add policy rule `ip rule add prio 32765 from all lookup external_ip` due to %v",
585-
err)
586-
return fmt.Errorf("failed to add policy rule `ip rule add prio 32765 from all lookup external_ip` "+
587-
"due to %v", err)
585+
klog.Infof("Failed to add policy rule (equivalent to `ip rule add prio %d from %s lookup "+
586+
"%d`) due to %v", defaultDSRPolicyRulePriority, defaultPrefixCIDR, externalIPRouteTableID, err)
587+
return fmt.Errorf("failed to add policy rule (equivalent to `ip rule add prio %d from %s lookup "+
588+
"%d`) due to %v", defaultDSRPolicyRulePriority, defaultPrefixCIDR, externalIPRouteTableID, err)
588589
}
589590
}
590591

pkg/controllers/proxy/network_services_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,11 +1747,10 @@ func routeVIPTrafficToDirector(fwmark string, family v1.IPFamily) error {
17471747
return fmt.Errorf("failed to convert fwmark to uint32: %v", err)
17481748
}
17491749

1750-
nRule := &netlink.Rule{
1751-
Mark: uFWMark,
1752-
Table: customDSRRouteTableID,
1753-
Priority: defaultTrafficDirectorRulePriority,
1754-
}
1750+
nRule := netlink.NewRule()
1751+
nRule.Mark = uFWMark
1752+
nRule.Table = customDSRRouteTableID
1753+
nRule.Priority = defaultTrafficDirectorRulePriority
17551754

17561755
routes, err := netlink.RuleListFiltered(nFamily, nRule, netlink.RT_FILTER_MARK|netlink.RT_FILTER_TABLE)
17571756
if err != nil {

pkg/routes/pbr.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func ipRuleAbstraction(ipFamily int, ipOp int, cidr string) error {
4646
return fmt.Errorf("failed to parse CIDR: %s", err.Error())
4747
}
4848

49-
nRule := &netlink.Rule{
50-
Family: ipFamily,
51-
Src: nSrc,
52-
Table: CustomTableID,
53-
}
49+
nRule := netlink.NewRule()
50+
nRule.Family = ipFamily
51+
nRule.Src = nSrc
52+
nRule.Table = CustomTableID
53+
5454
rules, err := netlink.RuleListFiltered(ipFamily, nRule, netlink.RT_FILTER_SRC)
5555
if err != nil {
5656
return fmt.Errorf("failed to list rules: %s", err.Error())

0 commit comments

Comments
 (0)