mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
Migrate docker config to engine.Interface
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
@@ -20,11 +20,12 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/docker"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/operator"
|
||||
log "github.com/sirupsen/logrus"
|
||||
cli "github.com/urfave/cli/v2"
|
||||
)
|
||||
@@ -170,7 +171,9 @@ func Setup(c *cli.Context, o *options) error {
|
||||
}
|
||||
o.runtimeDir = runtimeDir
|
||||
|
||||
cfg, err := LoadConfig(o.config)
|
||||
cfg, err := docker.New(
|
||||
docker.WithPath(o.config),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load config: %v", err)
|
||||
}
|
||||
@@ -180,7 +183,8 @@ func Setup(c *cli.Context, o *options) error {
|
||||
return fmt.Errorf("unable to update config: %v", err)
|
||||
}
|
||||
|
||||
err = FlushConfig(cfg, o.config)
|
||||
log.Infof("Flushing docker config to %v", o.config)
|
||||
_, err = cfg.Save(o.config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to flush config: %v", err)
|
||||
}
|
||||
@@ -204,20 +208,26 @@ func Cleanup(c *cli.Context, o *options) error {
|
||||
return fmt.Errorf("unable to parse args: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := LoadConfig(o.config)
|
||||
cfg, err := docker.New(
|
||||
docker.WithPath(o.config),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load config: %v", err)
|
||||
}
|
||||
|
||||
err = RevertConfig(cfg)
|
||||
err = RevertConfig(cfg, o)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to update config: %v", err)
|
||||
}
|
||||
|
||||
err = FlushConfig(cfg, o.config)
|
||||
log.Infof("Flushing docker config to %v", o.config)
|
||||
n, err := cfg.Save(o.config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to flush config: %v", err)
|
||||
}
|
||||
if n == 0 {
|
||||
log.Infof("Config file is empty, removed")
|
||||
}
|
||||
|
||||
err = RestartDocker(o)
|
||||
if err != nil {
|
||||
@@ -243,18 +253,17 @@ func ParseArgs(c *cli.Context) (string, error) {
|
||||
return runtimeDir, nil
|
||||
}
|
||||
|
||||
// LoadConfig loads the docker config from disk
|
||||
func LoadConfig(config string) (map[string]interface{}, error) {
|
||||
return docker.LoadConfig(config)
|
||||
}
|
||||
|
||||
// UpdateConfig updates the docker config to include the nvidia runtimes
|
||||
func UpdateConfig(config map[string]interface{}, o *options) error {
|
||||
for runtimeName, runtimePath := range o.getRuntimeBinaries() {
|
||||
setAsDefault := runtimeName == o.getDefaultRuntime()
|
||||
err := docker.UpdateConfig(config, runtimeName, runtimePath, setAsDefault)
|
||||
func UpdateConfig(cfg engine.Interface, o *options) error {
|
||||
runtimes := operator.GetRuntimes(
|
||||
operator.WithNvidiaRuntimeName(o.runtimeName),
|
||||
operator.WithSetAsDefault(o.setAsDefault),
|
||||
operator.WithRoot(o.runtimeDir),
|
||||
)
|
||||
for name, runtime := range runtimes {
|
||||
err := cfg.AddRuntime(name, runtime.Path, runtime.SetAsDefault)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update runtime %q: %v", runtimeName, err)
|
||||
return fmt.Errorf("failed to update runtime %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,33 +271,22 @@ func UpdateConfig(config map[string]interface{}, o *options) error {
|
||||
}
|
||||
|
||||
// RevertConfig reverts the docker config to remove the nvidia runtime
|
||||
func RevertConfig(config map[string]interface{}) error {
|
||||
if _, exists := config["default-runtime"]; exists {
|
||||
defaultRuntime := config["default-runtime"].(string)
|
||||
if _, exists := nvidiaRuntimeBinaries[defaultRuntime]; exists {
|
||||
config["default-runtime"] = defaultDockerRuntime
|
||||
func RevertConfig(cfg engine.Interface, o *options) error {
|
||||
runtimes := operator.GetRuntimes(
|
||||
operator.WithNvidiaRuntimeName(o.runtimeName),
|
||||
operator.WithSetAsDefault(o.setAsDefault),
|
||||
operator.WithRoot(o.runtimeDir),
|
||||
)
|
||||
for name := range runtimes {
|
||||
err := cfg.RemoveRuntime(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to remove runtime %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, exists := config["runtimes"]; exists {
|
||||
runtimes := config["runtimes"].(map[string]interface{})
|
||||
|
||||
for name := range nvidiaRuntimeBinaries {
|
||||
delete(runtimes, name)
|
||||
}
|
||||
|
||||
if len(runtimes) == 0 {
|
||||
delete(config, "runtimes")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FlushConfig flushes the updated/reverted config out to disk
|
||||
func FlushConfig(cfg map[string]interface{}, config string) error {
|
||||
return docker.FlushConfig(cfg, config)
|
||||
}
|
||||
|
||||
// RestartDocker restarts docker depending on the value of restartModeFlag
|
||||
func RestartDocker(o *options) error {
|
||||
switch o.restartMode {
|
||||
@@ -385,31 +383,3 @@ func SignalDocker(socket string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getDefaultRuntime 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) getDefaultRuntime() string {
|
||||
if o.setAsDefault == false {
|
||||
return ""
|
||||
}
|
||||
|
||||
return o.runtimeName
|
||||
}
|
||||
|
||||
// getRuntimeBinaries returns a map of runtime names to binary paths. This includes the
|
||||
// renaming of the `nvidia` runtime as per the --runtime-class command line flag.
|
||||
func (o options) getRuntimeBinaries() map[string]string {
|
||||
runtimeBinaries := make(map[string]string)
|
||||
|
||||
for rt, bin := range nvidiaRuntimeBinaries {
|
||||
runtime := rt
|
||||
if o.runtimeName != "" && o.runtimeName != nvidiaExperimentalRuntimeName && runtime == defaultRuntimeName {
|
||||
runtime = o.runtimeName
|
||||
}
|
||||
|
||||
runtimeBinaries[runtime] = filepath.Join(o.runtimeDir, bin)
|
||||
}
|
||||
|
||||
return runtimeBinaries
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/engine/docker"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -60,9 +61,9 @@ func TestUpdateConfigDefaultRuntime(t *testing.T) {
|
||||
runtimeDir: runtimeDir,
|
||||
}
|
||||
|
||||
config := map[string]interface{}{}
|
||||
config := docker.Config(map[string]interface{}{})
|
||||
|
||||
err := UpdateConfig(config, o)
|
||||
err := UpdateConfig(&config, o)
|
||||
require.NoError(t, err, "%d: %v", i, tc)
|
||||
|
||||
defaultRuntimeName := config["default-runtime"]
|
||||
@@ -74,7 +75,7 @@ func TestUpdateConfig(t *testing.T) {
|
||||
const runtimeDir = "/test/runtime/dir"
|
||||
|
||||
testCases := []struct {
|
||||
config map[string]interface{}
|
||||
config docker.Config
|
||||
setAsDefault bool
|
||||
runtimeName string
|
||||
expectedConfig map[string]interface{}
|
||||
@@ -254,7 +255,8 @@ func TestUpdateConfig(t *testing.T) {
|
||||
runtimeName: tc.runtimeName,
|
||||
runtimeDir: runtimeDir,
|
||||
}
|
||||
err := UpdateConfig(tc.config, options)
|
||||
|
||||
err := UpdateConfig(&tc.config, options)
|
||||
require.NoError(t, err, "%d: %v", i, tc)
|
||||
|
||||
configContent, err := json.MarshalIndent(tc.config, "", " ")
|
||||
@@ -269,7 +271,7 @@ func TestUpdateConfig(t *testing.T) {
|
||||
|
||||
func TestRevertConfig(t *testing.T) {
|
||||
testCases := []struct {
|
||||
config map[string]interface{}
|
||||
config docker.Config
|
||||
expectedConfig map[string]interface{}
|
||||
}{
|
||||
{
|
||||
@@ -368,7 +370,7 @@ func TestRevertConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
err := RevertConfig(tc.config)
|
||||
err := RevertConfig(&tc.config, &options{})
|
||||
|
||||
require.NoError(t, err, "%d: %v", i, tc)
|
||||
|
||||
@@ -381,43 +383,3 @@ func TestRevertConfig(t *testing.T) {
|
||||
require.EqualValues(t, string(expectedContent), string(configContent), "%d: %v", i, tc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagsDefaultRuntime(t *testing.T) {
|
||||
testCases := []struct {
|
||||
setAsDefault bool
|
||||
runtimeName string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
runtimeName: "not-bool",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
setAsDefault: false,
|
||||
runtimeName: "nvidia",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
setAsDefault: true,
|
||||
runtimeName: "nvidia",
|
||||
expected: "nvidia",
|
||||
},
|
||||
{
|
||||
setAsDefault: true,
|
||||
runtimeName: "nvidia-experimental",
|
||||
expected: "nvidia-experimental",
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
f := options{
|
||||
setAsDefault: tc.setAsDefault,
|
||||
runtimeName: tc.runtimeName,
|
||||
}
|
||||
|
||||
require.Equal(t, tc.expected, f.getDefaultRuntime(), "%d: %v", i, tc)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user