Skip to content

Commit

Permalink
Add flag to always run post-script in apply command
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Mar 24, 2024
1 parent bbc3708 commit d14705f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ import (
"github.com/spf13/cobra"
)

// applyCmd represents the apply command
var alwaysRunPostScript bool

var applyCmd = &cobra.Command{
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 {
needPostScript := true
if ip.ExistsNetns(netns) {
slog.Warn("netns is already exists", "name", netns)
needPostScript = false
} else {
slog.Info("create netns", "name", netns)
err := ip.AddNetns(netns)
Expand Down Expand Up @@ -68,15 +71,22 @@ var applyCmd = &cobra.Command{
return err
}

err = RunPostScript(netns, values.PostScript)
if err != nil {
return err
if needPostScript || alwaysRunPostScript {
err = RunPostScript(netns, values.PostScript)
if err != nil {
return err
}
}
}
return nil
},
}

func init() {
rootCmd.AddCommand(applyCmd)
applyCmd.Flags().BoolVarP(&alwaysRunPostScript, "always-run-post-script", "R", false, "always run post-script. by default, runs only when a netns is created.")
}

type IpCommand interface {
SetLinkUp(name string) error
ShowLink(name string) (*iproute2.Link, error)
Expand Down Expand Up @@ -301,10 +311,6 @@ func SetupVethDevices(netns string, devices map[string]config.VethDevice) error
return nil
}

func init() {
rootCmd.AddCommand(applyCmd)
}

func RunPostScript(netns string, script string) error {
if script == "" {
return nil
Expand Down

0 comments on commit d14705f

Please sign in to comment.