Skip to content

Commit 7c44646

Browse files
committed
feat(restarter): add k8s distro-specific restarters
Signed-off-by: Vaughn Dice <[email protected]>
1 parent 6c567cb commit 7c44646

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

internal/containerd/restart_unix.go

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,105 @@ package containerd
2222
import (
2323
"fmt"
2424
"log/slog"
25+
"os"
26+
"os/exec"
27+
"regexp"
2528
"syscall"
2629

2730
"github.com/mitchellh/go-ps"
2831
)
2932

3033
var psProcesses = ps.Processes
3134

32-
type restarter struct{}
35+
type defaultRestarter struct{}
3336

34-
func NewRestarter() Restarter {
35-
return restarter{}
37+
func NewDefaultRestarter() Restarter {
38+
return defaultRestarter{}
3639
}
3740

38-
func (c restarter) Restart() error {
41+
func (c defaultRestarter) Restart() error {
3942
pid, err := getPid()
4043
if err != nil {
4144
return err
4245
}
4346
slog.Debug("found containerd process", "pid", pid)
4447

4548
err = syscall.Kill(pid, syscall.SIGHUP)
46-
4749
if err != nil {
4850
return fmt.Errorf("failed to send SIGHUP to containerd: %w", err)
4951
}
5052
return nil
5153
}
5254

55+
type K0sRestarter struct{}
56+
57+
func (c K0sRestarter) Restart() error {
58+
// First, collect systemd units to determine which mode k0s is running in, eg
59+
// k0sworker or k0scontroller
60+
units, err := nsenterCmd("systemctl", "list-units").CombinedOutput()
61+
if err != nil {
62+
return fmt.Errorf("unable to list systemd units: %w", err)
63+
}
64+
service := regexp.MustCompile("k0sworker|k0scontroller").FindString(string(units))
65+
66+
out, err := nsenterCmd("systemctl", "restart", service).CombinedOutput()
67+
slog.Debug(string(out))
68+
if err != nil {
69+
return fmt.Errorf("unable to restart %s: %w", service, err)
70+
}
71+
72+
return nil
73+
}
74+
75+
type K3sRestarter struct{}
76+
77+
func (c K3sRestarter) Restart() error {
78+
out, err := nsenterCmd("systemctl", "restart", "k3s").CombinedOutput()
79+
slog.Debug(string(out))
80+
if err != nil {
81+
return fmt.Errorf("unable to restart k3s: %w", err)
82+
}
83+
84+
return nil
85+
}
86+
87+
type MicroK8sRestarter struct{}
88+
89+
func (c MicroK8sRestarter) Restart() error {
90+
out, err := nsenterCmd("systemctl", "restart", "snap.microk8s.daemon-containerd").CombinedOutput()
91+
slog.Debug(string(out))
92+
if err != nil {
93+
return fmt.Errorf("unable to restart snap.microk8s.daemon-containerd: %w", err)
94+
}
95+
96+
return nil
97+
}
98+
99+
type RKE2Restarter struct{}
100+
101+
func (c RKE2Restarter) Restart() error {
102+
// First, collect systemd units to determine which mode rke2 is running in, eg
103+
// rke2-agent or rke2-server
104+
units, err := nsenterCmd("systemctl", "list-units").CombinedOutput()
105+
if err != nil {
106+
return fmt.Errorf("unable to list systemd units: %w", err)
107+
}
108+
service := regexp.MustCompile("rke2-agent|rke2-server").FindString(string(units))
109+
110+
out, err := nsenterCmd("systemctl", "restart", service).CombinedOutput()
111+
slog.Debug(string(out))
112+
if err != nil {
113+
return fmt.Errorf("unable to restart %s: %w", service, err)
114+
}
115+
116+
return nil
117+
}
118+
119+
func nsenterCmd(cmd ...string) *exec.Cmd {
120+
return exec.Command("nsenter",
121+
append([]string{fmt.Sprintf("-m/%s/proc/1/ns/mnt", os.Getenv("HOST_ROOT")), "--"}, cmd...)...) // #nosec G204
122+
}
123+
53124
func getPid() (int, error) {
54125
processes, err := psProcesses()
55126
if err != nil {

internal/preset/preset.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Env struct {
2424
var Default = Settings{
2525
ConfigPath: "/etc/containerd/config.toml",
2626
Setup: func(_ Env) error { return nil },
27-
Restarter: containerd.NewRestarter(),
27+
Restarter: containerd.NewDefaultRestarter(),
2828
}
2929

3030
func (s Settings) WithConfigPath(path string) Settings {
@@ -37,9 +37,16 @@ func (s Settings) WithSetup(setup func(env Env) error) Settings {
3737
return s
3838
}
3939

40-
var MicroK8s = Default.WithConfigPath("/var/snap/microk8s/current/args/containerd-template.toml")
40+
func (s Settings) WithRestarter(restarter containerd.Restarter) Settings {
41+
s.Restarter = restarter
42+
return s
43+
}
44+
45+
var MicroK8s = Default.WithConfigPath("/var/snap/microk8s/current/args/containerd-template.toml").
46+
WithRestarter(containerd.MicroK8sRestarter{})
4147

4248
var RKE2 = Default.WithConfigPath("/var/lib/rancher/rke2/agent/etc/containerd/config.toml.tmpl").
49+
WithRestarter(containerd.RKE2Restarter{}).
4350
WithSetup(func(env Env) error {
4451
_, err := env.HostFs.Stat(env.ConfigPath)
4552
if err == nil {
@@ -75,9 +82,11 @@ var RKE2 = Default.WithConfigPath("/var/lib/rancher/rke2/agent/etc/containerd/co
7582
return err
7683
})
7784

78-
var K3s = RKE2.WithConfigPath("/var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl")
85+
var K3s = RKE2.WithConfigPath("/var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl").
86+
WithRestarter(containerd.K3sRestarter{})
7987

8088
var K0s = Default.WithConfigPath("/etc/k0s/containerd.d/config.toml").
89+
WithRestarter(containerd.K0sRestarter{}).
8190
WithSetup(func(env Env) error {
8291
_, err := env.HostFs.Stat(env.ConfigPath)
8392
if err == nil {

0 commit comments

Comments
 (0)