Skip to content

Commit

Permalink
refactor: move early initialization functions to pre-initialize phase
Browse files Browse the repository at this point in the history
Fixes #8900

Closes #9687

(contains splitting of late mounts)

The benefits:

* tasks run _before_ controllers are started
* tasks can register `defer` to undo actions

This decomposes sequencer tasks a bit.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Nov 12, 2024
1 parent 9916e2c commit 5a0fd5b
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 389 deletions.
43 changes: 31 additions & 12 deletions internal/app/machined/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import (
"github.com/siderolabs/go-cmd/pkg/cmd/proc/reaper"
debug "github.com/siderolabs/go-debug"
"github.com/siderolabs/go-procfs/procfs"
"go.uber.org/zap"
"golang.org/x/sys/unix"

"github.com/siderolabs/talos/internal/app/apid"
"github.com/siderolabs/talos/internal/app/dashboard"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime/emergency"
v1alpha1runtime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime/v1alpha1"
startuptasks "github.com/siderolabs/talos/internal/app/machined/pkg/startup"
"github.com/siderolabs/talos/internal/app/machined/pkg/system"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/services"
"github.com/siderolabs/talos/internal/app/maintenance"
Expand Down Expand Up @@ -161,18 +163,10 @@ func runDebugServer(ctx context.Context) {
}
}

//nolint:gocyclo
func run() error {
errCh := make(chan error)

// Limit GOMAXPROCS.
startup.LimitMaxProcs(constants.MachinedMaxProcs)

// Set the PATH env var.
if err := os.Setenv("PATH", constants.PATH); err != nil {
return errors.New("error setting PATH")
}

// Initialize the controller without a config.
c, err := v1alpha1runtime.NewController()
if err != nil {
Expand All @@ -181,10 +175,35 @@ func run() error {

revertSetState(c.Runtime().State().V1Alpha2().Resources())

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

logger, err := c.V1Alpha2().MakeLogger("early-startup")
if err != nil {
return err
}

start := time.Now()

// Run startup tasks, and then run the entrypoint.
return startuptasks.RunTasks(ctx, logger, c.Runtime(), append(
startuptasks.DefaultTasks(),
func(ctx context.Context, log *zap.Logger, _ runtime.Runtime, _ startuptasks.NextTaskFunc) error {
logger.Info("early startup done", zap.Duration("duration", time.Since(start)))

return runEntrypoint(ctx, c)
},
)...)
}

//nolint:gocyclo
func runEntrypoint(ctx context.Context, c *v1alpha1runtime.Controller) error {
errCh := make(chan error)

var controllerWaitGroup sync.WaitGroup
defer controllerWaitGroup.Wait() // wait for controller-runtime to finish before rebooting

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

drainer := runtime.NewDrainer()
Expand Down Expand Up @@ -237,7 +256,7 @@ func run() error {
initializeCanceled := false

// Initialize the machine.
if err = c.Run(ctx, runtime.SequenceInitialize, nil); err != nil {
if err := c.Run(ctx, runtime.SequenceInitialize, nil); err != nil {
if errors.Is(err, context.Canceled) {
initializeCanceled = true
} else {
Expand All @@ -248,7 +267,7 @@ func run() error {
// If Initialize sequence was canceled, don't run any other sequence.
if !initializeCanceled {
// Perform an installation if required.
if err = c.Run(ctx, runtime.SequenceInstall, nil); err != nil {
if err := c.Run(ctx, runtime.SequenceInstall, nil); err != nil {
return err
}

Expand All @@ -258,7 +277,7 @@ func run() error {
)

// Boot the machine.
if err = c.Run(ctx, runtime.SequenceBoot, nil); err != nil && !errors.Is(err, context.Canceled) {
if err := c.Run(ctx, runtime.SequenceBoot, nil); err != nil && !errors.Is(err, context.Canceled) {
return err
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/app/machined/pkg/runtime/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"

"github.com/cosi-project/runtime/pkg/controller"
"go.uber.org/zap"
)

// TaskSetupFunc defines the function that a task will execute for a specific runtime
Expand Down Expand Up @@ -61,4 +62,5 @@ type Controller interface {
type V1Alpha2Controller interface {
Run(context.Context, *Drainer) error
DependencyGraph() (*controller.DependencyGraph, error)
MakeLogger(serviceName string) (*zap.Logger, error)
}
23 changes: 0 additions & 23 deletions internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,9 @@ func (*Sequencer) Initialize(r runtime.Runtime) []runtime.Phase {
mode := r.State().Platform().Mode()
phases := PhaseList{}

phases = phases.Append("logMode", LogMode)

switch mode { //nolint:exhaustive
case runtime.ModeContainer:
phases = phases.Append(
"systemRequirements",
SetupSystemDirectory,
InitVolumeLifecycle,
).Append(
"etc",
CreateSystemCgroups,
CreateOSReleaseFile,
SetUserEnvVars,
).Append(
"machined",
StartMachined,
StartContainerd,
Expand All @@ -93,18 +82,6 @@ func (*Sequencer) Initialize(r runtime.Runtime) []runtime.Phase {
phases = phases.Append(
"systemRequirements",
EnforceKSPPRequirements,
SetupSystemDirectory,
MountCgroups,
SetRLimit,
InitVolumeLifecycle,
).Append(
"integrity",
WriteIMAPolicy,
).Append(
"etc",
CreateSystemCgroups,
CreateOSReleaseFile,
SetUserEnvVars,
).Append(
"earlyServices",
StartUdevd,
Expand Down
Loading

0 comments on commit 5a0fd5b

Please sign in to comment.