diff --git a/internal/config/config.go b/internal/config/config.go index 8e328487..9f4f16f3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,6 +38,7 @@ var ( // Note: This is currently duplicated by the HookConfig in cmd/nvidia-container-toolkit/hook_config.go type Config struct { NVIDIAContainerCLIConfig ContainerCLIConfig `toml:"nvidia-container-cli"` + NVIDIACTKConfig CTKConfig `toml:"nvidia-ctk"` NVIDIAContainerRuntimeConfig RuntimeConfig `toml:"nvidia-container-runtime"` } @@ -83,6 +84,7 @@ func getConfigFrom(toml *toml.Tree) *Config { } cfg.NVIDIAContainerCLIConfig = *getContainerCLIConfigFrom(toml) + cfg.NVIDIACTKConfig = *getCTKConfigFrom(toml) cfg.NVIDIAContainerRuntimeConfig = *getRuntimeConfigFrom(toml) return cfg @@ -92,6 +94,7 @@ func getConfigFrom(toml *toml.Tree) *Config { func getDefaultConfig() *Config { c := Config{ NVIDIAContainerCLIConfig: *getDefaultContainerCLIConfig(), + NVIDIACTKConfig: *getDefaultCTKConfig(), NVIDIAContainerRuntimeConfig: *getDefaultRuntimeConfig(), } diff --git a/internal/config/runtime_test.go b/internal/config/config_test.go similarity index 87% rename from internal/config/runtime_test.go rename to internal/config/config_test.go index fe78b529..cf50c4ed 100644 --- a/internal/config/runtime_test.go +++ b/internal/config/config_test.go @@ -65,43 +65,58 @@ func TestGetConfig(t *testing.T) { Experimental: false, DiscoverMode: "auto", }, + NVIDIACTKConfig: CTKConfig{ + Path: "nvidia-ctk", + }, }, }, { description: "config options set inline", contents: []string{ + "nvidia-container-cli.root = \"/bar/baz\"", "nvidia-container-runtime.debug = \"/foo/bar\"", "nvidia-container-runtime.experimental = true", "nvidia-container-runtime.discover-mode = \"not-legacy\"", + "nvidia-ctk.path = \"/foo/bar/nvidia-ctk\"", }, expectedConfig: &Config{ NVIDIAContainerCLIConfig: ContainerCLIConfig{ - Root: "", + Root: "/bar/baz", }, NVIDIAContainerRuntimeConfig: RuntimeConfig{ DebugFilePath: "/foo/bar", Experimental: true, DiscoverMode: "not-legacy", }, + NVIDIACTKConfig: CTKConfig{ + Path: "/foo/bar/nvidia-ctk", + }, }, }, { description: "config options set in section", contents: []string{ + "[nvidia-container-cli]", + "root = \"/bar/baz\"", "[nvidia-container-runtime]", "debug = \"/foo/bar\"", "experimental = true", "discover-mode = \"not-legacy\"", + "[nvidia-ctk]", + "path = \"/foo/bar/nvidia-ctk\"", }, expectedConfig: &Config{ NVIDIAContainerCLIConfig: ContainerCLIConfig{ - Root: "", + Root: "/bar/baz", }, NVIDIAContainerRuntimeConfig: RuntimeConfig{ DebugFilePath: "/foo/bar", Experimental: true, DiscoverMode: "not-legacy", }, + NVIDIACTKConfig: CTKConfig{ + Path: "/foo/bar/nvidia-ctk", + }, }, }, } diff --git a/internal/config/toolkit-cli.go b/internal/config/toolkit-cli.go new file mode 100644 index 00000000..1fe89717 --- /dev/null +++ b/internal/config/toolkit-cli.go @@ -0,0 +1,46 @@ +/** +# 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 config + +import "github.com/pelletier/go-toml" + +// CTKConfig stores the config options for the NVIDIA Container Toolkit CLI (nvidia-ctk) +type CTKConfig struct { + Path string `toml:"path"` +} + +// getCTKConfigFrom reads the nvidia container runtime config from the specified toml Tree. +func getCTKConfigFrom(toml *toml.Tree) *CTKConfig { + cfg := getDefaultCTKConfig() + + if toml == nil { + return cfg + } + + cfg.Path = toml.GetDefault("nvidia-ctk.path", cfg.Path).(string) + + return cfg +} + +// getDefaultCTKConfig defines the default values for the config +func getDefaultCTKConfig() *CTKConfig { + c := CTKConfig{ + Path: "nvidia-ctk", + } + + return &c +}