Skip to content

Commit

Permalink
fetch current container runtime config through the command line
Browse files Browse the repository at this point in the history
Signed-off-by: Tariq Ibrahim <[email protected]>
  • Loading branch information
tariq1890 committed Sep 9, 2024
1 parent f1791c9 commit 3518b7c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ linters:
linters-settings:
goimports:
local-prefixes: github.com/NVIDIA/nvidia-container-toolkit
gosec:
excludes:
# TODO: Consider hardening security of command line invocations
- G204

issues:
exclude:
Expand Down
3 changes: 3 additions & 0 deletions cmd/nvidia-ctk/runtime/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/urfave/cli/v2"

"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
pkgconfig "github.com/NVIDIA/nvidia-container-toolkit/pkg/config"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/crio"
Expand Down Expand Up @@ -234,11 +235,13 @@ func (m command) configureConfigFile(c *cli.Context, config *config) error {
cfg, err = containerd.New(
containerd.WithLogger(m.logger),
containerd.WithPath(configFilePath),
containerd.WithReference(pkgconfig.FromCLI([]string{"containerd", "config", "dump"})),
)
case "crio":
cfg, err = crio.New(
crio.WithLogger(m.logger),
crio.WithPath(configFilePath),
crio.WithReference(pkgconfig.FromCLI([]string{"crio", "status", "config"})),
)
case "docker":
cfg, err = docker.New(
Expand Down
12 changes: 12 additions & 0 deletions pkg/config/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ func FromFile(path string) Loader {
return tomlFile(path)
}

// FromCLI creates a TOML source from a CLI output.
// If an empty slice is passed as input, an empty toml config is used.
func FromCLI(command []string) Loader {
if len(command) == 0 {
return Empty
}
return &tomlFromCommandOutput{
command: command[0],
args: command[1:],
}
}

// Load loads the contents of the specified TOML file as a map.
func (f tomlFile) Load() (*Toml, error) {
info, err := os.Stat(string(f))
Expand Down
14 changes: 13 additions & 1 deletion tools/container/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ func main() {
// Setup updates a containerd configuration to include the nvidia-containerd-runtime and reloads it
func Setup(c *cli.Context, o *options) error {
log.Infof("Starting 'setup' for %v", c.App.Name)
var cliArgs []string
if o.HostRootMount != "" {
cliArgs = append(cliArgs, "chroot", o.HostRootMount)
}
cliArgs = append(cliArgs, "containerd", "config", "dump")

cfg, err := containerd.New(
containerd.WithReference(config.FromFile(o.Config)),
containerd.WithReference(config.FromCLI(cliArgs)),
containerd.WithPath(o.Config),
containerd.WithRuntimeType(o.runtimeType),
containerd.WithUseLegacyConfig(o.useLegacyConfig),
Expand Down Expand Up @@ -223,8 +228,15 @@ func Setup(c *cli.Context, o *options) error {
func Cleanup(c *cli.Context, o *options) error {
log.Infof("Starting 'cleanup' for %v", c.App.Name)

var cliArgs []string
if o.HostRootMount != "" {
cliArgs = append(cliArgs, "chroot", o.HostRootMount)
}
cliArgs = append(cliArgs, "containerd", "config", "dump")

cfg, err := containerd.New(
containerd.WithPath(o.Config),
containerd.WithReference(config.FromCLI(cliArgs)),
containerd.WithRuntimeType(o.runtimeType),
containerd.WithUseLegacyConfig(o.useLegacyConfig),
containerd.WithContainerAnnotations(o.containerAnnotationsFromCDIPrefixes()...),
Expand Down
15 changes: 15 additions & 0 deletions tools/container/crio/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
pkgconfig "github.com/NVIDIA/nvidia-container-toolkit/pkg/config"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/crio"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/ocihook"
"github.com/NVIDIA/nvidia-container-toolkit/tools/container"
Expand Down Expand Up @@ -220,8 +221,15 @@ func setupHook(o *options) error {
func setupConfig(o *options) error {
log.Infof("Updating config file")

var cliArgs []string
if o.HostRootMount != "" {
cliArgs = append(cliArgs, "chroot", o.HostRootMount)
}
cliArgs = append(cliArgs, "crio", "status", "config")

cfg, err := crio.New(
crio.WithPath(o.Config),
crio.WithReference(pkgconfig.FromCLI(cliArgs)),
)
if err != nil {
return fmt.Errorf("unable to load config: %v", err)
Expand Down Expand Up @@ -271,8 +279,15 @@ func cleanupHook(o *options) error {
func cleanupConfig(o *options) error {
log.Infof("Reverting config file modifications")

var cliArgs []string
if o.HostRootMount != "" {
cliArgs = append(cliArgs, "chroot", o.HostRootMount)
}
cliArgs = append(cliArgs, "crio", "status", "config")

cfg, err := crio.New(
crio.WithPath(o.Config),
crio.WithReference(pkgconfig.FromCLI(cliArgs)),
)
if err != nil {
return fmt.Errorf("unable to load config: %v", err)
Expand Down

0 comments on commit 3518b7c

Please sign in to comment.