From 22f073b32e276bc34c41fbf2ce519d4994b3ecdb Mon Sep 17 00:00:00 2001 From: Andrew Rynhard Date: Sat, 2 Nov 2019 23:18:12 +0000 Subject: [PATCH] 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 --- .../phase/services/stop_containerd.go | 37 --------------- .../services/stop_non_crucial_services.go | 47 ------------------- .../internal/phase/services/stop_services.go | 36 +++++++++++--- .../internal/sequencer/v1alpha1/types.go | 10 ++-- .../app/machined/pkg/system/services/osd.go | 2 + 5 files changed, 34 insertions(+), 98 deletions(-) delete mode 100644 internal/app/machined/internal/phase/services/stop_containerd.go delete mode 100644 internal/app/machined/internal/phase/services/stop_non_crucial_services.go diff --git a/internal/app/machined/internal/phase/services/stop_containerd.go b/internal/app/machined/internal/phase/services/stop_containerd.go deleted file mode 100644 index a4839874a..000000000 --- a/internal/app/machined/internal/phase/services/stop_containerd.go +++ /dev/null @@ -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 -} diff --git a/internal/app/machined/internal/phase/services/stop_non_crucial_services.go b/internal/app/machined/internal/phase/services/stop_non_crucial_services.go deleted file mode 100644 index 2856f8d7a..000000000 --- a/internal/app/machined/internal/phase/services/stop_non_crucial_services.go +++ /dev/null @@ -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 -} diff --git a/internal/app/machined/internal/phase/services/stop_services.go b/internal/app/machined/internal/phase/services/stop_services.go index 296bd1924..ca47f1c77 100644 --- a/internal/app/machined/internal/phase/services/stop_services.go +++ b/internal/app/machined/internal/phase/services/stop_services.go @@ -5,27 +5,49 @@ 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" + "github.com/talos-systems/talos/pkg/config/machine" ) // StopServices represents the StopServices task. -type StopServices struct{} +type StopServices struct { + upgrade bool +} // NewStopServicesTask initializes and returns an Services task. -func NewStopServicesTask() phase.Task { - return &StopServices{} +func NewStopServicesTask(upgrade bool) phase.Task { + return &StopServices{ + upgrade: upgrade, + } } // TaskFunc returns the runtime function. 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() + return nil } diff --git a/internal/app/machined/internal/sequencer/v1alpha1/types.go b/internal/app/machined/internal/sequencer/v1alpha1/types.go index 05997bf57..5a6733063 100644 --- a/internal/app/machined/internal/sequencer/v1alpha1/types.go +++ b/internal/app/machined/internal/sequencer/v1alpha1/types.go @@ -155,7 +155,7 @@ func (d *Sequencer) Shutdown() error { phaserunner.Add( phase.NewPhase( "stop services", - services.NewStopServicesTask(), + services.NewStopServicesTask(false), ), ) @@ -198,17 +198,13 @@ func (d *Sequencer) Upgrade(req *machineapi.UpgradeRequest) error { kubernetes.NewCordonAndDrainTask(), upgrade.NewLeaveEtcdTask(), ), - phase.NewPhase( - "stop services", - services.NewStopNonCrucialServicesTask(), - ), phase.NewPhase( "kill all tasks", kubernetes.NewKillKubernetesTasksTask(), ), phase.NewPhase( - "stop containerd", - services.NewStopContainerdTask(), + "stop services", + services.NewStopServicesTask(true), ), phase.NewPhase( "remove submounts", diff --git a/internal/app/machined/pkg/system/services/osd.go b/internal/app/machined/pkg/system/services/osd.go index 957a16d28..245e673f8 100644 --- a/internal/app/machined/pkg/system/services/osd.go +++ b/internal/app/machined/pkg/system/services/osd.go @@ -86,9 +86,11 @@ func (o *OSD) Runner(config runtime.Configurator) (runner.Runner, error) { // Set the mounts. 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/ssl", Source: "/etc/ssl", Options: []string{"bind", "ro"}}, {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: constants.ConfigPath, Source: constants.ConfigPath, Options: []string{"rbind", "ro"}}, {Type: "bind", Destination: constants.ContainerdAddress, Source: constants.ContainerdAddress, Options: []string{"bind", "ro"}},