Skip to content

Commit

Permalink
Improve logging output for network configuration setup
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Mar 20, 2024
1 parent 8b63d40 commit 472e654
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
23 changes: 14 additions & 9 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (

// applyCmd represents the apply command
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply netns networks configuration to running system",
Long: "Apply netns networks configuration to running system",
Use: "apply",
Short: "Apply netns networks configuration to running system",
Long: "Apply netns networks configuration to running system",
RunE: func(cmd *cobra.Command, args []string) error {
for netns, values := range cfg.Netns {
if ip.NetnsExists(netns) {
Expand Down Expand Up @@ -70,24 +70,27 @@ var applyCmd = &cobra.Command{
}

func SetupDevice(name string, addresses []string) error {
slog.Info("add addresses", "name", name, "addresses", addresses)

for _, address := range addresses {
err := ip.AddAddress(name, address)
if err != nil {
return err
}
}

err := ip.SetLinkUp(name)
if err != nil {
return err
}
return SetLinkUp(name)
}

return nil
func SetLinkUp(name string) error {
slog.Info("link up", "name", "lo", "netns", ip.Netns())

return ip.SetLinkUp(name)
}

func SetupLoopback(netns string) error {
return ip.IntoNetns(netns, func() error {
return ip.SetLinkUp("lo")
return SetLinkUp("lo")
})
}

Expand All @@ -108,6 +111,7 @@ func SetupEthernets(netns string, ethernets map[string]config.EthernetConfig) er
func SetupDummyDevices(netns string, devices map[string]config.EthernetConfig) error {
for name, values := range devices {
ip.IntoNetns(netns, func() error {
slog.Info("add dummy device", "name", name, "netns", netns)
err := ip.AddDummyDevice(name)
if err != nil {
return err
Expand All @@ -124,6 +128,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig)
peerName := values.Peer.Name
peerNetns := values.Peer.Netns

slog.Info("add veth device", "name", name, "netns", netns, "peer name", peerName, "peer netns", peerNetns)
err := ip.AddVethDevice(name, peerName)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions iproute2/iproute2.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ func (i *Iproute2) IntoNetns(netns string, fn func() error) error {
return err
}

func (i *Iproute2) Netns() string {
if i.useNetns {
return i.netns
}
return ""
}

func (i *Iproute2) NetnsExists(name string) bool {
return slices.Contains(i.ListNetns(), name)
}
Expand Down

0 comments on commit 472e654

Please sign in to comment.