Small changes to how process reloading works

This commit is contained in:
Brian Picciano 2024-10-24 21:19:58 +02:00
parent 5c41cedea3
commit 88ffa97c0f
2 changed files with 37 additions and 26 deletions

View File

@ -48,10 +48,9 @@ func (o *Opts) withDefaults() *Opts {
// - dnsmasq // - dnsmasq
// - garage (0 or more, depending on configured storage allocations) // - garage (0 or more, depending on configured storage allocations)
type Children struct { type Children struct {
logger *mlog.Logger logger *mlog.Logger
networkConfig daecommon.NetworkConfig runtimeDir toolkit.Dir
runtimeDir toolkit.Dir opts Opts
opts Opts
pmux *pmuxlib.Pmux pmux *pmuxlib.Pmux
} }
@ -80,10 +79,9 @@ func New(
} }
c := &Children{ c := &Children{
logger: logger, logger: logger,
networkConfig: networkConfig, runtimeDir: runtimeDir,
runtimeDir: runtimeDir, opts: *opts,
opts: *opts,
} }
pmuxConfig, err := c.newPmuxConfig( pmuxConfig, err := c.newPmuxConfig(
@ -112,13 +110,14 @@ func New(
return c, nil return c, nil
} }
// RestartDNSMasq rewrites the dnsmasq config and restarts the process.
//
// TODO block until process has been confirmed to have come back up // TODO block until process has been confirmed to have come back up
// successfully. // successfully.
func (c *Children) RestartDNSMasq(hostBootstrap bootstrap.Bootstrap) error { func (c *Children) reloadDNSMasq(
networkConfig daecommon.NetworkConfig,
hostBootstrap bootstrap.Bootstrap,
) error {
_, err := dnsmasqWriteConfig( _, err := dnsmasqWriteConfig(
c.runtimeDir.Path, c.networkConfig, hostBootstrap, c.runtimeDir.Path, networkConfig, hostBootstrap,
) )
if err != nil { if err != nil {
return fmt.Errorf("writing new dnsmasq config: %w", err) return fmt.Errorf("writing new dnsmasq config: %w", err)
@ -128,40 +127,50 @@ func (c *Children) RestartDNSMasq(hostBootstrap bootstrap.Bootstrap) error {
return nil return nil
} }
// RestartNebula rewrites the nebula config and restarts the process. func (c *Children) reloadNebula(
// ctx context.Context,
// TODO block until process has been confirmed to have come back up networkConfig daecommon.NetworkConfig,
// successfully. hostBootstrap bootstrap.Bootstrap,
func (c *Children) RestartNebula(hostBootstrap bootstrap.Bootstrap) error { ) error {
_, err := nebulaWriteConfig( _, err := nebulaWriteConfig(
c.runtimeDir.Path, c.networkConfig, hostBootstrap, c.runtimeDir.Path, networkConfig, hostBootstrap,
) )
if err != nil { if err != nil {
return fmt.Errorf("writing a new nebula config: %w", err) return fmt.Errorf("writing a new nebula config: %w", err)
} }
c.pmux.Restart("nebula") c.pmux.Restart("nebula")
c.logger.Info(ctx, "Waiting for nebula VPN to come online")
if err := waitForNebula(ctx, c.logger, hostBootstrap); err != nil {
return fmt.Errorf("waiting for nebula to start: %w", err)
}
return nil return nil
} }
// Reload applies a ReloadDiff to the Children, using the given bootstrap which // Reload applies a ReloadDiff to the Children, using the given bootstrap which
// must be the same one which was passed to CalculateReloadDiff. // must be the same one which was passed to CalculateReloadDiff.
func (c *Children) Reload( func (c *Children) Reload(
ctx context.Context, newBootstrap bootstrap.Bootstrap, diff ReloadDiff, ctx context.Context,
newNetworkConfig daecommon.NetworkConfig,
newBootstrap bootstrap.Bootstrap,
diff ReloadDiff,
) error { ) error {
var errs []error var errs []error
if diff.DNSChanged { if diff.DNSChanged {
c.logger.Info(ctx, "Restarting dnsmasq to account for bootstrap changes") c.logger.Info(ctx, "Reloading dnsmasq to account for bootstrap changes")
if err := c.RestartDNSMasq(newBootstrap); err != nil { if err := c.reloadDNSMasq(newNetworkConfig, newBootstrap); err != nil {
errs = append(errs, fmt.Errorf("restarting dnsmasq: %w", err)) errs = append(errs, fmt.Errorf("reloading dnsmasq: %w", err))
} }
} }
if diff.NebulaChanged { if diff.NebulaChanged {
c.logger.Info(ctx, "Restarting nebula to account for bootstrap changes") c.logger.Info(ctx, "Reloading nebula to account for bootstrap changes")
if err := c.RestartNebula(newBootstrap); err != nil { err := c.reloadNebula(ctx, newNetworkConfig, newBootstrap)
errs = append(errs, fmt.Errorf("restarting nebula: %w", err)) if err != nil {
errs = append(errs, fmt.Errorf("reloading nebula: %w", err))
} }
} }

View File

@ -602,10 +602,12 @@ func (n *network) reloadWithHosts(
n.logger.Info(ctx, "Bootstrap has changed, storing new bootstrap") n.logger.Info(ctx, "Bootstrap has changed, storing new bootstrap")
n.l.Lock() n.l.Lock()
networkConfig := n.networkConfig
n.currBootstrap = newBootstrap n.currBootstrap = newBootstrap
n.l.Unlock() n.l.Unlock()
if err := n.children.Reload(ctx, newBootstrap, diff); err != nil { err = n.children.Reload(ctx, networkConfig, newBootstrap, diff)
if err != nil {
return fmt.Errorf("reloading child processes (diff:%+v): %w", diff, err) return fmt.Errorf("reloading child processes (diff:%+v): %w", diff, err)
} }