@@ -39,8 +39,8 @@ func NewDefaultRestarter() Restarter {
3939}
4040
4141func (c defaultRestarter ) Restart () error {
42- // If systemctl exists, use that, otherwise go pid
43- if UsesSystemd () {
42+ // If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
43+ if _ , err := listSystemdUnits (); err == nil {
4444 out , err := nsenterCmd ("systemctl" , "restart" , "containerd" ).CombinedOutput ()
4545 slog .Debug (string (out ))
4646 if err != nil {
@@ -67,7 +67,7 @@ type K0sRestarter struct{}
6767func (c K0sRestarter ) Restart () error {
6868 // First, collect systemd units to determine which mode k0s is running in, eg
6969 // k0sworker or k0scontroller
70- units , err := nsenterCmd ( "systemctl" , "list-units" ). CombinedOutput ()
70+ units , err := listSystemdUnits ()
7171 if err != nil {
7272 return fmt .Errorf ("unable to list systemd units: %w" , err )
7373 }
@@ -85,9 +85,10 @@ func (c K0sRestarter) Restart() error {
8585type K3sRestarter struct {}
8686
8787func (c K3sRestarter ) Restart () error {
88- // This restarter will be used both for stock K3s distros
89- // using systemd as well as K3d, which does not.
90- if UsesSystemd () {
88+ // This restarter will be used both for stock K3s distros, which use systemd as well as K3d, which does not.
89+
90+ // If listing systemd units succeeds, prefer systemctl restart; otherwise kill pid
91+ if _ , err := listSystemdUnits (); err == nil {
9192 out , err := nsenterCmd ("systemctl" , "restart" , "k3s" ).CombinedOutput ()
9293 slog .Debug (string (out ))
9394 if err != nil {
@@ -129,7 +130,7 @@ type RKE2Restarter struct{}
129130func (c RKE2Restarter ) Restart () error {
130131 // First, collect systemd units to determine which mode rke2 is running in, eg
131132 // rke2-agent or rke2-server
132- units , err := nsenterCmd ( "systemctl" , "list-units" ). CombinedOutput ()
133+ units , err := listSystemdUnits ()
133134 if err != nil {
134135 return fmt .Errorf ("unable to list systemd units: %w" , err )
135136 }
@@ -144,16 +145,8 @@ func (c RKE2Restarter) Restart() error {
144145 return nil
145146}
146147
147- // TODO: lifted and amended from https://github.com/spinframework/runtime-class-manager/pull/387
148- //
149- // UsesSystemd checks if the system is using systemd
150- func UsesSystemd () bool {
151- cmd := nsenterCmd ("systemctl" , "list-units" , "|" , "grep" , "-q" , "containerd.service" )
152- if err := cmd .Run (); err != nil {
153- slog .Debug ("Error with systemctl: \n " , "error" , err )
154- return false
155- }
156- return true
148+ func listSystemdUnits () ([]byte , error ) {
149+ return nsenterCmd ("systemctl" , "list-units" , "--type" , "service" ).CombinedOutput ()
157150}
158151
159152func nsenterCmd (cmd ... string ) * exec.Cmd {
0 commit comments