Pass bootstrap into newNetwork, will simplify future changes

This commit is contained in:
Brian Picciano 2025-01-05 12:25:02 +01:00
parent 4bce0e3fa0
commit 9848394ffc

View File

@ -210,6 +210,7 @@ func newNetwork(
stateDir toolkit.Dir,
runtimeDir toolkit.Dir,
dirsMayExist bool,
currBootstrap bootstrap.Bootstrap,
opts *Opts,
) (
*network, error,
@ -224,6 +225,7 @@ func newNetwork(
stateDir: stateDir,
runtimeDir: runtimeDir,
opts: opts.withDefaults(),
currBootstrap: currBootstrap,
workerCtx: ctx,
workerCancel: cancel,
}
@ -277,6 +279,11 @@ func (constructorsImpl) load(
) (
Network, error,
) {
currBootstrap, err := loadBootstrapFromStateDir(stateDir.Path)
if err != nil {
return nil, fmt.Errorf("loading bootstrap from state dir: %w", err)
}
n, err := newNetwork(
ctx,
logger,
@ -285,18 +292,14 @@ func (constructorsImpl) load(
stateDir,
runtimeDir,
true,
currBootstrap,
opts,
)
if err != nil {
return nil, fmt.Errorf("instantiating Network: %w", err)
}
currBootstrap, err := loadBootstrapFromStateDir(n.stateDir.Path)
if err != nil {
return nil, fmt.Errorf("loading bootstrap from state dir: %w", err)
}
if err := n.initialize(ctx, currBootstrap, false); err != nil {
if err := n.initialize(ctx, false); err != nil {
return nil, fmt.Errorf("initializing with bootstrap: %w", err)
}
@ -323,6 +326,7 @@ func (constructorsImpl) join(
stateDir,
runtimeDir,
false,
joiningBootstrap.Bootstrap,
opts,
)
if err != nil {
@ -335,7 +339,7 @@ func (constructorsImpl) join(
return nil, fmt.Errorf("importing secrets: %w", err)
}
if err := n.initialize(ctx, joiningBootstrap.Bootstrap, false); err != nil {
if err := n.initialize(ctx, false); err != nil {
return nil, fmt.Errorf("initializing with bootstrap: %w", err)
}
@ -363,6 +367,17 @@ func (constructorsImpl) create(
garageRPCSecret := toolkit.RandStr(32)
hostBootstrap, err := bootstrap.New(
nebulaCACreds,
creationParams,
map[nebula.HostName]bootstrap.Host{},
hostName,
ipNet.FirstAddr(),
)
if err != nil {
return nil, fmt.Errorf("initializing bootstrap data: %w", err)
}
n, err := newNetwork(
ctx,
logger,
@ -371,6 +386,7 @@ func (constructorsImpl) create(
stateDir,
runtimeDir,
false,
hostBootstrap,
opts,
)
if err != nil {
@ -395,18 +411,7 @@ func (constructorsImpl) create(
return nil, fmt.Errorf("setting nebula CA signing key secret: %w", err)
}
hostBootstrap, err := bootstrap.New(
nebulaCACreds,
creationParams,
map[nebula.HostName]bootstrap.Host{},
hostName,
ipNet.FirstAddr(),
)
if err != nil {
return nil, fmt.Errorf("initializing bootstrap data: %w", err)
}
if err := n.initialize(ctx, hostBootstrap, true); err != nil {
if err := n.initialize(ctx, true); err != nil {
return nil, fmt.Errorf("initializing with bootstrap: %w", err)
}
@ -447,10 +452,12 @@ func (n *network) periodically(
func (n *network) initialize(
ctx context.Context,
prevBootstrap bootstrap.Bootstrap,
isCreate bool,
) error {
prevThisHost := prevBootstrap.ThisHost()
var (
prevBootstrap = n.currBootstrap
prevThisHost = prevBootstrap.ThisHost()
)
// we update this Host's data using whatever configuration has been provided
// by the daemon config. This way the network has the most up-to-date