From 8b63d402dd7113f749db963e50b50e9eab460fab Mon Sep 17 00:00:00 2001 From: buty4649 Date: Wed, 20 Mar 2024 15:22:52 +0900 Subject: [PATCH 1/3] Refactor command flags and their usage in rootCmd --- cmd/apply.go | 7 ++++--- cmd/destroy.go | 6 +++--- cmd/root.go | 36 +++++++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/cmd/apply.go b/cmd/apply.go index 1639b58..15a8a3a 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -30,14 +30,15 @@ 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) { slog.Warn("netns is already exists", "name", netns) } else { + slog.Info("create netns", "name", netns) err := ip.AddNetns(netns) if err != nil { return err diff --git a/cmd/destroy.go b/cmd/destroy.go index e1fb6cd..6ca9e42 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -29,9 +29,9 @@ import ( // destroyCmd represents the destroy command var destroyCmd = &cobra.Command{ - Use: "destroy", - Short: "Destroy netns networks configuration from running system", - Long: "Destroy netns networks configuration from running system", + Use: "destroy", + Short: "Destroy netns networks configuration from running system", + Long: "Destroy netns networks configuration from running system", RunE: func(cmd *cobra.Command, args []string) error { for n := range cfg.Netns { if ip.NetnsExists(n) { diff --git a/cmd/root.go b/cmd/root.go index 4caeaf9..318347c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,9 +34,13 @@ import ( "gitlab.com/greyxor/slogor" ) -var cfgFilePath string -var ipCmdPath string -var debug bool +type Flags struct { + ConfigPath string + IpCmdPath string + Debug, Quiet bool +} + +var flags Flags var cfg *config.Config var ip *iproute2.Iproute2 @@ -47,23 +51,30 @@ var rootCmd = &cobra.Command{ Long: "Easily automate Linux netns networks and configurations via YAML", SilenceUsage: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - logLevel := slog.LevelInfo - if debug { + err := cmd.ValidateFlagGroups() + if err != nil { + return err + } + + var logLevel slog.Level + if flags.Debug { logLevel = slog.LevelDebug } + if flags.Quiet { + logLevel = slog.LevelError + } slog.SetDefault(slog.New(slogor.NewHandler(os.Stdout, slogor.Options{ TimeFormat: "", Level: logLevel, ShowSource: false, }))) - var err error - cfg, err = config.LoadConfig(cfgFilePath) + cfg, err = config.LoadConfig(flags.ConfigPath) if err != nil { return err } - ip = iproute2.New(ipCmdPath) + ip = iproute2.New(flags.IpCmdPath) return nil }, } @@ -82,7 +93,10 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().StringVar(&cfgFilePath, "config", "./netnsplan.yaml", "config file") - rootCmd.PersistentFlags().StringVar(&ipCmdPath, "cmd", "/bin/ip", "ip command path") - rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "debug mode") + rootCmd.PersistentFlags().StringVar(&flags.ConfigPath, "config", "./netnsplan.yaml", "config file") + rootCmd.PersistentFlags().StringVar(&flags.IpCmdPath, "cmd", "/bin/ip", "ip command path") + + rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "debug mode") + rootCmd.PersistentFlags().BoolVarP(&flags.Quiet, "quiet", "q", false, "debug mode") + rootCmd.MarkFlagsMutuallyExclusive("debug", "quiet") } From 472e6545f315fc4a3b80827759830cc954d7dd33 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Wed, 20 Mar 2024 15:58:49 +0900 Subject: [PATCH 2/3] 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) } From 236ce01825d92e2fa4975f536cee28467b1e12a8 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Wed, 20 Mar 2024 16:03:56 +0900 Subject: [PATCH 3/3] Refactor SetupDevice to handle SetLinkUp internally --- cmd/apply.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/apply.go b/cmd/apply.go index 5e9c6df..389028c 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -70,6 +70,11 @@ var applyCmd = &cobra.Command{ } func SetupDevice(name string, addresses []string) error { + err := SetLinkUp(name) + if err != nil { + return err + } + slog.Info("add addresses", "name", name, "addresses", addresses) for _, address := range addresses { @@ -78,12 +83,11 @@ func SetupDevice(name string, addresses []string) error { return err } } - - return SetLinkUp(name) + return nil } func SetLinkUp(name string) error { - slog.Info("link up", "name", "lo", "netns", ip.Netns()) + slog.Info("link up", "name", name, "netns", ip.Netns()) return ip.SetLinkUp(name) } @@ -148,11 +152,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig) return err } ip.IntoNetns(netns, func() error { - err = SetupDevice(peerName, values.Peer.Addresses) - if err != nil { - return err - } - return nil + return SetupDevice(peerName, values.Peer.Addresses) }) } else { SetupDevice(peerName, values.Peer.Addresses)