Skip to content

Commit 33be00e

Browse files
fix: Scope default gateway detection to main routing table
The `getDefaultGwInterfaces` function previously filtered routes from all routing tables. This caused interfaces to be incorrectly identified as default gateways and excluded from the ResourceSlice if they had a default route in ANY table, even non-main ones.
1 parent 00612d9 commit 33be00e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pkg/inventory/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func (db *DB) Run(ctx context.Context) error {
152152
// Obtain data that will not change after the startup
153153
db.instance = getInstanceProperties(ctx)
154154
db.gwInterfaces = getDefaultGwInterfaces()
155+
klog.V(2).Infof("Default gateway interfaces: %v", db.gwInterfaces.UnsortedList())
155156

156157
for {
157158
err := db.rateLimiter.Wait(ctx)

pkg/inventory/net.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ import (
2121
"github.com/cilium/ebpf/link"
2222
"github.com/google/dranet/internal/nlwrap"
2323
"github.com/vishvananda/netlink"
24+
"golang.org/x/sys/unix"
2425

2526
"k8s.io/apimachinery/pkg/util/sets"
2627
"k8s.io/klog/v2"
2728
)
2829

30+
// getDefaultGwInterfaces returns a set of interface names that are configured
31+
// as default gateways in the main routing table. It identifies these by querying
32+
// the main routing table for routes with an unspecified destination (0.0.0.0/0
33+
// for IPv4 or ::/0 for IPv6).
2934
func getDefaultGwInterfaces() sets.Set[string] {
3035
interfaces := sets.Set[string]{}
31-
filter := &netlink.Route{}
36+
filter := &netlink.Route{
37+
Table: unix.RT_TABLE_MAIN,
38+
}
3239
routes, err := nlwrap.RouteListFiltered(netlink.FAMILY_ALL, filter, netlink.RT_FILTER_TABLE)
3340
if err != nil {
3441
return interfaces
@@ -68,7 +75,6 @@ func getDefaultGwInterfaces() sets.Set[string] {
6875
interfaces.Insert(intfLink.Attrs().Name)
6976
}
7077
}
71-
klog.V(4).Infof("Found following interfaces for the default gateway: %v", interfaces.UnsortedList())
7278
return interfaces
7379
}
7480

0 commit comments

Comments
 (0)