Encode "list-netdevs" output in JSON
This commit is contained in:
1 parent
e611f04f41
commit
9754da15d6
3 files changed
+24
-58
No files matched your search
+7
-50
@@ -1,54 +1,20 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
"github.com/vishvananda/netns"
|
"github.com/vishvananda/netns"
|
||||||
)
|
)
|
||||||
|
|
||||||
var netdevRegExp = regexp.MustCompile(`^([^ ]+?) ([^ ]+?) \[([^]]*)\]$`)
|
|
||||||
|
|
||||||
type Dev struct {
|
type Dev struct {
|
||||||
Name string
|
Name string `json:"Name,omitempty"`
|
||||||
MAC net.HardwareAddr
|
MAC net.HardwareAddr `json:"MAC,omitempty"`
|
||||||
CIDRs []*net.IPNet
|
CIDRs []*net.IPNet `json:"CIDRs,omitempty"`
|
||||||
}
|
|
||||||
|
|
||||||
func (d Dev) String() string {
|
|
||||||
return fmt.Sprintf("%s %s %s", d.Name, d.MAC, d.CIDRs)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseNetDev(netdev string) (Dev, error) {
|
|
||||||
match := netdevRegExp.FindStringSubmatch(netdev)
|
|
||||||
if match == nil {
|
|
||||||
return Dev{}, fmt.Errorf("invalid netdev: %s", netdev)
|
|
||||||
}
|
|
||||||
|
|
||||||
iface := match[1]
|
|
||||||
mac, err := net.ParseMAC(match[2])
|
|
||||||
if err != nil {
|
|
||||||
return Dev{}, fmt.Errorf("cannot parse mac %s: %s", match[2], err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var cidrs []*net.IPNet
|
|
||||||
for _, cidr := range strings.Split(match[3], " ") {
|
|
||||||
if cidr != "" {
|
|
||||||
ip, ipnet, err := net.ParseCIDR(cidr)
|
|
||||||
if err != nil {
|
|
||||||
return Dev{}, fmt.Errorf("cannot parse cidr %s: %s", cidr, err)
|
|
||||||
}
|
|
||||||
ipnet.IP = ip
|
|
||||||
cidrs = append(cidrs, ipnet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Dev{Name: iface, MAC: mac, CIDRs: cidrs}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func LinkToNetDev(link netlink.Link) (Dev, error) {
|
func LinkToNetDev(link netlink.Link) (Dev, error) {
|
||||||
@@ -125,26 +91,17 @@ func GetNetDevsByVethPeerIds(processID int, peerIDs []int) ([]Dev, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var netdevs []Dev
|
var netdevs []Dev
|
||||||
|
|
||||||
peersStr := make([]string, len(peerIDs))
|
peersStr := make([]string, len(peerIDs))
|
||||||
for i, id := range peerIDs {
|
for i, id := range peerIDs {
|
||||||
peersStr[i] = strconv.Itoa(id)
|
peersStr[i] = strconv.Itoa(id)
|
||||||
}
|
}
|
||||||
netdevsStr, err := WithNetNSByPid(processID, "list-netdevs", strings.Join(peersStr, ","))
|
nds, err := WithNetNSByPid(processID, "list-netdevs", peersStr...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("list-netdevs failed: %s", err)
|
return nil, fmt.Errorf("list-netdevs failed: %s", err)
|
||||||
}
|
}
|
||||||
for _, netdevStr := range strings.Split(netdevsStr, "\n") {
|
err = json.Unmarshal(nds, &netdevs)
|
||||||
if netdevStr != "" {
|
|
||||||
netdev, err := ParseNetDev(netdevStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("cannot parse netdev %s: %s", netdevStr, err)
|
|
||||||
}
|
|
||||||
netdevs = append(netdevs, netdev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return netdevs, nil
|
return netdevs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the weave bridge interface.
|
// Get the weave bridge interface.
|
||||||
|
|||||||
+4
-4
@@ -59,7 +59,7 @@ func WithNetNSLinkUnsafe(ns netns.NsHandle, ifName string, work func(link netlin
|
|||||||
|
|
||||||
// A safe version of WithNetNS* which creates a process executing
|
// A safe version of WithNetNS* which creates a process executing
|
||||||
// "nsenter --net=<ns-path> weaveutil <cmd> [args]".
|
// "nsenter --net=<ns-path> weaveutil <cmd> [args]".
|
||||||
func WithNetNS(nsPath string, cmd string, args ...string) (string, error) {
|
func WithNetNS(nsPath string, cmd string, args ...string) ([]byte, error) {
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
|
|
||||||
args = append([]string{"--net=" + nsPath, "weaveutil", cmd}, args...)
|
args = append([]string{"--net=" + nsPath, "weaveutil", cmd}, args...)
|
||||||
@@ -67,13 +67,13 @@ func WithNetNS(nsPath string, cmd string, args ...string) (string, error) {
|
|||||||
c.Stdout = &stdout
|
c.Stdout = &stdout
|
||||||
c.Stderr = &stderr
|
c.Stderr = &stderr
|
||||||
if err := c.Run(); err != nil {
|
if err := c.Run(); err != nil {
|
||||||
return "", fmt.Errorf("%s: %s", string(stderr.Bytes()), err)
|
return nil, fmt.Errorf("%s: %s", string(stderr.Bytes()), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(stdout.Bytes()), nil
|
return stdout.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithNetNSByPid(pid int, cmd string, args ...string) (string, error) {
|
func WithNetNSByPid(pid int, cmd string, args ...string) ([]byte, error) {
|
||||||
return WithNetNS(NSPathByPid(pid), cmd, args...)
|
return WithNetNS(NSPathByPid(pid), cmd, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -97,12 +98,12 @@ func configureARP(args []string) error {
|
|||||||
// listNetDevs outputs network ifaces identified by the given indexes
|
// listNetDevs outputs network ifaces identified by the given indexes
|
||||||
// in the format of weavenet.Dev.
|
// in the format of weavenet.Dev.
|
||||||
func listNetDevs(args []string) error {
|
func listNetDevs(args []string) error {
|
||||||
if len(args) != 1 {
|
if len(args) == 0 {
|
||||||
cmdUsage("list-netdevs", "<iface-index>[,<iface-index>]")
|
cmdUsage("list-netdevs", "<iface-index>[ <iface-index>]")
|
||||||
}
|
}
|
||||||
|
|
||||||
indexes := make(map[int]struct{})
|
indexes := make(map[int]struct{})
|
||||||
for _, index := range strings.Split(args[0], ",") {
|
for _, index := range args {
|
||||||
if index != "" {
|
if index != "" {
|
||||||
id, err := strconv.Atoi(index)
|
id, err := strconv.Atoi(index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -117,15 +118,23 @@ func listNetDevs(args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var netdevs []weavenet.Dev
|
||||||
|
|
||||||
for _, link := range links {
|
for _, link := range links {
|
||||||
if _, found := indexes[link.Attrs().Index]; found {
|
if _, found := indexes[link.Attrs().Index]; found {
|
||||||
netdev, err := weavenet.LinkToNetDev(link)
|
netdev, err := weavenet.LinkToNetDev(link)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(netdev)
|
netdevs = append(netdevs, netdev)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nds, err := json.Marshal(netdevs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(string(nds))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user