Skip to content

Commit 9cdbb1a

Browse files
committed
[nvidia-ctk-installer] introduce unconfigure mode to determine cleanup behavior
Signed-off-by: Christopher Desiniotis <[email protected]>
1 parent fae680c commit 9cdbb1a

File tree

13 files changed

+132
-8
lines changed

13 files changed

+132
-8
lines changed

cmd/nvidia-ctk-installer/container/container.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type Options struct {
5757
HostRootMount string
5858

5959
ConfigSources []string
60+
61+
UnconfigureMode string
6062
}
6163

6264
// Configure applies the options to the specified config
@@ -68,6 +70,29 @@ func (o Options) Configure(cfg engine.Interface) error {
6870
return o.flush(cfg)
6971
}
7072

73+
func (o Options) UnsetDefaultRuntime(cfg engine.Interface) error {
74+
runtimes := operator.GetRuntimes(
75+
operator.WithNvidiaRuntimeName(o.RuntimeName),
76+
operator.WithSetAsDefault(o.SetAsDefault),
77+
operator.WithRoot(o.RuntimeDir),
78+
)
79+
defaultRuntimeName := ""
80+
for name, runtime := range runtimes {
81+
if runtime.SetAsDefault {
82+
defaultRuntimeName = name
83+
}
84+
}
85+
if defaultRuntimeName == "" {
86+
return nil
87+
}
88+
89+
cfg.UnsetDefaultRuntime(defaultRuntimeName)
90+
if err := o.flush(cfg); err != nil {
91+
return err
92+
}
93+
return nil
94+
}
95+
7196
// Unconfigure removes the options from the specified config
7297
func (o Options) Unconfigure(cfg engine.Interface) error {
7398
err := o.RevertConfig(cfg)

cmd/nvidia-ctk-installer/container/runtime/containerd/containerd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
DefaultSocket = "/run/containerd/containerd.sock"
4040
DefaultRestartMode = "signal"
4141

42+
DefaultUnconfigureMode = "all"
43+
4244
defaultRuntimeType = "io.containerd.runc.v2"
4345
)
4446

@@ -115,11 +117,21 @@ func Setup(c *cli.Command, o *container.Options, co *Options) error {
115117
func Cleanup(c *cli.Command, o *container.Options, co *Options) error {
116118
log.Infof("Starting 'cleanup' for %v", c.Name)
117119

120+
if o.UnconfigureMode == "none" {
121+
log.Infof("Skipping cleanup as unconfigure mode is set to 'none'")
122+
return nil
123+
}
124+
118125
cfg, err := getRuntimeConfig(o, co)
119126
if err != nil {
120127
return fmt.Errorf("unable to load config: %v", err)
121128
}
122129

130+
if o.UnconfigureMode == "default-runtime-only" {
131+
log.Infof("Removing nvidia as the default runtime if configured")
132+
return o.UnsetDefaultRuntime(cfg)
133+
}
134+
123135
err = o.Unconfigure(cfg)
124136
if err != nil {
125137
return fmt.Errorf("unable to unconfigure containerd: %v", err)

cmd/nvidia-ctk-installer/container/runtime/crio/config_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func TestCrioConfigLifecycle(t *testing.T) {
8888
},
8989
assertCleanupPostConditions: func(t *testing.T, co *container.Options, _ *Options) error {
9090
require.NoFileExists(t, co.TopLevelConfigPath)
91-
require.NoFileExists(t, co.DropInConfig)
9291
return nil
9392
},
9493
},
@@ -182,8 +181,6 @@ signature_policy = "/etc/crio/policy.json"
182181
assertCleanupPostConditions: func(t *testing.T, co *container.Options, o *Options) error {
183182
require.FileExists(t, co.TopLevelConfigPath)
184183

185-
require.NoFileExists(t, co.DropInConfig)
186-
187184
actualTopLevel, err := os.ReadFile(co.TopLevelConfigPath)
188185
require.NoError(t, err)
189186

@@ -480,8 +477,6 @@ plugin_dirs = [
480477
`
481478
require.Equal(t, expected, string(actual))
482479

483-
require.NoFileExists(t, co.DropInConfig)
484-
485480
return nil
486481
},
487482
},

cmd/nvidia-ctk-installer/container/runtime/crio/crio.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const (
5151

5252
DefaultSocket = "/var/run/crio/crio.sock"
5353
DefaultRestartMode = "systemd"
54+
55+
DefaultUnconfigureMode = "default-runtime-only"
5456
)
5557

5658
// Options defines the cri-o specific options.
@@ -182,11 +184,21 @@ func cleanupHook(co *Options) error {
182184
func cleanupConfig(o *container.Options) error {
183185
log.Infof("Reverting config file modifications")
184186

187+
if o.UnconfigureMode == "none" {
188+
log.Infof("Skipping cleanup as unconfigure mode is set to 'none'")
189+
return nil
190+
}
191+
185192
cfg, err := getRuntimeConfig(o)
186193
if err != nil {
187194
return fmt.Errorf("unable to load config: %v", err)
188195
}
189196

197+
if o.UnconfigureMode == "default-runtime-only" {
198+
log.Infof("Removing nvidia as the default runtime if configured")
199+
return o.UnsetDefaultRuntime(cfg)
200+
}
201+
190202
err = o.Unconfigure(cfg)
191203
if err != nil {
192204
return fmt.Errorf("unable to unconfigure cri-o: %v", err)

cmd/nvidia-ctk-installer/container/runtime/docker/docker.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import (
3030
const (
3131
Name = "docker"
3232

33-
DefaultConfig = "/etc/docker/daemon.json"
34-
DefaultSocket = "/var/run/docker.sock"
35-
DefaultRestartMode = "signal"
33+
DefaultConfig = "/etc/docker/daemon.json"
34+
DefaultSocket = "/var/run/docker.sock"
35+
DefaultRestartMode = "signal"
36+
DefaultUnconfigureMode = "all"
3637
)
3738

3839
type Options struct{}

cmd/nvidia-ctk-installer/container/runtime/runtime.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ func Flags(opts *Options) []cli.Flag {
128128
Destination: &opts.ConfigSources,
129129
Sources: cli.EnvVars("RUNTIME_CONFIG_SOURCES", "RUNTIME_CONFIG_SOURCE"),
130130
},
131+
&cli.StringFlag{
132+
Name: "unconfigure-mode",
133+
Usage: "Specify how the runtime should be unconfigured; If 'none' is selected nothing will be reverted in the config [ all | default-runtime-only | none ]",
134+
Value: runtimeSpecificDefault,
135+
Destination: &opts.UnconfigureMode,
136+
Sources: cli.EnvVars("RUNTIME_UNCONFIGURE_MODE"),
137+
},
131138
}
132139

133140
flags = append(flags, containerd.Flags(&opts.containerdOptions)...)
@@ -178,6 +185,9 @@ func (opts *Options) Validate(logger logger.Interface, c *cli.Command, runtime s
178185
if opts.RestartMode == runtimeSpecificDefault {
179186
opts.RestartMode = containerd.DefaultRestartMode
180187
}
188+
if opts.UnconfigureMode == runtimeSpecificDefault {
189+
opts.UnconfigureMode = containerd.DefaultUnconfigureMode
190+
}
181191
case crio.Name:
182192
if opts.TopLevelConfigPath == runtimeSpecificDefault {
183193
opts.TopLevelConfigPath = crio.DefaultConfig
@@ -191,6 +201,9 @@ func (opts *Options) Validate(logger logger.Interface, c *cli.Command, runtime s
191201
if opts.RestartMode == runtimeSpecificDefault {
192202
opts.RestartMode = crio.DefaultRestartMode
193203
}
204+
if opts.UnconfigureMode == runtimeSpecificDefault {
205+
opts.UnconfigureMode = crio.DefaultUnconfigureMode
206+
}
194207
case docker.Name:
195208
if opts.TopLevelConfigPath == runtimeSpecificDefault {
196209
opts.TopLevelConfigPath = docker.DefaultConfig
@@ -205,6 +218,9 @@ func (opts *Options) Validate(logger logger.Interface, c *cli.Command, runtime s
205218
if opts.RestartMode == runtimeSpecificDefault {
206219
opts.RestartMode = docker.DefaultRestartMode
207220
}
221+
if opts.UnconfigureMode == runtimeSpecificDefault {
222+
opts.UnconfigureMode = docker.DefaultUnconfigureMode
223+
}
208224
default:
209225
return fmt.Errorf("undefined runtime %v", runtime)
210226
}

pkg/config/engine/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Interface interface {
2929
EnableCDI()
3030
GetRuntimeConfig(string) (RuntimeConfig, error)
3131
RemoveRuntime(string) error
32+
UnsetDefaultRuntime(string)
3233
Save(string) (int64, error)
3334
String() string
3435
}

pkg/config/engine/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type RuntimeConfigDestination interface {
4343
AddRuntimeWithOptions(string, string, bool, interface{}) error
4444
EnableCDI()
4545
RemoveRuntime(string) error
46+
UnsetDefaultRuntime(string)
4647
Save(string) (int64, error)
4748
String() string
4849
}
@@ -60,6 +61,11 @@ func (c *Config) RemoveRuntime(runtime string) error {
6061
return c.Destination.RemoveRuntime(runtime)
6162
}
6263

64+
// UnsetDefaultRuntime removes the default runtime setting from the destination config.
65+
func (c *Config) UnsetDefaultRuntime(runtime string) {
66+
c.Destination.UnsetDefaultRuntime(runtime)
67+
}
68+
6369
// EnableCDI enables CDI in the destination config.
6470
func (c *Config) EnableCDI() {
6571
c.Destination.EnableCDI()

pkg/config/engine/containerd/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ func (c *Config) EnableCDI() {
123123
*c.Tree = config
124124
}
125125

126+
func (c *Config) UnsetDefaultRuntime(name string) {
127+
if c == nil || c.Tree == nil {
128+
return
129+
}
130+
131+
config := *c.Tree
132+
if runtime, ok := config.GetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"}).(string); ok {
133+
if runtime == name {
134+
config.DeletePath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "default_runtime_name"})
135+
}
136+
}
137+
*c.Tree = config
138+
}
139+
126140
// RemoveRuntime removes a runtime from the docker config
127141
func (c *Config) RemoveRuntime(name string) error {
128142
if c == nil || c.Tree == nil {

pkg/config/engine/containerd/config_drop_in.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func (c *ConfigWithDropIn) Save(dropInPath string) (int64, error) {
8888
return bytesWritten, nil
8989
}
9090

91+
func (c *ConfigWithDropIn) UnsetDefaultRuntime(name string) {
92+
c.Interface.UnsetDefaultRuntime(name)
93+
}
94+
9195
// RemoveRuntime removes the runtime from both configs.
9296
func (c *ConfigWithDropIn) RemoveRuntime(name string) error {
9397
if err := c.topLevelConfig.RemoveRuntime(name); err != nil {

0 commit comments

Comments
 (0)