@@ -22,34 +22,105 @@ package containerd
2222import  (
2323	"fmt" 
2424	"log/slog" 
25+ 	"os" 
26+ 	"os/exec" 
27+ 	"regexp" 
2528	"syscall" 
2629
2730	"github.com/mitchellh/go-ps" 
2831)
2932
3033var  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: %s" , 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: %s" , 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: %s" , 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: %s" , 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: %s" , 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: %s" , 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 ... )... )
122+ }
123+ 
53124func  getPid () (int , error ) {
54125	processes , err  :=  psProcesses ()
55126	if  err  !=  nil  {
0 commit comments