func routeCollector()

in pkg/skoop/collector/podcollector/collector.go [454:516]


func routeCollector(sandboxInfo *netstack.NetNSInfo) error {
	rules, err := netlink.RuleList(netlink.FAMILY_V4)
	if err != nil {
		return fmt.Errorf("error collector rule list: %v", err)
	}
	tableIDSet := map[int]interface{}{}
	for _, rule := range rules {
		if _, ok := tableIDSet[rule.Table]; !ok {
			tableIDSet[rule.Table] = struct{}{}
		}
	}

	for tableID := range tableIDSet {
		v4Route, err := netlink.RouteListFiltered(netlink.FAMILY_V4, &netlink.Route{Table: tableID}, netlink.RT_FILTER_TABLE)
		if err != nil {
			return fmt.Errorf("error collector route list: %v", err)
		}
		for _, route := range v4Route {
			var iif, oif netlink.Link
			if route.ILinkIndex != 0 {
				iif, err = netlink.LinkByIndex(route.ILinkIndex)
				if err != nil {
					return err
				}
			}
			if route.LinkIndex != 0 {
				oif, err = netlink.LinkByIndex(route.LinkIndex)
				if err != nil {
					return err
				}
			}

			routeInfo := netstack.Route{
				Family:   netlink.FAMILY_V4,
				Scope:    netstack.Scope(route.Scope),
				Dst:      route.Dst,
				Src:      route.Src,
				Gw:       route.Gw,
				Protocol: int(route.Protocol),
				Priority: route.Priority,
				Table:    route.Table,
				Type:     route.Type,
				Tos:      route.Tos,
				Flags:    route.Flags,
			}
			// default route
			if routeInfo.Dst == nil {
				_, routeInfo.Dst, _ = net.ParseCIDR("0.0.0.0/0")
			}

			if iif != nil {
				routeInfo.IifName = iif.Attrs().Name
			}

			if oif != nil {
				routeInfo.OifName = oif.Attrs().Name
			}

			sandboxInfo.RouteInfo = append(sandboxInfo.RouteInfo, routeInfo)
		}
	}
	return nil
}