Skip to content

Commit

Permalink
Make cleanup on shutdown optional (#327)
Browse files Browse the repository at this point in the history
* Make cleanup on shutdown optional

* Make clean-up on shutdown the default

* Sync cmdline help and docs
  • Loading branch information
Alex Stockinger authored Mar 28, 2023
1 parent 12ad275 commit 2fa4ae7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/kg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var cmd = &cobra.Command{

var (
backend string
cleanUp bool
cleanUpIface bool
createIface bool
cni bool
Expand Down Expand Up @@ -126,6 +127,7 @@ var (

func init() {
cmd.Flags().StringVar(&backend, "backend", k8s.Backend, fmt.Sprintf("The backend for the mesh. Possible values: %s", availableBackends))
cmd.Flags().BoolVar(&cleanUp, "clean-up", true, "Should kilo clean up network modifications on shutdown?")
cmd.Flags().BoolVar(&cleanUpIface, "clean-up-interface", false, "Should Kilo delete its interface when it shuts down?")
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?")
Expand Down Expand Up @@ -257,7 +259,7 @@ func runRoot(_ *cobra.Command, _ []string) error {
serviceCIDRs = append(serviceCIDRs, s)
}

m, err := mesh.New(b, enc, gr, hostname, port, s, local, cni, cniPath, iface, cleanUpIface, createIface, mtu, resyncPeriod, prioritisePrivateAddr, iptablesForwardRule, serviceCIDRs, log.With(logger, "component", "kilo"), registry)
m, err := mesh.New(b, enc, gr, hostname, port, s, local, cni, cniPath, iface, cleanUp, cleanUpIface, createIface, mtu, resyncPeriod, prioritisePrivateAddr, iptablesForwardRule, serviceCIDRs, log.With(logger, "component", "kilo"), registry)
if err != nil {
return fmt.Errorf("failed to create Kilo mesh: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions docs/kg.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Available Commands:
Flags:
--backend string The backend for the mesh. Possible values: kubernetes (default "kubernetes")
--clean-up Should kilo clean up network modifications on shutdown? (default true)
--clean-up-interface Should Kilo delete its interface when it shuts down?
--cni Should Kilo manage the node's CNI configuration? (default true)
--cni-path string Path to CNI config. (default "/etc/cni/net.d/10-kilo.conflist")
Expand Down
17 changes: 13 additions & 4 deletions pkg/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
// Mesh is able to create Kilo network meshes.
type Mesh struct {
Backend
cleanup bool
cleanUpIface bool
cni bool
cniPath string
Expand Down Expand Up @@ -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, serviceCIDRs []*net.IPNet, logger log.Logger, registerer prometheus.Registerer) (*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, serviceCIDRs []*net.IPNet, logger log.Logger, registerer prometheus.Registerer) (*Mesh, error) {
if err := os.MkdirAll(kiloPath, 0700); err != nil {
return nil, fmt.Errorf("failed to create directory to store configuration: %v", err)
}
Expand Down Expand Up @@ -117,9 +118,14 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
}
var kiloIface int
if createIface {
kiloIface, _, err = wireguard.New(iface, mtu)
link, err := netlink.LinkByName(iface)
if err != nil {
return nil, fmt.Errorf("failed to create WireGuard interface: %v", err)
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)
Expand Down Expand Up @@ -162,6 +168,7 @@ func New(backend Backend, enc encapsulation.Encapsulator, granularity Granularit
}
mesh := Mesh{
Backend: backend,
cleanup: cleanup,
cleanUpIface: cleanUpIface,
cni: cni,
cniPath: cniPath,
Expand Down Expand Up @@ -257,7 +264,9 @@ func (m *Mesh) Run(ctx context.Context) error {
}
}
}()
defer m.cleanUp()
if m.cleanup {
defer m.cleanUp()
}
resync := time.NewTimer(m.resyncPeriod)
checkIn := time.NewTimer(checkInPeriod)
nw := m.Nodes().Watch()
Expand Down

0 comments on commit 2fa4ae7

Please sign in to comment.