@@ -25,7 +25,6 @@ import (
2525 "path/filepath"
2626 "strconv"
2727 "syscall"
28- "time"
2928
3029 "github.com/pelletier/go-toml"
3130 "github.com/urfave/cli/v2"
@@ -42,12 +41,14 @@ import (
4241 "github.com/NVIDIA/k8s-kata-manager/internal/oras"
4342 "github.com/NVIDIA/k8s-kata-manager/internal/runtime"
4443 containerd "github.com/NVIDIA/k8s-kata-manager/internal/runtime/containerd"
44+ "github.com/NVIDIA/k8s-kata-manager/internal/runtime/crio"
4545 "github.com/NVIDIA/k8s-kata-manager/internal/version"
4646)
4747
4848const (
4949 defaultContainerdConfigFilePath = "/etc/containerd/config.toml"
5050 defaultContainerdSocketFilePath = "/run/containerd/containerd.sock"
51+ defaultCrioConfigFilePath = "/etc/crio/crio.conf"
5152
5253 cdiRoot = "/var/run/cdi"
5354)
@@ -74,6 +75,8 @@ type worker struct {
7475 ContainerdSocket string
7576 LoadKernelModules bool
7677 CDIEnabled bool
78+ Runtime string
79+ CrioConfig string
7780}
7881
7982// newWorker returns a new worker struct
@@ -146,6 +149,20 @@ func main() {
146149 Destination : & worker .CDIEnabled ,
147150 EnvVars : []string {"CDI_ENABLED" },
148151 },
152+ & cli.StringFlag {
153+ Name : "runtime" ,
154+ Usage : "Runtime name" ,
155+ Value : "" ,
156+ Destination : & worker .Runtime ,
157+ EnvVars : []string {"RUNTIME" },
158+ },
159+ & cli.StringFlag {
160+ Name : "crio-config" ,
161+ Usage : "Path to the CRI-O config file" ,
162+ Value : defaultCrioConfigFilePath ,
163+ Destination : & worker .CrioConfig ,
164+ EnvVars : []string {"CRIO_CONFIG" },
165+ },
149166 }
150167
151168 c .Before = func (c * cli.Context ) error {
@@ -245,11 +262,9 @@ func (w *worker) Run(c *cli.Context) error {
245262 return fmt .Errorf ("failed to generate CDI spec: %w" , err )
246263 }
247264 }
248-
249- options := runtime.Options {Path : w .ContainerdConfig , RuntimeType : "io.containerd.kata.v2" , PodAnnotations : []string {"io.katacontainers.*" }}
250- ctrdConfig , err := containerd .Setup (& options )
265+ runtimeConfig , err := w .getRuntimeConfig ()
251266 if err != nil {
252- klog .Errorf ("error creating containerd. config client : %s" , err )
267+ klog .Errorf ("error creating runtime config client : %s" , err )
253268 return err
254269 }
255270
@@ -292,7 +307,7 @@ func (w *worker) Run(c *cli.Context) error {
292307 return fmt .Errorf ("error transforming kata configuration file: %w" , err )
293308 }
294309
295- err = ctrdConfig .AddRuntime (
310+ err = runtimeConfig .AddRuntime (
296311 rc .Name ,
297312 kataConfigPath ,
298313 false ,
@@ -302,23 +317,21 @@ func (w *worker) Run(c *cli.Context) error {
302317 }
303318
304319 }
305-
306- n , err := ctrdConfig .Save (w .ContainerdConfig )
320+ n , err := runtimeConfig .Save ()
307321 if err != nil {
308322 return fmt .Errorf ("unable to flush config: %w" , err )
309323 }
310-
311324 if n == 0 {
312- klog .Infof ("Removed empty config from %v" , w . ContainerdConfig )
325+ klog .Infof ("Removed empty config" )
313326 } else {
314- klog .Infof ("Wrote updated config to %v" , w . ContainerdConfig )
327+ klog .Infof ("Wrote updated config" )
315328 }
316329
317- klog .Infof ("Restarting containerd " )
318- if err := restartContainerd ( w . ContainerdSocket ); err != nil {
319- return fmt .Errorf ("unable to restart containerd : %w" , err )
330+ klog .Infof ("Restarting runtime " )
331+ if err := runtimeConfig . Restart ( ); err != nil {
332+ return fmt .Errorf ("unable to restart runtime service : %w" , err )
320333 }
321- klog .Info ("containerd successfully restarted" )
334+ klog .Info ("runtime successfully restarted" )
322335
323336 if err := waitForSignal (); err != nil {
324337 return fmt .Errorf ("unable to wait for signal: %w" , err )
@@ -331,22 +344,37 @@ func (w *worker) Run(c *cli.Context) error {
331344 return nil
332345}
333346
334- // CleanUp reverts the containerd config to remove the nvidia-container-runtime
347+ func (w * worker ) getRuntimeConfig () (runtime.Runtime , error ) {
348+ var runtimeConfig runtime.Runtime
349+ var err error
350+ if w .Runtime == api .CRIO .String () {
351+ options := runtime.Options {Path : w .CrioConfig , RuntimeType : "vm" , PodAnnotations : []string {"io.katacontainers.*" }}
352+ runtimeConfig , err = crio .Setup (& options )
353+ } else if w .Runtime == api .Containerd .String () {
354+ options := runtime.Options {Path : w .ContainerdConfig , RuntimeType : "io.containerd.kata.v2" , PodAnnotations : []string {"io.katacontainers.*" }, Socket : w .ContainerdSocket }
355+ runtimeConfig , err = containerd .Setup (& options )
356+ }
357+ if err != nil {
358+ klog .Errorf ("error creating runtime config client : %s" , err )
359+ return nil , err
360+ }
361+ return runtimeConfig , nil
362+ }
363+
364+ // CleanUp reverts the runtime config added by kata manager
335365func (w * worker ) CleanUp () error {
336- ctrdConfig , err := containerd .New (
337- containerd .WithPath (w .ContainerdConfig ),
338- )
366+ runtimeConfig , err := w .getRuntimeConfig ()
339367 if err != nil {
340- klog .Errorf ("error creating containerd. config client : %s" , err )
368+ klog .Errorf ("error creating runtime config client : %s" , err )
341369 return err
342370 }
343371 for _ , rc := range w .Config .RuntimeClasses {
344- err := ctrdConfig .RemoveRuntime (rc .Name )
372+ err := runtimeConfig .RemoveRuntime (rc .Name )
345373 if err != nil {
346374 return fmt .Errorf ("unable to revert config for runtime class '%v': %w" , rc , err )
347375 }
348376 }
349- n , err := ctrdConfig .Save (w . ContainerdConfig )
377+ n , err := runtimeConfig .Save ()
350378 if err != nil {
351379 return fmt .Errorf ("unable to flush config: %w" , err )
352380 }
@@ -356,8 +384,8 @@ func (w *worker) CleanUp() error {
356384 } else {
357385 klog .Infof ("Wrote updated config to %v" , w .ContainerdConfig )
358386 }
359- if err := restartContainerd ( w . ContainerdSocket ); err != nil {
360- return fmt .Errorf ("unable to restart containerd : %w" , err )
387+ if err := runtimeConfig . Restart ( ); err != nil {
388+ return fmt .Errorf ("unable to restart runtime service : %w" , err )
361389 }
362390 return nil
363391}
@@ -385,48 +413,6 @@ func initialize() error {
385413 return nil
386414}
387415
388- func restartContainerd (containerdSocket string ) error {
389-
390- // Create a channel to receive signals
391- sigs := make (chan os.Signal , 1 )
392- signal .Notify (sigs , syscall .SIGTERM , syscall .SIGHUP )
393-
394- // Set up a timer to ignore the signal for 5 seconds
395- ignoreTimer := time .NewTimer (5 * time .Second )
396-
397- // Create a channel to signal when the function has finished executing
398- done := make (chan error )
399-
400- // Start the function in a goroutine
401- go func () {
402- // Execute your function here
403- err := containerd .RestartContainerd (containerdSocket )
404- if err != nil {
405- klog .Errorf ("error restarting containerd: %v" , err )
406- done <- err
407- }
408- // Since we are restarintg Containerd we need to
409- // Ignore the SIGTERM signal for 5 seconds
410- <- ignoreTimer .C
411- // Signal that the function has finished executing
412- done <- nil
413- }()
414-
415- // Wait for the function to finish executing or for the signal to be received
416- select {
417- case err := <- done :
418- if err != nil {
419- return err
420- }
421- case s := <- sigs :
422- fmt .Printf ("Received signal %v" , s )
423- // Reset the timer to ignore the signal for another 5 seconds
424- ignoreTimer .Reset (5 * time .Second )
425- }
426-
427- return nil
428- }
429-
430416func transformKataConfig (path string ) error {
431417 config , err := toml .LoadFile (path )
432418 if err != nil {
0 commit comments