mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-22 08:18:32 +00:00
Merge branch 'CNT-2350/configure-containerd' into 'main'
Add support for containerd configs to nvidia-ctk runtime configure command See merge request nvidia/container-toolkit/container-toolkit!355
This commit is contained in:
commit
276e1960b1
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## v1.14.0-rc.1
|
## v1.14.0-rc.1
|
||||||
|
|
||||||
|
* Add support for updating containerd configs to the `nvidia-ctk runtime configure` command.
|
||||||
|
|
||||||
## v1.13.1
|
## v1.13.1
|
||||||
|
|
||||||
* Update `update-ldcache` hook to only update ldcache if it exists.
|
* Update `update-ldcache` hook to only update ldcache if it exists.
|
||||||
|
@ -17,14 +17,13 @@
|
|||||||
package configure
|
package configure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/runtime/nvidia"
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine"
|
||||||
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/containerd"
|
||||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/crio"
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/crio"
|
||||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/docker"
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/docker"
|
||||||
"github.com/pelletier/go-toml"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
@ -32,8 +31,15 @@ import (
|
|||||||
const (
|
const (
|
||||||
defaultRuntime = "docker"
|
defaultRuntime = "docker"
|
||||||
|
|
||||||
defaultDockerConfigFilePath = "/etc/docker/daemon.json"
|
// defaultNVIDIARuntimeName is the default name to use in configs for the NVIDIA Container Runtime
|
||||||
|
defaultNVIDIARuntimeName = "nvidia"
|
||||||
|
// defaultNVIDIARuntimeExecutable is the default NVIDIA Container Runtime executable file name
|
||||||
|
defaultNVIDIARuntimeExecutable = "nvidia-container-runtime"
|
||||||
|
defailtNVIDIARuntimeExpecutablePath = "/usr/bin/nvidia-container-runtime"
|
||||||
|
|
||||||
|
defaultContainerdConfigFilePath = "/etc/containerd/config.toml"
|
||||||
defaultCrioConfigFilePath = "/etc/crio/crio.conf"
|
defaultCrioConfigFilePath = "/etc/crio/crio.conf"
|
||||||
|
defaultDockerConfigFilePath = "/etc/docker/daemon.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type command struct {
|
type command struct {
|
||||||
@ -54,7 +60,12 @@ type config struct {
|
|||||||
dryRun bool
|
dryRun bool
|
||||||
runtime string
|
runtime string
|
||||||
configFilePath string
|
configFilePath string
|
||||||
nvidiaOptions nvidia.Options
|
|
||||||
|
nvidiaRuntime struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
setAsDefault bool
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m command) build() *cli.Command {
|
func (m command) build() *cli.Command {
|
||||||
@ -65,6 +76,9 @@ func (m command) build() *cli.Command {
|
|||||||
configure := cli.Command{
|
configure := cli.Command{
|
||||||
Name: "configure",
|
Name: "configure",
|
||||||
Usage: "Add a runtime to the specified container engine",
|
Usage: "Add a runtime to the specified container engine",
|
||||||
|
Before: func(c *cli.Context) error {
|
||||||
|
return validateFlags(c, &config)
|
||||||
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
return m.configureWrapper(c, &config)
|
return m.configureWrapper(c, &config)
|
||||||
},
|
},
|
||||||
@ -78,7 +92,7 @@ func (m command) build() *cli.Command {
|
|||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "runtime",
|
Name: "runtime",
|
||||||
Usage: "the target runtime engine. One of [crio, docker]",
|
Usage: "the target runtime engine; one of [containerd, crio, docker]",
|
||||||
Value: defaultRuntime,
|
Value: defaultRuntime,
|
||||||
Destination: &config.runtime,
|
Destination: &config.runtime,
|
||||||
},
|
},
|
||||||
@ -90,124 +104,119 @@ func (m command) build() *cli.Command {
|
|||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "nvidia-runtime-name",
|
Name: "nvidia-runtime-name",
|
||||||
Usage: "specify the name of the NVIDIA runtime that will be added",
|
Usage: "specify the name of the NVIDIA runtime that will be added",
|
||||||
Value: nvidia.RuntimeName,
|
Value: defaultNVIDIARuntimeName,
|
||||||
Destination: &config.nvidiaOptions.RuntimeName,
|
Destination: &config.nvidiaRuntime.name,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "runtime-path",
|
Name: "nvidia-runtime-path",
|
||||||
|
Aliases: []string{"runtime-path"},
|
||||||
Usage: "specify the path to the NVIDIA runtime executable",
|
Usage: "specify the path to the NVIDIA runtime executable",
|
||||||
Value: nvidia.RuntimeExecutable,
|
Value: defaultNVIDIARuntimeExecutable,
|
||||||
Destination: &config.nvidiaOptions.RuntimePath,
|
Destination: &config.nvidiaRuntime.path,
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "set-as-default",
|
Name: "nvidia-set-as-default",
|
||||||
Usage: "set the specified runtime as the default runtime",
|
Aliases: []string{"set-as-default"},
|
||||||
Destination: &config.nvidiaOptions.SetAsDefault,
|
Usage: "set the NVIDIA runtime as the default runtime",
|
||||||
|
Destination: &config.nvidiaRuntime.setAsDefault,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return &configure
|
return &configure
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m command) configureWrapper(c *cli.Context, config *config) error {
|
func validateFlags(c *cli.Context, config *config) error {
|
||||||
switch config.runtime {
|
switch config.runtime {
|
||||||
case "crio":
|
case "containerd", "crio", "docker":
|
||||||
return m.configureCrio(c, config)
|
break
|
||||||
case "docker":
|
default:
|
||||||
return m.configureDocker(c, config)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("unrecognized runtime '%v'", config.runtime)
|
return fmt.Errorf("unrecognized runtime '%v'", config.runtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// configureDocker updates the docker config to enable the NVIDIA Container Runtime
|
switch config.runtime {
|
||||||
func (m command) configureDocker(c *cli.Context, config *config) error {
|
case "containerd", "crio":
|
||||||
configFilePath := config.configFilePath
|
if config.nvidiaRuntime.path == defaultNVIDIARuntimeExecutable {
|
||||||
if configFilePath == "" {
|
config.nvidiaRuntime.path = defailtNVIDIARuntimeExpecutablePath
|
||||||
configFilePath = defaultDockerConfigFilePath
|
|
||||||
}
|
}
|
||||||
|
if !filepath.IsAbs(config.nvidiaRuntime.path) {
|
||||||
cfg, err := docker.New(
|
return fmt.Errorf("the NVIDIA runtime path %q is not an absolute path", config.nvidiaRuntime.path)
|
||||||
docker.WithPath(configFilePath),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to load config: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cfg.AddRuntime(
|
|
||||||
config.nvidiaOptions.RuntimeName,
|
|
||||||
config.nvidiaOptions.RuntimePath,
|
|
||||||
config.nvidiaOptions.SetAsDefault,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to update config: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.dryRun {
|
|
||||||
output, err := json.MarshalIndent(cfg, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to convert to JSON: %v", err)
|
|
||||||
}
|
|
||||||
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
n, err := cfg.Save(configFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to flush config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
m.logger.Infof("Removed empty config from %v", configFilePath)
|
|
||||||
} else {
|
|
||||||
m.logger.Infof("Wrote updated config to %v", configFilePath)
|
|
||||||
}
|
|
||||||
m.logger.Infof("It is recommended that the docker daemon be restarted.")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// configureCrio updates the crio config to enable the NVIDIA Container Runtime
|
// configureWrapper updates the specified container engine config to enable the NVIDIA runtime
|
||||||
func (m command) configureCrio(c *cli.Context, config *config) error {
|
func (m command) configureWrapper(c *cli.Context, config *config) error {
|
||||||
configFilePath := config.configFilePath
|
configFilePath := config.resolveConfigFilePath()
|
||||||
if configFilePath == "" {
|
|
||||||
configFilePath = defaultCrioConfigFilePath
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := crio.New(
|
var cfg engine.Interface
|
||||||
|
var err error
|
||||||
|
switch config.runtime {
|
||||||
|
case "containerd":
|
||||||
|
cfg, err = containerd.New(
|
||||||
|
containerd.WithPath(configFilePath),
|
||||||
|
)
|
||||||
|
case "crio":
|
||||||
|
cfg, err = crio.New(
|
||||||
crio.WithPath(configFilePath),
|
crio.WithPath(configFilePath),
|
||||||
)
|
)
|
||||||
if err != nil {
|
case "docker":
|
||||||
return fmt.Errorf("unable to load config: %v", err)
|
cfg, err = docker.New(
|
||||||
|
docker.WithPath(configFilePath),
|
||||||
|
)
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("unrecognized runtime '%v'", config.runtime)
|
||||||
|
}
|
||||||
|
if err != nil || cfg == nil {
|
||||||
|
return fmt.Errorf("unable to load config for runtime %v: %v", config.runtime, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cfg.AddRuntime(
|
err = cfg.AddRuntime(
|
||||||
config.nvidiaOptions.RuntimeName,
|
config.nvidiaRuntime.name,
|
||||||
config.nvidiaOptions.RuntimePath,
|
config.nvidiaRuntime.path,
|
||||||
config.nvidiaOptions.SetAsDefault,
|
config.nvidiaRuntime.setAsDefault,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to update config: %v", err)
|
return fmt.Errorf("unable to update config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.dryRun {
|
outputPath := config.getOuputConfigPath()
|
||||||
output, err := toml.Marshal(cfg)
|
n, err := cfg.Save(outputPath)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to convert to TOML: %v", err)
|
|
||||||
}
|
|
||||||
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
n, err := cfg.Save(configFilePath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to flush config: %v", err)
|
return fmt.Errorf("unable to flush config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
m.logger.Infof("Removed empty config from %v", configFilePath)
|
m.logger.Infof("Removed empty config from %v", outputPath)
|
||||||
} else {
|
} else {
|
||||||
m.logger.Infof("Wrote updated config to %v", configFilePath)
|
m.logger.Infof("Wrote updated config to %v", outputPath)
|
||||||
}
|
}
|
||||||
m.logger.Infof("It is recommended that the cri-o daemon be restarted.")
|
m.logger.Infof("It is recommended that %v daemon be restarted.", config.runtime)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveConfigFilePath returns the default config file path for the configured container engine
|
||||||
|
func (c *config) resolveConfigFilePath() string {
|
||||||
|
if c.configFilePath != "" {
|
||||||
|
return c.configFilePath
|
||||||
|
}
|
||||||
|
switch c.runtime {
|
||||||
|
case "containerd":
|
||||||
|
return defaultContainerdConfigFilePath
|
||||||
|
case "crio":
|
||||||
|
return defaultCrioConfigFilePath
|
||||||
|
case "docker":
|
||||||
|
return defaultDockerConfigFilePath
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// getOuputConfigPath returns the configured config path or "" if dry-run is enabled
|
||||||
|
func (c *config) getOuputConfigPath() string {
|
||||||
|
if c.dryRun {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return c.resolveConfigFilePath()
|
||||||
|
}
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
/*
|
|
||||||
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
# See the License for the specific language governing permissions and
|
|
||||||
# limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package nvidia
|
|
||||||
|
|
||||||
const (
|
|
||||||
// RuntimeName is the default name to use in configs for the NVIDIA Container Runtime
|
|
||||||
RuntimeName = "nvidia"
|
|
||||||
// RuntimeExecutable is the default NVIDIA Container Runtime executable file name
|
|
||||||
RuntimeExecutable = "nvidia-container-runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Options specifies the options for the NVIDIA Container Runtime w.r.t a container engine such as docker.
|
|
||||||
type Options struct {
|
|
||||||
SetAsDefault bool
|
|
||||||
RuntimeName string
|
|
||||||
RuntimePath string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Runtime defines an NVIDIA runtime with a name and a executable
|
|
||||||
type Runtime struct {
|
|
||||||
Name string
|
|
||||||
Path string
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultRuntime returns the default runtime for the configured options.
|
|
||||||
// If the configuration is invalid or the default runtimes should not be set
|
|
||||||
// the empty string is returned.
|
|
||||||
func (o Options) DefaultRuntime() string {
|
|
||||||
if !o.SetAsDefault {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return o.RuntimeName
|
|
||||||
}
|
|
||||||
|
|
||||||
// Runtime creates a runtime struct based on the options.
|
|
||||||
func (o Options) Runtime() Runtime {
|
|
||||||
path := o.RuntimePath
|
|
||||||
|
|
||||||
if o.RuntimePath == "" {
|
|
||||||
path = RuntimeExecutable
|
|
||||||
}
|
|
||||||
|
|
||||||
r := Runtime{
|
|
||||||
Name: o.RuntimeName,
|
|
||||||
Path: path,
|
|
||||||
}
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// DockerRuntimesConfig generatest the expected docker config for the specified runtime
|
|
||||||
func (r Runtime) DockerRuntimesConfig() map[string]interface{} {
|
|
||||||
runtimes := make(map[string]interface{})
|
|
||||||
runtimes[r.Name] = map[string]interface{}{
|
|
||||||
"path": r.Path,
|
|
||||||
"args": []string{},
|
|
||||||
}
|
|
||||||
|
|
||||||
return runtimes
|
|
||||||
}
|
|
@ -138,6 +138,11 @@ func (c Config) Save(path string) (int64, error) {
|
|||||||
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if path == "" {
|
||||||
|
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
||||||
|
return int64(len(output)), nil
|
||||||
|
}
|
||||||
|
|
||||||
if len(output) == 0 {
|
if len(output) == 0 {
|
||||||
err := os.Remove(path)
|
err := os.Remove(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -108,6 +108,11 @@ func (c Config) Save(path string) (int64, error) {
|
|||||||
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if path == "" {
|
||||||
|
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
||||||
|
return int64(len(output)), nil
|
||||||
|
}
|
||||||
|
|
||||||
if len(output) == 0 {
|
if len(output) == 0 {
|
||||||
err := os.Remove(path)
|
err := os.Remove(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -117,6 +117,11 @@ func (c Config) Save(path string) (int64, error) {
|
|||||||
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
|
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if path == "" {
|
||||||
|
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
||||||
|
return int64(len(output)), nil
|
||||||
|
}
|
||||||
|
|
||||||
if len(output) == 0 {
|
if len(output) == 0 {
|
||||||
err := os.Remove(path)
|
err := os.Remove(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user