Skip to content

Commit d08aca9

Browse files
authored
Merge pull request #621 from vdice/fix/k3s-restart-systemd
fix(containerd): update k3s systemd unit restart to support agent mode
2 parents 91c619e + 5370c2e commit d08aca9

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

internal/containerd/restart_unix.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package containerd
2121

2222
import (
23+
"bytes"
2324
"context"
2425
"fmt"
2526
"log/slog"
@@ -89,11 +90,23 @@ func (c K3sRestarter) Restart() error {
8990
// This restarter will be used both for stock K3s distros, which use systemd as well as K3d, which does not.
9091

9192
// If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
92-
if _, err := ListSystemdUnits(); err == nil {
93-
out, err := nsenterCmd("systemctl", "restart", "k3s").CombinedOutput()
93+
// First, collect systemd units to determine which k3s service to restart
94+
if units, err := ListSystemdUnits(); err == nil {
95+
var service string
96+
// Prioritize k3s-agent (more common); otherwise k3s
97+
switch {
98+
case bytes.Contains(units, []byte("k3s-agent.service")):
99+
service = "k3s-agent"
100+
case bytes.Contains(units, []byte("k3s.service")):
101+
service = "k3s"
102+
default:
103+
return fmt.Errorf("failed to find a registered k3s systemd service")
104+
}
105+
106+
out, err := nsenterCmd("systemctl", "restart", service).CombinedOutput()
94107
slog.Debug(string(out))
95108
if err != nil {
96-
return fmt.Errorf("unable to restart k3s: %w", err)
109+
return fmt.Errorf("unable to restart the %s systemd service: %w", service, err)
97110
}
98111
} else {
99112
// Assuming k3d; PID 1 is the main k3d entrypoint which should be restarted

0 commit comments

Comments
 (0)