2023-02-23 20:01:24 +00:00
|
|
|
/**
|
|
|
|
# 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 containerd
|
|
|
|
|
|
|
|
import (
|
2024-08-08 14:27:07 +00:00
|
|
|
"fmt"
|
2023-12-01 01:10:10 +00:00
|
|
|
|
2023-05-26 08:15:27 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
2024-08-08 14:27:07 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml"
|
2023-02-23 20:01:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the containerd config
|
|
|
|
type Config struct {
|
|
|
|
*toml.Tree
|
2024-07-02 08:26:41 +00:00
|
|
|
Logger logger.Interface
|
2023-02-23 20:01:24 +00:00
|
|
|
RuntimeType string
|
|
|
|
UseDefaultRuntimeName bool
|
2023-03-23 19:12:23 +00:00
|
|
|
ContainerAnnotations []string
|
2023-02-23 20:01:24 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:59:34 +00:00
|
|
|
var _ engine.Interface = (*Config)(nil)
|
|
|
|
|
2024-08-08 22:40:00 +00:00
|
|
|
type containerdCfgRuntime struct {
|
|
|
|
tree *toml.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ engine.RuntimeConfig = (*containerdCfgRuntime)(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 (c *containerdCfgRuntime) GetBinaryPath() string {
|
|
|
|
if c == nil || c.tree == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
binPath, _ := c.tree.GetPath([]string{"options", "BinaryName"}).(string)
|
|
|
|
return binPath
|
|
|
|
}
|
|
|
|
|
2023-02-23 20:01:24 +00:00
|
|
|
// New creates a containerd config with the specified options
|
|
|
|
func New(opts ...Option) (engine.Interface, error) {
|
2024-08-08 14:27:07 +00:00
|
|
|
b := &builder{
|
|
|
|
runtimeType: defaultRuntimeType,
|
|
|
|
}
|
2023-02-23 20:01:24 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(b)
|
|
|
|
}
|
2023-05-26 08:15:27 +00:00
|
|
|
if b.logger == nil {
|
|
|
|
b.logger = logger.New()
|
|
|
|
}
|
2024-08-08 14:27:07 +00:00
|
|
|
if b.configSource == nil {
|
|
|
|
b.configSource = toml.FromFile(b.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
tomlConfig, err := b.configSource.Load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-11-18 05:55:58 +00:00
|
|
|
useLegacyConfig := b.configVersion == 1
|
|
|
|
|
2024-08-08 14:27:07 +00:00
|
|
|
cfg := &Config{
|
|
|
|
Tree: tomlConfig,
|
|
|
|
Logger: b.logger,
|
|
|
|
RuntimeType: b.runtimeType,
|
2024-11-18 05:55:58 +00:00
|
|
|
UseDefaultRuntimeName: useLegacyConfig,
|
2024-08-08 14:27:07 +00:00
|
|
|
ContainerAnnotations: b.containerAnnotations,
|
|
|
|
}
|
2023-05-26 08:15:27 +00:00
|
|
|
|
2024-11-18 05:55:58 +00:00
|
|
|
version, err := cfg.parseVersion(useLegacyConfig)
|
2024-08-08 14:27:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse config version: %v", err)
|
|
|
|
}
|
|
|
|
switch version {
|
|
|
|
case 1:
|
|
|
|
return (*ConfigV1)(cfg), nil
|
|
|
|
case 2:
|
|
|
|
return cfg, nil
|
2024-11-18 05:44:39 +00:00
|
|
|
case 3:
|
|
|
|
return (*ConfigV3)(cfg), nil
|
2024-08-08 14:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unsupported config version: %v", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseVersion returns the version of the config
|
|
|
|
func (c *Config) parseVersion(useLegacyConfig bool) (int, error) {
|
2024-11-18 05:44:39 +00:00
|
|
|
defaultVersion := 3
|
2024-08-08 14:27:07 +00:00
|
|
|
if useLegacyConfig {
|
|
|
|
defaultVersion = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := c.Get("version").(type) {
|
|
|
|
case nil:
|
|
|
|
switch len(c.Keys()) {
|
|
|
|
case 0: // No config exists, or the config file is empty, use version inferred from containerd
|
|
|
|
return defaultVersion, nil
|
|
|
|
default: // A config file exists, has content, and no version is set
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
case int64:
|
|
|
|
return int(v), nil
|
|
|
|
default:
|
|
|
|
return -1, fmt.Errorf("unsupported type for version field: %v", v)
|
|
|
|
}
|
2023-02-23 20:01:24 +00:00
|
|
|
}
|
2024-08-08 22:40:00 +00:00
|
|
|
|
|
|
|
func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {
|
|
|
|
if c == nil || c.Tree == nil {
|
|
|
|
return nil, fmt.Errorf("config is nil")
|
|
|
|
}
|
|
|
|
runtimeData := c.GetSubtreeByPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name})
|
|
|
|
return &containerdCfgRuntime{
|
|
|
|
tree: runtimeData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommandLineSource returns the CLI-based containerd config loader
|
|
|
|
func CommandLineSource(hostRoot string) toml.Loader {
|
2024-10-23 01:20:52 +00:00
|
|
|
return toml.FromCommandLine(chrootIfRequired(hostRoot, "containerd", "config", "dump")...)
|
2024-08-08 22:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func chrootIfRequired(hostRoot string, commandLine ...string) []string {
|
|
|
|
if hostRoot == "" || hostRoot == "/" {
|
|
|
|
return commandLine
|
|
|
|
}
|
|
|
|
|
|
|
|
return append([]string{"chroot", hostRoot}, commandLine...)
|
|
|
|
}
|