isle/go/daemon/config.go

77 lines
1.8 KiB
Go

package daemon
import (
"context"
"fmt"
"isle/bootstrap"
"isle/daemon/daecommon"
"isle/daemon/network"
)
// getDeprecatedNetworkConfig will return the NetworkConfig configured in the
// deprecated configuration format, or nil if the deprecated format is not used.
//
// In the deprecated format it is not possible to explicitly know which network
// a NetworkConfig is intended for, so the Daemon can only allow a single
// network to be joined when using this format.
//
// DEPRECATED
func getDeprecatedNetworkConfig(
networkConfigs map[string]daecommon.NetworkConfig,
) *daecommon.NetworkConfig {
if len(networkConfigs) != 1 {
return nil
}
if c, ok := networkConfigs[daecommon.DeprecatedNetworkID]; ok {
return &c
}
return nil
}
func pickNetworkConfig(
networkConfigs map[string]daecommon.NetworkConfig,
creationParams bootstrap.CreationParams,
) *daecommon.NetworkConfig {
if c := getDeprecatedNetworkConfig(networkConfigs); c != nil {
return c
}
for searchStr, networkConfig := range networkConfigs {
if creationParams.Matches(searchStr) {
return &networkConfig
}
}
return nil
}
func validateConfig(
ctx context.Context,
networkLoader network.Loader,
daemonConfig daecommon.Config,
loadableNetworks []bootstrap.CreationParams,
) error {
givenConfigs := daemonConfig.Networks
daemonConfig.Networks = map[string]daecommon.NetworkConfig{}
for _, creationParams := range loadableNetworks {
id := creationParams.ID
if c := pickNetworkConfig(givenConfigs, creationParams); c != nil {
daemonConfig.Networks[id] = *c
continue
}
c, err := networkLoader.StoredConfig(ctx, id)
if err != nil {
return fmt.Errorf("getting stored config for %q: %w", id, err)
}
daemonConfig.Networks[id] = c
}
return daemonConfig.Validate()
}