-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
259 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//go:build linux | ||
|
||
package network | ||
|
||
import ( | ||
"github.com/cloud-barista/cm-honeybee/agent/lib/routes" | ||
"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/model/onprem/network" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
func GetRoutes() ([]network.Route, error) { | ||
var rs []network.Route | ||
var osRoutes []routes.RouteStruct | ||
var err error | ||
|
||
osRoutes, err = routes.GetRoutes(false) | ||
if err != nil { | ||
return rs, err | ||
} | ||
|
||
type routeExt struct { | ||
Source string | ||
Scope string | ||
Proto string | ||
Link string | ||
} | ||
var routeExts = make(map[string]routeExt) | ||
|
||
rts, err := netlink.RouteList(nil, netlink.FAMILY_ALL) | ||
if err != nil { | ||
return rs, err | ||
} | ||
for _, rt := range rts { | ||
iface := "N/A" | ||
linkState := "" | ||
|
||
if rt.LinkIndex != 0 { | ||
link, err := netlink.LinkByIndex(rt.LinkIndex) | ||
if err == nil { | ||
iface = link.Attrs().Name | ||
if iface == "N/A" { | ||
continue | ||
} | ||
|
||
if link.Attrs().OperState == netlink.OperUp { | ||
linkState = "up" | ||
} else { | ||
linkState = "down" | ||
} | ||
} | ||
} | ||
|
||
source := "" | ||
if rt.Src != nil { | ||
source = rt.Src.String() | ||
} | ||
scope := rt.Scope.String() | ||
protocol := rt.Protocol.String() | ||
|
||
routeExts[iface] = routeExt{ | ||
Source: source, | ||
Scope: scope, | ||
Proto: protocol, | ||
Link: linkState, | ||
} | ||
} | ||
|
||
for _, r := range osRoutes { | ||
if r.NextHop == "on-link" { | ||
r.NextHop = r.Interface | ||
} | ||
|
||
rt := network.Route{ | ||
Destination: r.Destination, | ||
Netmask: r.Netmask, | ||
NextHop: r.NextHop, | ||
Metric: r.Metric, | ||
} | ||
|
||
ext, ok := routeExts[r.Interface] | ||
if ok { | ||
rt.Source = ext.Source | ||
rt.Scope = ext.Scope | ||
rt.Proto = ext.Proto | ||
rt.Link = ext.Link | ||
} | ||
|
||
rs = append(rs, rt) | ||
} | ||
|
||
return rs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.