mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-25 05:21:33 +00:00
Add engine.Config to encapsulate writing
This change adds an engine.Config type to encapsulate the writing of config files for container engines. Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
bbe9742c46
commit
baf94181aa
@ -190,12 +190,14 @@ func (m command) configureWrapper(c *cli.Context, config *config) error {
|
|||||||
return fmt.Errorf("unable to flush config: %v", err)
|
return fmt.Errorf("unable to flush config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if outputPath != "" {
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
m.logger.Infof("Removed empty config from %v", outputPath)
|
m.logger.Infof("Removed empty config from %v", outputPath)
|
||||||
} else {
|
} else {
|
||||||
m.logger.Infof("Wrote updated config to %v", outputPath)
|
m.logger.Infof("Wrote updated config to %v", outputPath)
|
||||||
}
|
}
|
||||||
m.logger.Infof("It is recommended that %v daemon be restarted.", config.runtime)
|
m.logger.Infof("It is recommended that %v daemon be restarted.", config.runtime)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
53
pkg/config/engine/config.go
Normal file
53
pkg/config/engine/config.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
# Copyright (c) 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 engine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config represents a runtime config
|
||||||
|
type Config string
|
||||||
|
|
||||||
|
// Write writes the specified contents to a config file.
|
||||||
|
func (c Config) Write(output []byte) (int, error) {
|
||||||
|
path := string(c)
|
||||||
|
if path == "" {
|
||||||
|
n, err := os.Stdout.Write(output)
|
||||||
|
if err == nil {
|
||||||
|
os.Stdout.WriteString("\n")
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(output) == 0 {
|
||||||
|
err := os.Remove(path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("unable to remove empty file: %v", err)
|
||||||
|
}
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("unable to open %v for writing: %v", path, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
return f.Write(output)
|
||||||
|
}
|
@ -18,8 +18,8 @@ package containerd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
||||||
"github.com/pelletier/go-toml"
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -133,34 +133,11 @@ func (c *Config) RemoveRuntime(name string) error {
|
|||||||
// Save writes the config to the specified path
|
// Save writes the config to the specified path
|
||||||
func (c Config) Save(path string) (int64, error) {
|
func (c Config) Save(path string) (int64, error) {
|
||||||
config := c.Tree
|
config := c.Tree
|
||||||
output, err := config.ToTomlString()
|
output, err := config.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
n, err := engine.Config(path).Write(output)
|
||||||
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
|
||||||
return int64(len(output)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(output) == 0 {
|
|
||||||
err := os.Remove(path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to remove empty file: %v", err)
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to open '%v' for writing: %v", path, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
n, err := f.WriteString(output)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to write output: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return int64(n), err
|
return int64(n), err
|
||||||
}
|
}
|
||||||
|
@ -108,26 +108,23 @@ func (b *builder) build() (engine.Interface, error) {
|
|||||||
|
|
||||||
// loadConfig loads the containerd config from disk
|
// loadConfig loads the containerd config from disk
|
||||||
func (b *builder) loadConfig(config string) (*Config, error) {
|
func (b *builder) loadConfig(config string) (*Config, error) {
|
||||||
b.logger.Infof("Loading config: %v", config)
|
|
||||||
|
|
||||||
info, err := os.Stat(config)
|
info, err := os.Stat(config)
|
||||||
if os.IsExist(err) && info.IsDir() {
|
if os.IsExist(err) && info.IsDir() {
|
||||||
return nil, fmt.Errorf("config file is a directory")
|
return nil, fmt.Errorf("config file is a directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
configFile := config
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
configFile = "/dev/null"
|
b.logger.Infof("Config file does not exist; using empty config")
|
||||||
b.logger.Infof("Config file does not exist, creating new one")
|
config = "/dev/null"
|
||||||
|
} else {
|
||||||
|
b.logger.Infof("Loading config from %v", config)
|
||||||
}
|
}
|
||||||
|
|
||||||
tomlConfig, err := toml.LoadFile(configFile)
|
tomlConfig, err := toml.LoadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.logger.Infof("Successfully loaded config")
|
|
||||||
|
|
||||||
cfg := Config{
|
cfg := Config{
|
||||||
Tree: tomlConfig,
|
Tree: tomlConfig,
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ package crio
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
||||||
"github.com/pelletier/go-toml"
|
"github.com/pelletier/go-toml"
|
||||||
@ -103,34 +102,11 @@ func (c *Config) RemoveRuntime(name string) error {
|
|||||||
// Save writes the config to the specified path
|
// Save writes the config to the specified path
|
||||||
func (c Config) Save(path string) (int64, error) {
|
func (c Config) Save(path string) (int64, error) {
|
||||||
config := (toml.Tree)(c)
|
config := (toml.Tree)(c)
|
||||||
output, err := config.ToTomlString()
|
output, err := config.Marshal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
n, err := engine.Config(path).Write(output)
|
||||||
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
|
||||||
return int64(len(output)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(output) == 0 {
|
|
||||||
err := os.Remove(path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to remove empty file: %v", err)
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to open '%v' for writing: %v", path, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
n, err := f.WriteString(output)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to write output: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return int64(n), err
|
return int64(n), err
|
||||||
}
|
}
|
||||||
|
@ -67,13 +67,14 @@ func (b *builder) loadConfig(config string) (*Config, error) {
|
|||||||
return nil, fmt.Errorf("config file is a directory")
|
return nil, fmt.Errorf("config file is a directory")
|
||||||
}
|
}
|
||||||
|
|
||||||
configFile := config
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
configFile = "/dev/null"
|
b.logger.Infof("Config file does not exist; using empty config")
|
||||||
b.logger.Infof("Config file does not exist, creating new one")
|
config = "/dev/null"
|
||||||
|
} else {
|
||||||
|
b.logger.Infof("Loading config from %v", config)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := toml.LoadFile(configFile)
|
cfg, err := toml.LoadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ package docker
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
||||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
||||||
@ -122,29 +121,6 @@ func (c Config) Save(path string) (int64, error) {
|
|||||||
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
|
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
n, err := engine.Config(path).Write(output)
|
||||||
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
|
return int64(n), err
|
||||||
return int64(len(output)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(output) == 0 {
|
|
||||||
err := os.Remove(path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to remove empty file: %v", err)
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to open %v for writing: %v", path, err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
n, err := f.WriteString(string(output))
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("unable to write output: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return int64(n), nil
|
|
||||||
}
|
}
|
||||||
|
@ -58,10 +58,8 @@ func (b *builder) build() (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// loadConfig loads the docker config from disk
|
// loadConfig loads the docker config from disk
|
||||||
func (b *builder) loadConfig(configFilePath string) (*Config, error) {
|
func (b *builder) loadConfig(config string) (*Config, error) {
|
||||||
b.logger.Infof("Loading docker config from %v", configFilePath)
|
info, err := os.Stat(config)
|
||||||
|
|
||||||
info, err := os.Stat(configFilePath)
|
|
||||||
if os.IsExist(err) && info.IsDir() {
|
if os.IsExist(err) && info.IsDir() {
|
||||||
return nil, fmt.Errorf("config file is a directory")
|
return nil, fmt.Errorf("config file is a directory")
|
||||||
}
|
}
|
||||||
@ -69,11 +67,12 @@ func (b *builder) loadConfig(configFilePath string) (*Config, error) {
|
|||||||
cfg := make(Config)
|
cfg := make(Config)
|
||||||
|
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
b.logger.Infof("Config file does not exist, creating new one")
|
b.logger.Infof("Config file does not exist; using empty config")
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
readBytes, err := ioutil.ReadFile(configFilePath)
|
b.logger.Infof("Loading config from %v", config)
|
||||||
|
readBytes, err := ioutil.ReadFile(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read config: %v", err)
|
return nil, fmt.Errorf("unable to read config: %v", err)
|
||||||
}
|
}
|
||||||
@ -82,7 +81,5 @@ func (b *builder) loadConfig(configFilePath string) (*Config, error) {
|
|||||||
if err := json.NewDecoder(reader).Decode(&cfg); err != nil {
|
if err := json.NewDecoder(reader).Decode(&cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.logger.Infof("Successfully loaded config")
|
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user