Make cleanup on shutdown optional

This commit is contained in:
Alex Stockinger 2022-07-28 07:31:21 +00:00
parent cb238c85a1
commit df5f79dccc
2 changed files with 17 additions and 6 deletions

View File

@ -95,6 +95,7 @@ var cmd = &cobra.Command{
var (
backend string
cleanUp bool
cleanUpIface bool
createIface bool
cni bool
@ -125,7 +126,8 @@ var (
func init() {
cmd.Flags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
cmd.Flags().BoolVar(&cleanUpIface, "clean-up-interface", false, "Should Kilo delete its interface when it shuts down?")
cmd.Flags().BoolVar(&cleanUp, "clean-up", false, "Clean up network modifications on shutdown.")
cmd.Flags().BoolVar(&cleanUpIface, "clean-up-interface", false, "Should Kilo delete its interface when it shuts down? Only relevant when cleanup is true.")
cmd.Flags().BoolVar(&createIface, "create-interface", true, "Should kilo create an interface on startup?")
cmd.Flags().BoolVar(&cni, "cni", true, "Should Kilo manage the node's CNI configuration?")
cmd.Flags().StringVar(&cniPath, "cni-path", mesh.DefaultCNIPath, "Path to CNI config.")
@ -245,7 +247,7 @@ func runRoot(_ *cobra.Command, _ []string) error {
if port < 1 || port > 1<<16-1 {
return fmt.Errorf("invalid port: port mus be in range [%d:%d], but got %d", 1, 1<<16-1, port)
}
m, err := mesh.New(b, enc, gr, hostname, port, s, local, cni, cniPath, iface, cleanUpIface, createIface, mtu, resyncPeriod, prioritisePrivateAddr, iptablesForwardRule, log.With(logger, "component", "kilo"))
m, err := mesh.New(b, enc, gr, hostname, port, s, local, cni, cniPath, iface, cleanUp, cleanUpIface, createIface, mtu, resyncPeriod, prioritisePrivateAddr, iptablesForwardRule, log.With(logger, "component", "kilo"))
if err != nil {
return fmt.Errorf("failed to create Kilo mesh: %v", err)
}

View File

@ -51,6 +51,7 @@ const (
// Mesh is able to create Kilo network meshes.
type Mesh struct {
Backend
cleanup bool
cleanUpIface bool
cni bool
cniPath string
@ -88,7 +89,7 @@ type Mesh struct {
}
// New returns a new Mesh instance.
func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port int, subnet *net.IPNet, local, cni bool, cniPath, iface string, cleanUpIface bool, createIface bool, mtu uint, resyncPeriod time.Duration, prioritisePrivateAddr, iptablesForwardRule bool, logger log.Logger) (*Mesh, error) {
func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularity, hostname string, port int, subnet *net.IPNet, local, cni bool, cniPath, iface string, cleanup bool, cleanUpIface bool, createIface bool, mtu uint, resyncPeriod time.Duration, prioritisePrivateAddr, iptablesForwardRule bool, logger log.Logger) (*Mesh, error) {
if err := os.MkdirAll(kiloPath, 0700); err != nil {
return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
}
@ -117,10 +118,15 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
}
var kiloIface int
if createIface {
link, err := netlink.LinkByName(iface)
if err != nil {
kiloIface, _, err = wireguard.New(iface, mtu)
if err != nil {
return nil, fmt.Errorf("failed to create WireGuard interface: %v", err)
}
} else {
kiloIface = link.Attrs().Index
}
} else {
link, err := netlink.LinkByName(iface)
if err != nil {
@ -162,6 +168,7 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
}
return &Mesh{
Backend: backend,
cleanup: cleanup,
cleanUpIface: cleanUpIface,
cni: cni,
cniPath: cniPath,
@ -248,7 +255,9 @@ func (m *Mesh) Run(ctx context.Context) error {
}
}
}()
if m.cleanup {
defer m.cleanUp()
}
resync := time.NewTimer(m.resyncPeriod)
checkIn := time.NewTimer(checkInPeriod)
nw := m.Nodes().Watch()