From 472e6545f315fc4a3b80827759830cc954d7dd33 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Wed, 20 Mar 2024 15:58:49 +0900 Subject: [PATCH] Improve logging output for network configuration setup --- cmd/apply.go | 23 ++++++++++++++--------- iproute2/iproute2.go | 7 +++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/cmd/apply.go b/cmd/apply.go index 15a8a3a..5e9c6df 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -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) { @@ -70,6 +70,8 @@ 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 { @@ -77,17 +79,18 @@ func SetupDevice(name string, addresses []string) error { } } - 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") }) } @@ -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 @@ -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 diff --git a/iproute2/iproute2.go b/iproute2/iproute2.go index 6abaf43..798ab5e 100644 --- a/iproute2/iproute2.go +++ b/iproute2/iproute2.go @@ -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) }