File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2020package containerd
2121
2222import (
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 // TODO: this approach still leads to the behavior mentioned in https://github.com/spinframework/runtime-class-manager/issues/140:
You can’t perform that action at this time.
0 commit comments