mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 20:13:33 +00:00
refactor corrosion service init for machine
This commit is contained in:
+30
-11
@@ -33,6 +33,29 @@ type Config struct {
|
|||||||
// DataDir is the directory where the machine stores its persistent state.
|
// DataDir is the directory where the machine stores its persistent state.
|
||||||
DataDir string
|
DataDir string
|
||||||
APISockPath string
|
APISockPath string
|
||||||
|
|
||||||
|
CorrosionDir string
|
||||||
|
CorrosionService corrosion.Service
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDefaults returns a new Config with default values set where not provided.
|
||||||
|
func (c *Config) SetDefaults() *Config {
|
||||||
|
// Copy c into a new Config to avoid modifying the original.
|
||||||
|
cfg := *c
|
||||||
|
|
||||||
|
if cfg.DataDir == "" {
|
||||||
|
cfg.DataDir = "/var/lib/uncloud"
|
||||||
|
}
|
||||||
|
if cfg.APISockPath == "" {
|
||||||
|
cfg.APISockPath = DefaultAPISockPath
|
||||||
|
}
|
||||||
|
if cfg.CorrosionDir == "" {
|
||||||
|
cfg.CorrosionDir = filepath.Join(cfg.DataDir, "corrosion")
|
||||||
|
}
|
||||||
|
if cfg.CorrosionService == nil {
|
||||||
|
cfg.CorrosionService = corrosion.DefaultSystemdService(cfg.CorrosionDir)
|
||||||
|
}
|
||||||
|
return &cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
type Machine struct {
|
type Machine struct {
|
||||||
@@ -51,6 +74,8 @@ type Machine struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewMachine(config *Config) (*Machine, error) {
|
func NewMachine(config *Config) (*Machine, error) {
|
||||||
|
config = config.SetDefaults()
|
||||||
|
|
||||||
// Load the existing machine state or create a new one.
|
// Load the existing machine state or create a new one.
|
||||||
statePath := StatePath(config.DataDir)
|
statePath := StatePath(config.DataDir)
|
||||||
state, err := ParseState(statePath)
|
state, err := ParseState(statePath)
|
||||||
@@ -135,17 +160,13 @@ func (m *Machine) Run(ctx context.Context) error {
|
|||||||
errGroup, ctx := errgroup.WithContext(ctx)
|
errGroup, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
// Start the machine local API server.
|
// Start the machine local API server.
|
||||||
apiSockPath := DefaultAPISockPath
|
localListener, err := listenUnixSocket(m.config.APISockPath)
|
||||||
if m.config.APISockPath != "" {
|
|
||||||
apiSockPath = m.config.APISockPath
|
|
||||||
}
|
|
||||||
localListener, err := listenUnixSocket(apiSockPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("listen API unix socket %q: %w", apiSockPath, err)
|
return fmt.Errorf("listen API unix socket %q: %w", m.config.APISockPath, err)
|
||||||
}
|
}
|
||||||
errGroup.Go(
|
errGroup.Go(
|
||||||
func() error {
|
func() error {
|
||||||
slog.Info("Starting local API server.", "path", apiSockPath)
|
slog.Info("Starting local API server.", "path", m.config.APISockPath)
|
||||||
if err := m.localServer.Serve(localListener); err != nil {
|
if err := m.localServer.Serve(localListener); err != nil {
|
||||||
return fmt.Errorf("local API server failed: %w", err)
|
return fmt.Errorf("local API server failed: %w", err)
|
||||||
}
|
}
|
||||||
@@ -177,13 +198,11 @@ func (m *Machine) Run(ctx context.Context) error {
|
|||||||
slog.Info("Starting network controller.")
|
slog.Info("Starting network controller.")
|
||||||
networkServer := newGRPCServer(m, m.cluster)
|
networkServer := newGRPCServer(m, m.cluster)
|
||||||
|
|
||||||
corroDir := filepath.Join(m.config.DataDir, "corrosion")
|
if err = m.configureCorrosion(m.config.CorrosionDir); err != nil {
|
||||||
if err = m.configureCorrosion(corroDir); err != nil {
|
|
||||||
return fmt.Errorf("configure corrosion service: %w", err)
|
return fmt.Errorf("configure corrosion service: %w", err)
|
||||||
}
|
}
|
||||||
corroService := corrosion.DefaultSystemdService(corroDir)
|
|
||||||
|
|
||||||
ctrl, err = newNetworkController(m.state, networkServer, corroService, m.newMachinesCh)
|
ctrl, err = newNetworkController(m.state, networkServer, m.config.CorrosionService, m.newMachinesCh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("initialise network controller: %w", err)
|
return fmt.Errorf("initialise network controller: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user