Skip to content

Commit 0a12528

Browse files
committed
feat: net list
1 parent 7cbbaf6 commit 0a12528

4 files changed

Lines changed: 108 additions & 9 deletions

File tree

cmd/net.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ func netFunc(cmd *cobra.Command, args []string) {
4646
}
4747
}
4848
case "list":
49-
// if len(args) != 2 {
50-
// errFormat(args)
51-
// } else {
52-
// if list, err := veth.List(); err != nil {
53-
// errPrint(err)
54-
// } else {
55-
// fmt.Print(list)
56-
// }
57-
// }
49+
if len(args) != 2 {
50+
errFormat(args)
51+
} else {
52+
if list, err := veth.List(args[1]); err != nil {
53+
errPrint(err)
54+
} else {
55+
fmt.Print(list)
56+
}
57+
}
5858
case "set-ip":
5959
case "up":
6060
case "down:":
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
┌──────────────────┬─────┬──────────────────┬─────────────────┐
2+
│IFACE │STATE│IP │MAC │
3+
├──────────────────┼─────┼──────────────────┼─────────────────┤
4+
│lo │down │- │- │
5+
└──────────────────┴─────┴──────────────────┴─────────────────┘

integration-test/veth/test.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ cleanup_two_namespace() {
2626
../nsctl ns delete test-2
2727
}
2828

29+
list_test_1_lo() {
30+
output=$(../nsctl net list test-1)
31+
expect=$(cat list_test_1_lo.txt)
32+
33+
diff "List test 1 lo" "$output" "$expect"
34+
}
35+
2936
connect_test() {
3037
output=$(../nsctl net connect test-1 test-2)
3138
expect=$(cat connect_test.txt)
@@ -50,6 +57,7 @@ disconnect_test() {
5057
main() {
5158
setup_two_namespace
5259

60+
list_test_1_lo
5361
connect_test
5462
connect_test_error
5563
disconnect_test

veth/list.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package veth
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/Alonza0314/nsctl/namespace"
8+
"github.com/vishvananda/netlink"
9+
"github.com/vishvananda/netns"
10+
)
11+
12+
func List(ns string) (string, error) {
13+
_, originCloseFunc, err := namespace.GetOriginNs()
14+
if err != nil {
15+
return "", err
16+
}
17+
defer originCloseFunc()
18+
19+
targetNsFd, targetNsCloseFunc, err := namespace.GetNsFd(ns)
20+
if err != nil {
21+
return "", fmt.Errorf("failed to namespace %s file descriptor: %v", ns, err)
22+
}
23+
defer targetNsCloseFunc()
24+
25+
if err := netns.Set(targetNsFd); err != nil {
26+
return "", fmt.Errorf("failed to set to namespace %s: %v", ns, err)
27+
}
28+
29+
links, err := netlink.LinkList()
30+
if err != nil {
31+
return "", fmt.Errorf("failed to get net links in namespace %s: %v", ns, err)
32+
}
33+
34+
return printTable(links), nil
35+
}
36+
37+
func printTable(links []netlink.Link) string {
38+
var b strings.Builder
39+
40+
ifWidth, stateWidth, ipWidth, macWidth := 18, 5, 18, 17
41+
42+
b.WriteString("┌" + strings.Repeat("─", ifWidth) + "┬" + strings.Repeat("─", stateWidth) + "┬" + strings.Repeat("─", ipWidth) + "┬" + strings.Repeat("─", macWidth) + "┐\n")
43+
b.WriteString(fmt.Sprintf("│%-*.*s│%-*.*s│%-*.*s│%-*.*s│\n", ifWidth, ifWidth, "IFACE", stateWidth, stateWidth, "STATE", ipWidth, ipWidth, "IP", macWidth, macWidth, "MAC"))
44+
b.WriteString("├" + strings.Repeat("─", ifWidth) + "┼" + strings.Repeat("─", stateWidth) + "┼" + strings.Repeat("─", ipWidth) + "┼" + strings.Repeat("─", macWidth) + "┤\n")
45+
46+
for _, link := range links {
47+
attrs := link.Attrs()
48+
if attrs == nil {
49+
continue
50+
}
51+
52+
if attrs.Name == "gre0" || attrs.Name == "gretap0" || attrs.Name == "erspan0" || attrs.Name == "ip6gre0" || attrs.Name == "ip6gretap0" {
53+
continue
54+
}
55+
56+
state := attrs.OperState.String()
57+
if state == "" {
58+
state = "-"
59+
}
60+
61+
ip := "-"
62+
if addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL); err == nil && len(addrs) > 0 {
63+
ipList := make([]string, 0, len(addrs))
64+
for _, addr := range addrs {
65+
if addr.IPNet != nil {
66+
ipList = append(ipList, addr.IPNet.IP.String())
67+
}
68+
}
69+
if len(ipList) > 0 {
70+
ip = strings.Join(ipList, ",")
71+
}
72+
}
73+
74+
mac := attrs.HardwareAddr.String()
75+
if mac == "" {
76+
mac = "-"
77+
}
78+
79+
b.WriteString(fmt.Sprintf("│%-*.*s│%-*.*s│%-*.*s│%-*.*s│\n", ifWidth, ifWidth, attrs.Name, stateWidth, stateWidth, state, ipWidth, ipWidth, ip, macWidth, macWidth, mac))
80+
81+
}
82+
83+
b.WriteString("└" + strings.Repeat("─", ifWidth) + "┴" + strings.Repeat("─", stateWidth) + "┴" + strings.Repeat("─", ipWidth) + "┴" + strings.Repeat("─", macWidth) + "┘\n")
84+
85+
return b.String()
86+
}

0 commit comments

Comments
 (0)