2022-01-14 15:29:20 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2021-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 docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-26 08:15:27 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2024-08-08 14:27:07 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config"
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
2022-01-14 15:29:20 +00:00
|
|
|
)
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
const (
|
|
|
|
defaultDockerRuntime = "runc"
|
|
|
|
)
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// Config defines a docker config file.
|
|
|
|
// TODO: This should not be public, but we need to access it from the tests in tools/container/docker
|
|
|
|
type Config map[string]interface{}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-12-01 23:59:34 +00:00
|
|
|
var _ engine.Interface = (*Config)(nil)
|
|
|
|
|
2024-08-08 22:40:00 +00:00
|
|
|
type dockerRuntime map[string]interface{}
|
|
|
|
|
|
|
|
var _ engine.RuntimeConfig = (*dockerRuntime)(nil)
|
|
|
|
|
2024-10-10 16:50:48 +00:00
|
|
|
// GetBinaryPath retrieves the path to the low-level runtime binary for a runtime.
|
|
|
|
// If no path is available, the empty string is returned.
|
2024-08-08 22:40:00 +00:00
|
|
|
func (d dockerRuntime) GetBinaryPath() string {
|
|
|
|
if d == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
path, _ := d["path"].(string)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// New creates a docker config with the specified options
|
|
|
|
func New(opts ...Option) (engine.Interface, error) {
|
|
|
|
b := &builder{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(b)
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 08:15:27 +00:00
|
|
|
if b.logger == nil {
|
|
|
|
b.logger = logger.New()
|
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
return b.build()
|
|
|
|
}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// AddRuntime adds a new runtime to the docker config
|
2024-09-27 11:19:23 +00:00
|
|
|
func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error {
|
2023-02-23 13:43:15 +00:00
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("config is nil")
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
config := *c
|
2022-01-14 15:29:20 +00:00
|
|
|
|
|
|
|
// Read the existing runtimes
|
|
|
|
runtimes := make(map[string]interface{})
|
|
|
|
if _, exists := config["runtimes"]; exists {
|
|
|
|
runtimes = config["runtimes"].(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add / update the runtime definitions
|
2023-02-23 13:43:15 +00:00
|
|
|
runtimes[name] = map[string]interface{}{
|
|
|
|
"path": path,
|
2022-07-14 14:19:19 +00:00
|
|
|
"args": []string{},
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
config["runtimes"] = runtimes
|
2022-07-14 14:19:19 +00:00
|
|
|
|
|
|
|
if setAsDefault {
|
2023-02-23 13:43:15 +00:00
|
|
|
config["default-runtime"] = name
|
2022-07-14 14:19:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
*c = config
|
2022-01-14 15:29:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// DefaultRuntime returns the default runtime for the docker config
|
|
|
|
func (c Config) DefaultRuntime() string {
|
|
|
|
r, ok := c["default-runtime"].(string)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// RemoveRuntime removes a runtime from the docker config
|
|
|
|
func (c *Config) RemoveRuntime(name string) error {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
config := *c
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
if _, exists := config["default-runtime"]; exists {
|
|
|
|
defaultRuntime := config["default-runtime"].(string)
|
|
|
|
if defaultRuntime == name {
|
|
|
|
config["default-runtime"] = defaultDockerRuntime
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, exists := config["runtimes"]; exists {
|
|
|
|
runtimes := config["runtimes"].(map[string]interface{})
|
|
|
|
|
|
|
|
delete(runtimes, name)
|
|
|
|
|
|
|
|
if len(runtimes) == 0 {
|
|
|
|
delete(config, "runtimes")
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2023-02-23 13:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*c = config
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:55:21 +00:00
|
|
|
// Set sets the specified docker option
|
2023-12-01 23:59:34 +00:00
|
|
|
func (c *Config) Set(key string, value interface{}) {
|
2023-11-20 22:55:21 +00:00
|
|
|
(*c)[key] = value
|
2023-10-31 14:24:27 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 13:43:15 +00:00
|
|
|
// Save writes the config to the specified path
|
|
|
|
func (c Config) Save(path string) (int64, error) {
|
|
|
|
output, err := json.MarshalIndent(c, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
|
|
|
|
}
|
2022-01-14 15:29:20 +00:00
|
|
|
|
2024-08-08 14:27:07 +00:00
|
|
|
n, err := config.Raw(path).Write(output)
|
2023-07-03 13:14:22 +00:00
|
|
|
return int64(n), err
|
2022-01-14 15:29:20 +00:00
|
|
|
}
|
2024-08-08 22:40:00 +00:00
|
|
|
|
|
|
|
// GetRuntimeConfig returns the runtime info of the runtime passed as input
|
|
|
|
func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {
|
|
|
|
if c == nil {
|
|
|
|
return nil, fmt.Errorf("config is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := *c
|
|
|
|
|
|
|
|
var runtimes map[string]interface{}
|
|
|
|
if _, ok := cfg["runtimes"]; ok {
|
|
|
|
runtimes = cfg["runtimes"].(map[string]interface{})
|
|
|
|
if r, ok := runtimes[name]; ok {
|
|
|
|
dr := dockerRuntime(r.(map[string]interface{}))
|
|
|
|
return &dr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &dockerRuntime{}, nil
|
|
|
|
}
|