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"
|
|
|
|
|
2023-04-24 11:58:03 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
2022-06-24 05:28:10 +00:00
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
)
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// Config represents the cri-o config
|
|
|
|
type Config toml.Tree
|
2022-06-24 05:28:10 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
return b.build()
|
|
|
|
}
|
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
|
|
|
|
func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error {
|
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("config is nil")
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
config := (toml.Tree)(*c)
|
2022-06-24 05:28:10 +00:00
|
|
|
|
|
|
|
switch runc := config.Get("crio.runtime.runtimes.runc").(type) {
|
|
|
|
case *toml.Tree:
|
|
|
|
runc, _ = toml.Load(runc.String())
|
2023-02-23 12:49:54 +00:00
|
|
|
config.SetPath([]string{"crio", "runtime", "runtimes", name}, runc)
|
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
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
*c = (Config)(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
|
|
|
|
func (c Config) DefaultRuntime() string {
|
|
|
|
config := (toml.Tree)(c)
|
|
|
|
if runtime, ok := config.GetPath([]string{"crio", "runtime", "default_runtime"}).(string); ok {
|
|
|
|
return runtime
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRuntime removes a runtime from the cri-o config
|
|
|
|
func (c *Config) RemoveRuntime(name string) error {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := (toml.Tree)(*c)
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
*c = (Config)(config)
|
2022-06-24 05:28:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// Save writes the config to the specified path
|
|
|
|
func (c Config) Save(path string) (int64, error) {
|
|
|
|
config := (toml.Tree)(c)
|
2023-07-03 13:14:22 +00:00
|
|
|
output, err := config.Marshal()
|
2022-06-24 05:28:10 +00:00
|
|
|
if err != nil {
|
2023-02-23 12:49:54 +00:00
|
|
|
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 13:14:22 +00:00
|
|
|
n, err := engine.Config(path).Write(output)
|
2023-02-23 12:49:54 +00:00
|
|
|
return int64(n), err
|
2022-06-24 05:28:10 +00:00
|
|
|
}
|