Skip to content

Commit

Permalink
remove duplicated vethpair
Browse files Browse the repository at this point in the history
  • Loading branch information
t1anz0ng committed Aug 3, 2022
1 parent 8571932 commit 886d9b4
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asset/* filter=lfs diff=lfs merge=lfs -text
Binary file modified asset/sample-table.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified asset/sample-term.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified asset/sample.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 12 additions & 11 deletions cmd/iftree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ Example:
generate table output
# sudo iftree --table
-d, --debug print debug message
-t, --table output in table
-g, --graph output in graphviz dot language(https://graphviz.org/doc/info/lang.html
-O, --output string graph output name/path (default "output")
-T, --gtype string graph output type, "jpg", "png", "svg", "dot" (graphviz dot language) default "png"
-d, --debug print debug message
-t, --table output in table
-g, --graph output in graphviz dot language(https://graphviz.org/doc/info/lang.html
-O, --output string graph output name/path (default "output")
-T, --gtype string graph output type, "jpg", "png", "svg", "dot" (graphviz dot language) default "png"
-a, --all show all veths, including unused.
Help Options:
-h, --help Show this help message
Expand Down Expand Up @@ -205,6 +206,9 @@ func main() {
case "dot":
_, err = io.Copy(os.Stdout, &buf)
case "jpg", "png", "svg":
if !pflag.CommandLine.Changed("output") && !pflag.CommandLine.Changed("gtype") {
log.Warn(`default output dst file: "output.png"`)
}
graph, errG := graphviz.ParseBytes(buf.Bytes())
if errG != nil {
log.Fatal(errG)
Expand Down Expand Up @@ -237,14 +241,11 @@ func main() {
if err != nil {
log.Fatal(err)
}
if err := formatter.TableParis(os.Stdout, vpairs); err != nil {
log.Fatal(err)
if *oUnusedVeths {
formatter.TableParis(os.Stdout, vpairs)
}
return
}

if err := formatter.Print(os.Stdout, vm, netNsMap, vpairs, *oUnusedVeths); err != nil {
log.Fatal(err)
}

formatter.Print(os.Stdout, vm, netNsMap, vpairs, *oUnusedVeths)
}
45 changes: 35 additions & 10 deletions pkg/formatter/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@ import (
)

var (
defaultStyle = lipgloss.NewStyle()
// styles

text = lipgloss.AdaptiveColor{Light: "#000000", Dark: "#FFFFFF"}
special = lipgloss.AdaptiveColor{Light: "#43BF6D", Dark: "#73F59F"}

mainStype = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63"))
tableStype = lipgloss.NewStyle()
basicTextStyle = lipgloss.NewStyle().Foreground(text).Padding(0, 1)
boldItalicTextStyle = lipgloss.NewStyle().
Bold(true).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFFFFF"))
TitleHighlight = lipgloss.NewStyle().
Background(lipgloss.Color("#F25D94")).
MarginTop(1).
MarginLeft(10).
MarginRight(10).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFF7DB"))

textHighlight = lipgloss.NewStyle().
MarginRight(1).
MarginLeft(1).
Padding(0, 1).
Foreground(special)

mainStype = defaultStyle.Copy()

tableStype = defaultStyle.Copy()

vethPairStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63"))
vethPairStyle = defaultStyle.Copy()

bridgeStyle = lipgloss.NewStyle().
MarginRight(5).
Padding(0, 1).
Bold(true).
Italic(true).
Foreground(lipgloss.AdaptiveColor{Light: "#43BF6D", Dark: "#73F59F"})
Expand All @@ -37,6 +54,14 @@ var (
Padding(0, 1).
Italic(false).
Foreground(lipgloss.Color("#FFFFFF"))

unusedVethStyle = lipgloss.NewStyle().
Background(lipgloss.Color("#F25D94")).
MarginLeft(5).
MarginRight(5).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFF7DB"))
)

func colorGrid(xSteps, ySteps int) [][]string {
Expand Down
51 changes: 32 additions & 19 deletions pkg/formatter/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package formatter
import (
"fmt"
"io"

"sort"
"strings"

"github.com/charmbracelet/lipgloss"
Expand All @@ -17,16 +19,13 @@ var (
)

func Table(w io.Writer, m map[string][]pkg.Node) error {

tbStr := strings.Builder{}
t := table.NewWriter()
t.SetOutputMirror(&tbStr)
t.SetTitle(lipgloss.NewStyle().
Bold(true).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFFFFF")).
SetString("bridge <---> veth <---> veth-in container, GROUP BY NetNS").String())
t.AppendHeader(table.Row{"bridge", "netns", "veth", "ifname(container)"})

t.SetTitle(boldItalicTextStyle.SetString("bridge <---> veth <---> veth in container, GROUP BY NetNS").String())
t.AppendHeader(table.Row{"bridge", "netns", "veth", "ifname"})

for bridge, v := range m {
for _, vp := range v {
Expand All @@ -36,14 +35,10 @@ func Table(w io.Writer, m map[string][]pkg.Node) error {
}
c := lipgloss.Color(tableNsColors[id%len(tableNsColors)][0])
t.AppendRow(table.Row{
lipgloss.NewStyle().
Bold(true).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFFFFF")).SetString(bridge),
lipgloss.NewStyle().Bold(true).Foreground(c).SetString(vp.NetNsName),
vp.Veth,
vp.PeerNameInNetns})
bridgeStyle.SetString(bridge),
netNsStyle.Copy().Foreground(c).SetString(vp.NetNsName),
basicTextStyle.SetString(vp.Veth),
basicTextStyle.SetString(vp.PeerNameInNetns)})
t.AppendSeparator()
}
}
Expand All @@ -59,19 +54,37 @@ func Table(w io.Writer, m map[string][]pkg.Node) error {
return nil
}

func TableParis(w io.WriteCloser, vpairs []pkg.Node) error {
func TableParis(w io.WriteCloser, vpairs []pkg.Node) {
tbStr := strings.Builder{}
t := table.NewWriter()
t.SetOutputMirror(&tbStr)
t.SetTitle("unused veth pairs (experimental)")

// (experimental)
t.SetTitle("unused veth pairs")
t.AppendHeader(table.Row{"veth", "pair"})
visited := make(map[string]struct{})
for _, v := range vpairs {
t.AppendRow(table.Row{v.Veth, v.Peer})
h := hashVethpair(v.Veth, v.Peer)
if _, ok := visited[h]; ok {
continue
}
t.AppendRow(table.Row{
basicTextStyle.SetString(v.Veth),
basicTextStyle.SetString(v.Peer)})
t.AppendSeparator()
visited[h] = struct{}{}
}
t.SetAutoIndex(true)
t.SetStyle(table.StyleRounded)
t.Render()
fmt.Fprintln(w, tbStr.String())
return nil
}

func hashVethpair(a, b string) string {
// FIXME, replace with a better hash function
res := []rune(a + b)
sort.Slice(res, func(i, j int) bool {
return res[i] < res[j]
})
return string(res)
}
58 changes: 23 additions & 35 deletions pkg/formatter/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@ import (
"github.com/t1anz0ng/iftree/pkg"
)

func Print(w io.Writer, vm map[string][]pkg.Node,
netNsMap map[int]string,
vpairs []pkg.Node,
all bool) error {
func Print(w io.Writer, vm map[string][]pkg.Node, netNsMap map[int]string, vpairs []pkg.Node, all bool) {

var content strings.Builder
var vpair strings.Builder
var contents []string

lw := list.NewWriter()
lw.SetOutputMirror(&content)
fmt.Fprintln(&content, lipgloss.NewStyle().
Background(lipgloss.Color("#F25D94")).
MarginLeft(10).
MarginRight(10).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFF7DB")).
SetString("Bridge <---->veth pair"))
fmt.Fprintln(&content, TitleHighlight.SetString("Bridge <----> veth pair"))
for k, v := range vm {
master, err := netlink.LinkByName(k)
if err != nil {
Expand All @@ -51,7 +41,9 @@ func Print(w io.Writer, vm map[string][]pkg.Node,
}
lw.AppendItem(
vethStyle.SetString(
fmt.Sprintf("%s\t%s", p.Veth, p.PeerNameInNetns)).String())
fmt.Sprintf("%s\t%s",
basicTextStyle.SetString(p.Veth),
basicTextStyle.SetString(p.PeerNameInNetns))).String())
}
}
if f {
Expand All @@ -64,33 +56,29 @@ func Print(w io.Writer, vm map[string][]pkg.Node,
lw.SetStyle(list.StyleConnectedRounded)
lw.Render()

var contents []string
contents = append(contents, mainStype.Render(content.String()))

if all {
lw.Reset()
lw.SetOutputMirror(&vpair)
var vpair strings.Builder

fmt.Fprintln(&vpair, unusedVethStyle.SetString("unused veth pairs"))

visited := make(map[string]struct{})

fmt.Fprintln(&vpair, lipgloss.NewStyle().
Background(lipgloss.Color("#F25D94")).
MarginLeft(5).
MarginRight(5).
Padding(0, 1).
Italic(true).
Foreground(lipgloss.Color("#FFF7DB")).
SetString("unused veth pairs"))
lw.SetStyle(list.StyleConnectedRounded)
for _, veth := range vpairs {
lw.AppendItem(fmt.Sprintf("%s%s%s", veth.Veth,
lipgloss.NewStyle().
MarginRight(1).
MarginLeft(1).
Padding(0, 1).
Foreground(special).SetString("<----->"),
veth.Peer))
h := hashVethpair(veth.Veth, veth.Peer)
if _, ok := visited[h]; ok {
continue
}

fmt.Fprintf(&vpair, "%s%s%s",
basicTextStyle.SetString(veth.Veth),
textHighlight.SetString("<----->"),
basicTextStyle.SetString(veth.Peer))
visited[h] = struct{}{}
}
lw.Render()

contents = append(contents, vethPairStyle.Render(vpair.String()))
}
fmt.Fprintln(w, lipgloss.JoinVertical(lipgloss.Top, contents...))
return nil
}
6 changes: 3 additions & 3 deletions pkg/netutil/netutil.go → pkg/netutil/netutil_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ func listNetNsPath() ([]string, error) {
// GetPeerInNs enter target netns to get veth peer's name
// root needed
func GetPeerInNs(ns string, origin netns.NsHandle, peerIdx int) (netlink.Link, error) {
return NetnsGetName(ns, origin, func() (netlink.Link, error) {
return netnsGetName(ns, origin, func() (netlink.Link, error) {
return netlink.LinkByIndex(peerIdx)
})
}

func GetLoInNs(ns string, origin netns.NsHandle) (netlink.Link, error) {
return NetnsGetName(ns, origin, func() (netlink.Link, error) {
return netnsGetName(ns, origin, func() (netlink.Link, error) {
return netlink.LinkByName("lo")
})
}

func NetnsGetName(ns string, origin netns.NsHandle, fn func() (netlink.Link, error)) (link netlink.Link, err error) {
func netnsGetName(ns string, origin netns.NsHandle, fn func() (netlink.Link, error)) (link netlink.Link, err error) {
hd, err := netns.GetFromPath(ns)
if err != nil {
return nil, err
Expand Down

0 comments on commit 886d9b4

Please sign in to comment.