Skip to content

Commit 2299c95

Browse files
author
Evan Lezar
committed
Merge branch 'create-config-folders' into 'main'
Ensure that folders exist when creating config files See merge request nvidia/container-toolkit/container-toolkit!433
2 parents ba80d03 + 6342dae commit 2299c95

File tree

9 files changed

+94
-104
lines changed

9 files changed

+94
-104
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
* Fix bug in creation of `/dev/char` symlinks by failing operation if kernel modules are not loaded.
1414
* Add option to load kernel modules when creating device nodes
1515
* Add option to create device nodes when creating `/dev/char` symlinks
16-
* Bump CUDA base image version to 12.1.1.
16+
* Create ouput folders if required when running `nvidia-ctk runtime configure`
17+
1718

1819
* [libnvidia-container] Support OpenSSL 3 with the Encrypt/Decrypt library
1920

21+
* [toolkit-container] Bump CUDA base image version to 12.1.1.
22+
2023
## v1.13.1
2124

2225
* Update `update-ldcache` hook to only update ldcache if it exists.

cmd/nvidia-ctk/runtime/configure/configure.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ func (m command) configureWrapper(c *cli.Context, config *config) error {
190190
return fmt.Errorf("unable to flush config: %v", err)
191191
}
192192

193-
if n == 0 {
194-
m.logger.Infof("Removed empty config from %v", outputPath)
195-
} else {
196-
m.logger.Infof("Wrote updated config to %v", outputPath)
193+
if outputPath != "" {
194+
if n == 0 {
195+
m.logger.Infof("Removed empty config from %v", outputPath)
196+
} else {
197+
m.logger.Infof("Wrote updated config to %v", outputPath)
198+
}
199+
m.logger.Infof("It is recommended that %v daemon be restarted.", config.runtime)
197200
}
198-
m.logger.Infof("It is recommended that %v daemon be restarted.", config.runtime)
199201

200202
return nil
201203
}

pkg/config/engine/config.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package engine
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
)
24+
25+
// Config represents a runtime config
26+
type Config string
27+
28+
// Write writes the specified contents to a config file.
29+
func (c Config) Write(output []byte) (int, error) {
30+
path := string(c)
31+
if path == "" {
32+
n, err := os.Stdout.Write(output)
33+
if err == nil {
34+
os.Stdout.WriteString("\n")
35+
}
36+
return n, err
37+
}
38+
39+
if len(output) == 0 {
40+
err := os.Remove(path)
41+
if err != nil {
42+
return 0, fmt.Errorf("unable to remove empty file: %v", err)
43+
}
44+
return 0, nil
45+
}
46+
47+
if dir := filepath.Dir(path); dir != "" {
48+
err := os.MkdirAll(dir, 0755)
49+
if err != nil {
50+
return 0, fmt.Errorf("unable to create directory %v: %v", dir, err)
51+
}
52+
}
53+
54+
f, err := os.Create(path)
55+
if err != nil {
56+
return 0, fmt.Errorf("unable to open %v for writing: %v", path, err)
57+
}
58+
defer f.Close()
59+
60+
return f.Write(output)
61+
}

pkg/config/engine/containerd/config_v2.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package containerd
1818

1919
import (
2020
"fmt"
21-
"os"
2221

22+
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
2323
"github.com/pelletier/go-toml"
2424
)
2525

@@ -133,34 +133,11 @@ func (c *Config) RemoveRuntime(name string) error {
133133
// Save writes the config to the specified path
134134
func (c Config) Save(path string) (int64, error) {
135135
config := c.Tree
136-
output, err := config.ToTomlString()
136+
output, err := config.Marshal()
137137
if err != nil {
138138
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
139139
}
140140

141-
if path == "" {
142-
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
143-
return int64(len(output)), nil
144-
}
145-
146-
if len(output) == 0 {
147-
err := os.Remove(path)
148-
if err != nil {
149-
return 0, fmt.Errorf("unable to remove empty file: %v", err)
150-
}
151-
return 0, nil
152-
}
153-
154-
f, err := os.Create(path)
155-
if err != nil {
156-
return 0, fmt.Errorf("unable to open '%v' for writing: %v", path, err)
157-
}
158-
defer f.Close()
159-
160-
n, err := f.WriteString(output)
161-
if err != nil {
162-
return 0, fmt.Errorf("unable to write output: %v", err)
163-
}
164-
141+
n, err := engine.Config(path).Write(output)
165142
return int64(n), err
166143
}

pkg/config/engine/containerd/option.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,23 @@ func (b *builder) build() (engine.Interface, error) {
108108

109109
// loadConfig loads the containerd config from disk
110110
func (b *builder) loadConfig(config string) (*Config, error) {
111-
b.logger.Infof("Loading config: %v", config)
112-
113111
info, err := os.Stat(config)
114112
if os.IsExist(err) && info.IsDir() {
115113
return nil, fmt.Errorf("config file is a directory")
116114
}
117115

118-
configFile := config
119116
if os.IsNotExist(err) {
120-
configFile = "/dev/null"
121-
b.logger.Infof("Config file does not exist, creating new one")
117+
b.logger.Infof("Config file does not exist; using empty config")
118+
config = "/dev/null"
119+
} else {
120+
b.logger.Infof("Loading config from %v", config)
122121
}
123122

124-
tomlConfig, err := toml.LoadFile(configFile)
123+
tomlConfig, err := toml.LoadFile(config)
125124
if err != nil {
126125
return nil, err
127126
}
128127

129-
b.logger.Infof("Successfully loaded config")
130-
131128
cfg := Config{
132129
Tree: tomlConfig,
133130
}

pkg/config/engine/crio/crio.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package crio
1818

1919
import (
2020
"fmt"
21-
"os"
2221

2322
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
2423
"github.com/pelletier/go-toml"
@@ -103,34 +102,11 @@ func (c *Config) RemoveRuntime(name string) error {
103102
// Save writes the config to the specified path
104103
func (c Config) Save(path string) (int64, error) {
105104
config := (toml.Tree)(c)
106-
output, err := config.ToTomlString()
105+
output, err := config.Marshal()
107106
if err != nil {
108107
return 0, fmt.Errorf("unable to convert to TOML: %v", err)
109108
}
110109

111-
if path == "" {
112-
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
113-
return int64(len(output)), nil
114-
}
115-
116-
if len(output) == 0 {
117-
err := os.Remove(path)
118-
if err != nil {
119-
return 0, fmt.Errorf("unable to remove empty file: %v", err)
120-
}
121-
return 0, nil
122-
}
123-
124-
f, err := os.Create(path)
125-
if err != nil {
126-
return 0, fmt.Errorf("unable to open '%v' for writing: %v", path, err)
127-
}
128-
defer f.Close()
129-
130-
n, err := f.WriteString(output)
131-
if err != nil {
132-
return 0, fmt.Errorf("unable to write output: %v", err)
133-
}
134-
110+
n, err := engine.Config(path).Write(output)
135111
return int64(n), err
136112
}

pkg/config/engine/crio/option.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ func (b *builder) loadConfig(config string) (*Config, error) {
6767
return nil, fmt.Errorf("config file is a directory")
6868
}
6969

70-
configFile := config
7170
if os.IsNotExist(err) {
72-
configFile = "/dev/null"
73-
b.logger.Infof("Config file does not exist, creating new one")
71+
b.logger.Infof("Config file does not exist; using empty config")
72+
config = "/dev/null"
73+
} else {
74+
b.logger.Infof("Loading config from %v", config)
7475
}
7576

76-
cfg, err := toml.LoadFile(configFile)
77+
cfg, err := toml.LoadFile(config)
7778
if err != nil {
7879
return nil, err
7980
}

pkg/config/engine/docker/docker.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package docker
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"os"
2322

2423
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2524
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
@@ -122,29 +121,6 @@ func (c Config) Save(path string) (int64, error) {
122121
return 0, fmt.Errorf("unable to convert to JSON: %v", err)
123122
}
124123

125-
if path == "" {
126-
os.Stdout.WriteString(fmt.Sprintf("%s\n", output))
127-
return int64(len(output)), nil
128-
}
129-
130-
if len(output) == 0 {
131-
err := os.Remove(path)
132-
if err != nil {
133-
return 0, fmt.Errorf("unable to remove empty file: %v", err)
134-
}
135-
return 0, nil
136-
}
137-
138-
f, err := os.Create(path)
139-
if err != nil {
140-
return 0, fmt.Errorf("unable to open %v for writing: %v", path, err)
141-
}
142-
defer f.Close()
143-
144-
n, err := f.WriteString(string(output))
145-
if err != nil {
146-
return 0, fmt.Errorf("unable to write output: %v", err)
147-
}
148-
149-
return int64(n), nil
124+
n, err := engine.Config(path).Write(output)
125+
return int64(n), err
150126
}

pkg/config/engine/docker/option.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ func (b *builder) build() (*Config, error) {
5858
}
5959

6060
// loadConfig loads the docker config from disk
61-
func (b *builder) loadConfig(configFilePath string) (*Config, error) {
62-
b.logger.Infof("Loading docker config from %v", configFilePath)
63-
64-
info, err := os.Stat(configFilePath)
61+
func (b *builder) loadConfig(config string) (*Config, error) {
62+
info, err := os.Stat(config)
6563
if os.IsExist(err) && info.IsDir() {
6664
return nil, fmt.Errorf("config file is a directory")
6765
}
6866

6967
cfg := make(Config)
7068

7169
if os.IsNotExist(err) {
72-
b.logger.Infof("Config file does not exist, creating new one")
70+
b.logger.Infof("Config file does not exist; using empty config")
7371
return &cfg, nil
7472
}
7573

76-
readBytes, err := ioutil.ReadFile(configFilePath)
74+
b.logger.Infof("Loading config from %v", config)
75+
readBytes, err := ioutil.ReadFile(config)
7776
if err != nil {
7877
return nil, fmt.Errorf("unable to read config: %v", err)
7978
}
@@ -82,7 +81,5 @@ func (b *builder) loadConfig(configFilePath string) (*Config, error) {
8281
if err := json.NewDecoder(reader).Decode(&cfg); err != nil {
8382
return nil, err
8483
}
85-
86-
b.logger.Infof("Successfully loaded config")
8784
return &cfg, nil
8885
}

0 commit comments

Comments
 (0)