refactor: unify service stop on upgrade

This simplifies service shutdown tasks. Shutdown and upgrade events now
use the same task.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
This commit is contained in:
Andrew Rynhard 2019-11-02 23:18:12 +00:00
parent f411491484
commit 22f073b32e
5 changed files with 34 additions and 98 deletions

View File

@ -1,37 +0,0 @@
// 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 services
import (
"context"
"github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/app/machined/pkg/system"
"github.com/talos-systems/talos/internal/pkg/runtime"
)
// StopContainerd represents the task for stop all services to perform
// an upgrade.
type StopContainerd struct{}
// NewStopContainerdTask initializes and returns an Services task.
func NewStopContainerdTask() phase.Task {
return &StopContainerd{}
}
// TaskFunc returns the runtime function.
func (task *StopContainerd) TaskFunc(mode runtime.Mode) phase.TaskFunc {
return func(r runtime.Runtime) error {
return task.standard()
}
}
func (task *StopContainerd) standard() (err error) {
if err = system.Services(nil).Stop(context.Background(), "containerd"); err != nil {
return err
}
return nil
}

View File

@ -1,47 +0,0 @@
// 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 services
import (
"context"
"github.com/containerd/containerd/namespaces"
"github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/app/machined/pkg/system"
"github.com/talos-systems/talos/internal/pkg/runtime"
"github.com/talos-systems/talos/pkg/config/machine"
)
// StopNonCrucialServices represents the task for stop all services to perform
// an upgrade.
type StopNonCrucialServices struct{}
// NewStopNonCrucialServicesTask initializes and returns an Services task.
func NewStopNonCrucialServicesTask() phase.Task {
return &StopNonCrucialServices{}
}
// TaskFunc returns the runtime function.
func (task *StopNonCrucialServices) TaskFunc(mode runtime.Mode) phase.TaskFunc {
return task.standard
}
func (task *StopNonCrucialServices) standard(r runtime.Runtime) (err error) {
ctx := namespaces.WithNamespace(context.Background(), "k8s.io")
services := []string{"osd", "udevd", "networkd", "ntpd", "apid"}
if r.Config().Machine().Type() == machine.Bootstrap || r.Config().Machine().Type() == machine.ControlPlane {
services = append(services, "trustd", "proxyd")
}
for _, service := range services {
if err = system.Services(nil).Stop(ctx, service); err != nil {
return err
}
}
return nil
}

View File

@ -5,27 +5,49 @@
package services package services
import ( import (
"context"
"github.com/talos-systems/talos/internal/app/machined/internal/phase" "github.com/talos-systems/talos/internal/app/machined/internal/phase"
"github.com/talos-systems/talos/internal/app/machined/pkg/system" "github.com/talos-systems/talos/internal/app/machined/pkg/system"
"github.com/talos-systems/talos/internal/pkg/runtime" "github.com/talos-systems/talos/internal/pkg/runtime"
"github.com/talos-systems/talos/pkg/config/machine"
) )
// StopServices represents the StopServices task. // StopServices represents the StopServices task.
type StopServices struct{} type StopServices struct {
upgrade bool
}
// NewStopServicesTask initializes and returns an Services task. // NewStopServicesTask initializes and returns an Services task.
func NewStopServicesTask() phase.Task { func NewStopServicesTask(upgrade bool) phase.Task {
return &StopServices{} return &StopServices{
upgrade: upgrade,
}
} }
// TaskFunc returns the runtime function. // TaskFunc returns the runtime function.
func (task *StopServices) TaskFunc(mode runtime.Mode) phase.TaskFunc { func (task *StopServices) TaskFunc(mode runtime.Mode) phase.TaskFunc {
return func(r runtime.Runtime) error { return task.standard
return task.standard()
}
} }
func (task *StopServices) standard() (err error) { func (task *StopServices) standard(r runtime.Runtime) (err error) {
if task.upgrade {
services := []string{"containerd", "networkd", "ntpd", "udevd"}
if r.Config().Machine().Type() == machine.Bootstrap || r.Config().Machine().Type() == machine.ControlPlane {
services = append(services, "etcd", "trustd")
}
for _, service := range services {
if err = system.Services(nil).Stop(context.Background(), service); err != nil {
return err
}
}
return nil
}
system.Services(nil).Shutdown() system.Services(nil).Shutdown()
return nil return nil
} }

View File

@ -155,7 +155,7 @@ func (d *Sequencer) Shutdown() error {
phaserunner.Add( phaserunner.Add(
phase.NewPhase( phase.NewPhase(
"stop services", "stop services",
services.NewStopServicesTask(), services.NewStopServicesTask(false),
), ),
) )
@ -198,17 +198,13 @@ func (d *Sequencer) Upgrade(req *machineapi.UpgradeRequest) error {
kubernetes.NewCordonAndDrainTask(), kubernetes.NewCordonAndDrainTask(),
upgrade.NewLeaveEtcdTask(), upgrade.NewLeaveEtcdTask(),
), ),
phase.NewPhase(
"stop services",
services.NewStopNonCrucialServicesTask(),
),
phase.NewPhase( phase.NewPhase(
"kill all tasks", "kill all tasks",
kubernetes.NewKillKubernetesTasksTask(), kubernetes.NewKillKubernetesTasksTask(),
), ),
phase.NewPhase( phase.NewPhase(
"stop containerd", "stop services",
services.NewStopContainerdTask(), services.NewStopServicesTask(true),
), ),
phase.NewPhase( phase.NewPhase(
"remove submounts", "remove submounts",

View File

@ -86,9 +86,11 @@ func (o *OSD) Runner(config runtime.Configurator) (runner.Runner, error) {
// Set the mounts. // Set the mounts.
mounts := []specs.Mount{ mounts := []specs.Mount{
// TODO(andrewrynhard): Remove this once the kubeconfig API is owned by machined.
{Type: "bind", Destination: "/etc/kubernetes", Source: "/etc/kubernetes", Options: []string{"rbind", "ro"}}, {Type: "bind", Destination: "/etc/kubernetes", Source: "/etc/kubernetes", Options: []string{"rbind", "ro"}},
{Type: "bind", Destination: "/etc/ssl", Source: "/etc/ssl", Options: []string{"bind", "ro"}}, {Type: "bind", Destination: "/etc/ssl", Source: "/etc/ssl", Options: []string{"bind", "ro"}},
{Type: "bind", Destination: "/tmp", Source: "/tmp", Options: []string{"rbind", "rshared", "rw"}}, {Type: "bind", Destination: "/tmp", Source: "/tmp", Options: []string{"rbind", "rshared", "rw"}},
// TODO(andrewrynhard): Remove this once the logs API is owned by machined.
{Type: "bind", Destination: "/var/log/pods", Source: "/var/log/pods", Options: []string{"bind", "ro"}}, {Type: "bind", Destination: "/var/log/pods", Source: "/var/log/pods", Options: []string{"bind", "ro"}},
{Type: "bind", Destination: constants.ConfigPath, Source: constants.ConfigPath, Options: []string{"rbind", "ro"}}, {Type: "bind", Destination: constants.ConfigPath, Source: constants.ConfigPath, Options: []string{"rbind", "ro"}},
{Type: "bind", Destination: constants.ContainerdAddress, Source: constants.ContainerdAddress, Options: []string{"bind", "ro"}}, {Type: "bind", Destination: constants.ContainerdAddress, Source: constants.ContainerdAddress, Options: []string{"bind", "ro"}},