Skip to content

Commit

Permalink
agent: Add metric value to route info
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Sep 20, 2024
1 parent b6f693d commit 44e24e0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions agent/driver/network/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func GetRoutes() ([]network.Route, error) {
Destination: r.Destination,
Netmask: r.Netmask,
NextHop: r.NextHop,
Metric: r.Metric,
})
}

Expand Down
13 changes: 11 additions & 2 deletions agent/lib/routes/routesParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type RouteStruct struct {
Destination string
Netmask string
NextHop string
Metric int
}

// Windows route output format is always like this:
Expand Down Expand Up @@ -57,6 +58,7 @@ func GetWindowsRoutes(getOnlyDefaults bool) ([]RouteStruct, error) {
netmaskField = 1 // field containing string dotted netmask
gatewayField = 2 // field containing string dotted gateway IP address
interfaceField = 3 // field containing string dotted interface IP address
metricField = 4 // field containing string metric
)

ipRegex := regexp.MustCompile(`^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})`)
Expand Down Expand Up @@ -87,11 +89,14 @@ func GetWindowsRoutes(getOnlyDefaults bool) ([]RouteStruct, error) {
continue
}

metric, _ := strconv.Atoi(fields[metricField])

routes = append(routes, RouteStruct{
Interface: fields[interfaceField],
Destination: fields[destinationField],
Netmask: fields[netmaskField],
NextHop: strings.ToLower(fields[gatewayField]),
Interface: fields[interfaceField],
Metric: metric,
})
}
if strings.HasPrefix(line, "=======") {
Expand Down Expand Up @@ -161,11 +166,12 @@ func GetLinuxRoutes(getOnlyDefaults bool) ([]RouteStruct, error) {
interfaceField = 0 // field containing string interface name
destinationField = 1 // field containing hex destination address
gatewayField = 2 // field containing hex gateway address
metricField = 6 // field containing int metric
maskField = 7 // field containing hex mask
)
scanner := bufio.NewScanner(bytes.NewReader(readAll))

// Skip header line
// Skip header line1
if !scanner.Scan() {
err := scanner.Err()
if err != nil {
Expand Down Expand Up @@ -222,11 +228,14 @@ func GetLinuxRoutes(getOnlyDefaults bool) ([]RouteStruct, error) {
nextHopStr = "on-link"
}

metric, _ := strconv.Atoi(tokens[metricField])

routes = append(routes, RouteStruct{
Interface: tokens[interfaceField],
Destination: destination.String(),
Netmask: netmask.String(),
NextHop: nextHopStr,
Metric: metric,
})
}

Expand Down
3 changes: 3 additions & 0 deletions agent/pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,9 @@ const docTemplate = `{
"destination": {
"type": "string"
},
"metric": {
"type": "integer"
},
"netmask": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions agent/pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,9 @@
"destination": {
"type": "string"
},
"metric": {
"type": "integer"
},
"netmask": {
"type": "string"
},
Expand Down
2 changes: 2 additions & 0 deletions agent/pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ definitions:
properties:
destination:
type: string
metric:
type: integer
netmask:
type: string
next_hop:
Expand Down
1 change: 1 addition & 0 deletions agent/pkg/api/rest/model/onprem/network/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type Route struct {
Destination string `json:"destination"`
Netmask string `json:"netmask"`
NextHop string `json:"next_hop"`
Metric int `json:"metric"`
}

0 comments on commit 44e24e0

Please sign in to comment.