From ec63533eb12a65947154f0cb50d7cdc4b68a1a2e Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Wed, 19 Jul 2023 14:37:49 +0200 Subject: [PATCH] Ensure default config comments are consistent Signed-off-by: Evan Lezar --- internal/config/cli.go | 7 +++--- internal/config/config.go | 12 +++++---- internal/config/config_test.go | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/internal/config/cli.go b/internal/config/cli.go index bd801a39..cb8d615c 100644 --- a/internal/config/cli.go +++ b/internal/config/cli.go @@ -18,7 +18,8 @@ package config // ContainerCLIConfig stores the options for the nvidia-container-cli type ContainerCLIConfig struct { - Root string `toml:"root"` - LoadKmods bool `toml:"load-kmods"` - Ldconfig string `toml:"ldconfig"` + Root string `toml:"root"` + LoadKmods bool `toml:"load-kmods"` + Ldconfig string `toml:"ldconfig"` + Environment []string `toml:"environment"` } diff --git a/internal/config/config.go b/internal/config/config.go index 576e16d7..c281acd9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -57,6 +57,7 @@ var ( // Config represents the contents of the config.toml file for the NVIDIA Container Toolkit // Note: This is currently duplicated by the HookConfig in cmd/nvidia-container-toolkit/hook_config.go type Config struct { + DisableRequire bool `toml:"disable-require"` AcceptEnvvarUnprivileged bool `toml:"accept-nvidia-visible-devices-envvar-when-unprivileged"` NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"` @@ -279,25 +280,26 @@ func (c Config) asCommentedToml() (*toml.Tree, error) { } for k, v := range commentedDefaults { set := asToml.Get(k) - fmt.Printf("k=%v v=%+v set=%+v\n", k, v, set) if !shouldComment(k, v, set) { continue } - fmt.Printf("set=%+v v=%+v\n", set, v) asToml.SetWithComment(k, "", true, v) } return asToml, nil } -func shouldComment(key string, value interface{}, set interface{}) bool { +func shouldComment(key string, defaultValue interface{}, setTo interface{}) bool { + if key == "nvidia-container-cli.root" && setTo == "" { + return true + } if key == "nvidia-container-cli.user" && !getCommentedUserGroup() { return false } - if key == "nvidia-container-runtime.debug" && set == "/dev/null" { + if key == "nvidia-container-runtime.debug" && setTo == "/dev/null" { return true } - if set == nil || value == set { + if setTo == nil || defaultValue == setTo { return true } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 029a0109..3f94bc26 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -17,6 +17,7 @@ package config import ( + "bytes" "io/ioutil" "os" "path/filepath" @@ -234,3 +235,47 @@ func TestGetConfig(t *testing.T) { }) } } + +func TestConfigDefault(t *testing.T) { + config, err := getDefault() + require.NoError(t, err) + + buffer := new(bytes.Buffer) + _, err = config.Save(buffer) + require.NoError(t, err) + + var lines []string + for _, l := range strings.Split(buffer.String(), "\n") { + l = strings.TrimSpace(l) + if strings.HasPrefix(l, "# ") { + l = "#" + strings.TrimPrefix(l, "# ") + } + lines = append(lines, l) + } + + // We take the lines from the config that was included in previous packages. + expectedLines := []string{ + "disable-require = false", + "#swarm-resource = \"DOCKER_RESOURCE_GPU\"", + "#accept-nvidia-visible-devices-envvar-when-unprivileged = true", + "#accept-nvidia-visible-devices-as-volume-mounts = false", + + "#root = \"/run/nvidia/driver\"", + "#path = \"/usr/bin/nvidia-container-cli\"", + "environment = []", + "#debug = \"/var/log/nvidia-container-toolkit.log\"", + "#ldcache = \"/etc/ld.so.cache\"", + "load-kmods = true", + "#no-cgroups = false", + "#user = \"root:video\"", + + "[nvidia-container-runtime]", + "#debug = \"/var/log/nvidia-container-runtime.log\"", + "log-level = \"info\"", + "mode = \"auto\"", + + "mount-spec-path = \"/etc/nvidia-container-runtime/host-files-for-container.d\"", + } + + require.Subset(t, lines, expectedLines) +}