2022-06-24 05:28:10 +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 crio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-07-02 08:26:41 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-12-01 01:10:10 +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"
|
2022-06-24 05:28:10 +00:00
|
|
|
)
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// Config represents the cri-o config
|
2024-07-02 08:26:41 +00:00
|
|
|
type Config struct {
|
|
|
|
*toml.Tree
|
|
|
|
Logger logger.Interface
|
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
|
2024-08-08 22:40:00 +00:00
|
|
|
type crioRuntime struct {
|
|
|
|
tree *toml.Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ engine.RuntimeConfig = (*crioRuntime)(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 *crioRuntime) GetBinaryPath() string {
|
|
|
|
if c.tree != nil {
|
|
|
|
if binaryPath, ok := c.tree.GetPath([]string{"runtime_path"}).(string); ok {
|
|
|
|
return binaryPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:59:34 +00:00
|
|
|
var _ engine.Interface = (*Config)(nil)
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// New creates a cri-o config with the specified options
|
|
|
|
func New(opts ...Option) (engine.Interface, error) {
|
|
|
|
b := &builder{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(b)
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
2024-08-08 14:27:07 +00:00
|
|
|
if b.logger == nil {
|
|
|
|
b.logger = logger.New()
|
|
|
|
}
|
|
|
|
if b.configSource == nil {
|
|
|
|
b.configSource = toml.FromFile(b.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
tomlConfig, err := b.configSource.Load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
|
2024-08-08 14:27:07 +00:00
|
|
|
cfg := Config{
|
|
|
|
Tree: tomlConfig,
|
|
|
|
Logger: b.logger,
|
|
|
|
}
|
|
|
|
return &cfg, nil
|
2023-02-23 12:49:54 +00:00
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// AddRuntime adds a new runtime to the crio config
|
2024-09-27 11:19:23 +00:00
|
|
|
func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error {
|
2023-02-23 12:49:54 +00:00
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("config is nil")
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 08:26:41 +00:00
|
|
|
config := *c.Tree
|
2022-06-24 05:28:10 +00:00
|
|
|
|
2024-08-08 22:40:00 +00:00
|
|
|
runtimeNamesForConfig := engine.GetLowLevelRuntimes(c)
|
2024-07-02 08:26:41 +00:00
|
|
|
for _, r := range runtimeNamesForConfig {
|
|
|
|
if options, ok := config.GetPath([]string{"crio", "runtime", "runtimes", r}).(*toml.Tree); ok {
|
|
|
|
c.Logger.Debugf("using options from runtime %v: %v", r, options.String())
|
|
|
|
options, _ = toml.Load(options.String())
|
|
|
|
config.SetPath([]string{"crio", "runtime", "runtimes", name}, options)
|
|
|
|
break
|
|
|
|
}
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
config.SetPath([]string{"crio", "runtime", "runtimes", name, "runtime_path"}, path)
|
|
|
|
config.SetPath([]string{"crio", "runtime", "runtimes", name, "runtime_type"}, "oci")
|
2022-06-24 05:28:10 +00:00
|
|
|
|
|
|
|
if setAsDefault {
|
2023-02-23 12:49:54 +00:00
|
|
|
config.SetPath([]string{"crio", "runtime", "default_runtime"}, name)
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 08:26:41 +00:00
|
|
|
*c.Tree = config
|
2022-06-24 05:28:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// DefaultRuntime returns the default runtime for the cri-o config
|
2024-07-02 08:26:41 +00:00
|
|
|
func (c *Config) DefaultRuntime() string {
|
|
|
|
if c == nil || c.Tree == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if runtime, ok := c.GetPath([]string{"crio", "runtime", "default_runtime"}).(string); ok {
|
2023-02-23 12:49:54 +00:00
|
|
|
return runtime
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRuntime removes a runtime from the cri-o config
|
|
|
|
func (c *Config) RemoveRuntime(name string) error {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-02 08:26:41 +00:00
|
|
|
config := *c.Tree
|
2022-06-24 05:28:10 +00:00
|
|
|
if runtime, ok := config.GetPath([]string{"crio", "runtime", "default_runtime"}).(string); ok {
|
2023-02-23 12:49:54 +00:00
|
|
|
if runtime == name {
|
2022-06-24 05:28:10 +00:00
|
|
|
config.DeletePath([]string{"crio", "runtime", "default_runtime"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
runtimeClassPath := []string{"crio", "runtime", "runtimes", name}
|
2022-06-24 05:28:10 +00:00
|
|
|
config.DeletePath(runtimeClassPath)
|
|
|
|
for i := 0; i < len(runtimeClassPath); i++ {
|
|
|
|
remainingPath := runtimeClassPath[:len(runtimeClassPath)-i]
|
|
|
|
if entry, ok := config.GetPath(remainingPath).(*toml.Tree); ok {
|
|
|
|
if len(entry.Keys()) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
config.DeletePath(remainingPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-02 08:26:41 +00:00
|
|
|
*c.Tree = config
|
2022-06-24 05:28:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
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{"crio", "runtime", "runtimes", name})
|
|
|
|
return &crioRuntime{
|
|
|
|
tree: runtimeData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommandLineSource returns the CLI-based crio config loader
|
|
|
|
func CommandLineSource(hostRoot string) toml.Loader {
|
2024-10-23 01:20:52 +00:00
|
|
|
return toml.LoadFirst(
|
|
|
|
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio", "status", "config")...),
|
|
|
|
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio-status", "config")...),
|
|
|
|
)
|
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...)
|
|
|
|
}
|