talos/internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_runtime.go
Andrey Smirnov 9a32e34cb1 feat: implement apply configuration without reboot
This allows config to be written to disk without being applied
immediately.

Small refactoring to extract common code paths.

At first, I tried to implement this via the sequencer, but looks like
it's too hard to get it right, as sequencer lacks context and config to
be written is not applied to the runtime.

Fixes #2828

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-11-23 12:42:44 -08:00

78 lines
1.8 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package v1alpha1
import (
"fmt"
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/config/configloader"
)
// Runtime implements the Runtime interface.
type Runtime struct {
c config.Provider
s runtime.State
e runtime.EventStream
l runtime.LoggingManager
}
// NewRuntime initializes and returns the v1alpha1 runtime.
func NewRuntime(c config.Provider, s runtime.State, e runtime.EventStream, l runtime.LoggingManager) *Runtime {
return &Runtime{
c: c,
s: s,
e: e,
l: l,
}
}
// Config implements the Runtime interface.
func (r *Runtime) Config() config.Provider {
return r.c
}
// ValidateConfig implements the Runtime interface.
func (r *Runtime) ValidateConfig(b []byte) (config.Provider, error) {
cfg, err := configloader.NewFromBytes(b)
if err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
if err := cfg.Validate(r.State().Platform().Mode()); err != nil {
return nil, fmt.Errorf("failed to validate config: %w", err)
}
return cfg, nil
}
// SetConfig implements the Runtime interface.
func (r *Runtime) SetConfig(b []byte) error {
cfg, err := r.ValidateConfig(b)
if err != nil {
return err
}
r.c = cfg
return nil
}
// State implements the Runtime interface.
func (r *Runtime) State() runtime.State {
return r.s
}
// Events implements the Runtime interface.
func (r *Runtime) Events() runtime.EventStream {
return r.e
}
// Logging implements the Runtime interface.
func (r *Runtime) Logging() runtime.LoggingManager {
return r.l
}