Merge branch 'fix-hook' into 'main'

Handle empty root in config

See merge request nvidia/container-toolkit/container-toolkit!454
This commit is contained in:
Evan Lezar 2023-07-19 12:45:49 +00:00
commit e4fee325cb
5 changed files with 71 additions and 10 deletions

View File

@ -92,7 +92,7 @@ func doPrestart() {
rootfs := getRootfsPath(container) rootfs := getRootfsPath(container)
args := []string{getCLIPath(cli)} args := []string{getCLIPath(cli)}
if cli.Root != nil { if cli.Root != nil && *cli.Root != "" {
args = append(args, fmt.Sprintf("--root=%s", *cli.Root)) args = append(args, fmt.Sprintf("--root=%s", *cli.Root))
} }
if cli.LoadKmods { if cli.LoadKmods {

View File

@ -18,7 +18,8 @@ package config
// ContainerCLIConfig stores the options for the nvidia-container-cli // ContainerCLIConfig stores the options for the nvidia-container-cli
type ContainerCLIConfig struct { type ContainerCLIConfig struct {
Root string `toml:"root"` Root string `toml:"root"`
LoadKmods bool `toml:"load-kmods"` LoadKmods bool `toml:"load-kmods"`
Ldconfig string `toml:"ldconfig"` Ldconfig string `toml:"ldconfig"`
Environment []string `toml:"environment"`
} }

View File

@ -57,6 +57,7 @@ var (
// Config represents the contents of the config.toml file for the NVIDIA Container Toolkit // 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 // Note: This is currently duplicated by the HookConfig in cmd/nvidia-container-toolkit/hook_config.go
type Config struct { type Config struct {
DisableRequire bool `toml:"disable-require"`
AcceptEnvvarUnprivileged bool `toml:"accept-nvidia-visible-devices-envvar-when-unprivileged"` AcceptEnvvarUnprivileged bool `toml:"accept-nvidia-visible-devices-envvar-when-unprivileged"`
NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"` NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"`
@ -279,25 +280,26 @@ func (c Config) asCommentedToml() (*toml.Tree, error) {
} }
for k, v := range commentedDefaults { for k, v := range commentedDefaults {
set := asToml.Get(k) set := asToml.Get(k)
fmt.Printf("k=%v v=%+v set=%+v\n", k, v, set)
if !shouldComment(k, v, set) { if !shouldComment(k, v, set) {
continue continue
} }
fmt.Printf("set=%+v v=%+v\n", set, v)
asToml.SetWithComment(k, "", true, v) asToml.SetWithComment(k, "", true, v)
} }
return asToml, nil 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() { if key == "nvidia-container-cli.user" && !getCommentedUserGroup() {
return false return false
} }
if key == "nvidia-container-runtime.debug" && set == "/dev/null" { if key == "nvidia-container-runtime.debug" && setTo == "/dev/null" {
return true return true
} }
if set == nil || value == set { if setTo == nil || defaultValue == setTo {
return true return true
} }

View File

@ -17,6 +17,7 @@
package config package config
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "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)
}

View File

@ -102,9 +102,20 @@ function sync() {
ubuntu*) pkg_type=deb ubuntu*) pkg_type=deb
;; ;;
*) echo "ERROR: unexpected distribution ${src_dist}" *) echo "ERROR: unexpected distribution ${src_dist}"
exit 1
;; ;;
esac esac
if [[ $# -ge 4 && $4 == "package_type" ]] ; then
if [[ "${src_dist}" != "ubuntu18.04" && "${src_dist}" != "centos7" ]]; then
echo "Package type repos require ubuntu18.04 or centos7 as the source"
echo "skipping"
return
fi
dst_dist=$pkg_type
fi
local arch=${target##*-} local arch=${target##*-}
local dst_arch=${arch} local dst_arch=${arch}
case ${src_dist} in case ${src_dist} in
@ -174,11 +185,13 @@ git -C ${PACKAGE_REPO_ROOT} clean -fdx ${REPO}
for target in ${targets[@]}; do for target in ${targets[@]}; do
sync ${target} ${PACKAGE_CACHE}/packages ${PACKAGE_REPO_ROOT}/${REPO} sync ${target} ${PACKAGE_CACHE}/packages ${PACKAGE_REPO_ROOT}/${REPO}
# We also create a `package_type` repo; internally we skip this for non-ubuntu18.04 or centos7 distributions
sync ${target} ${PACKAGE_CACHE}/packages ${PACKAGE_REPO_ROOT}/${REPO} "package_type"
done done
git -C ${PACKAGE_REPO_ROOT} add ${REPO} git -C ${PACKAGE_REPO_ROOT} add ${REPO}
if [[ ${REPO} == "stable" ]]; then if [[ "${REPO}" == "stable" ]]; then
# Stable release # Stable release
git -C ${PACKAGE_REPO_ROOT} commit -s -F- <<EOF git -C ${PACKAGE_REPO_ROOT} commit -s -F- <<EOF
Add packages for NVIDIA Container Toolkit ${VERSION} release Add packages for NVIDIA Container Toolkit ${VERSION} release