Skip to content

Commit e76c157

Browse files
committed
fixup! fetch current container runtime config through the command line
1 parent 53d1f09 commit e76c157

File tree

7 files changed

+80
-53
lines changed

7 files changed

+80
-53
lines changed

pkg/config/engine/api.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,3 @@ type Interface interface {
3030
type RuntimeConfig interface {
3131
GetBinaryPath() string
3232
}
33-
34-
func GetBinaryPathsForRuntimes(cfg Interface, runtimes ...string) []string {
35-
var binaryPaths []string
36-
37-
seen := make(map[string]bool)
38-
for _, runtime := range runtimes {
39-
runtimeConfig, err := cfg.GetRuntimeConfig(runtime)
40-
if err != nil {
41-
// TODO: It will be useful to log the error when GetRuntimeConfig fails for a runtime
42-
continue
43-
}
44-
binaryPath := runtimeConfig.GetBinaryPath()
45-
if binaryPath == "" || seen[binaryPath] {
46-
continue
47-
}
48-
seen[binaryPath] = true
49-
binaryPaths = append(binaryPaths, binaryPath)
50-
}
51-
52-
return binaryPaths
53-
}

pkg/config/engine/engine.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
# Copyright 2024 NVIDIA CORPORATION
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 "strings"
20+
21+
// GetBinaryPathsForRuntimes returns the list of binary paths for common rutimes.
22+
// The following list of runtimes is considered:
23+
//
24+
// the default runtime, "runc", and "crun"
25+
//
26+
// If an nvidia* runtime is set as the default runtime, this is ignored.
27+
func GetBinaryPathsForRuntimes(cfg Interface) []string {
28+
29+
var binaryPaths []string
30+
seen := make(map[string]bool)
31+
for _, runtime := range GetLowLevelRuntimes(cfg) {
32+
runtimeConfig, err := cfg.GetRuntimeConfig(runtime)
33+
if err != nil {
34+
// TODO: It will be useful to log the error when GetRuntimeConfig fails for a runtime
35+
continue
36+
}
37+
binaryPath := runtimeConfig.GetBinaryPath()
38+
if binaryPath == "" || seen[binaryPath] {
39+
continue
40+
}
41+
seen[binaryPath] = true
42+
binaryPaths = append(binaryPaths, binaryPath)
43+
}
44+
45+
return binaryPaths
46+
}
47+
48+
// GetLowLevelRuntimes returns a predefined list low-level runtimes from the specified config.
49+
// nvidia* runtimes are ignored.
50+
func GetLowLevelRuntimes(cfg Interface) []string {
51+
var runtimes []string
52+
isValidDefault := func(s string) bool {
53+
if s == "" {
54+
return false
55+
}
56+
// ignore nvidia* runtimes.
57+
return !strings.HasPrefix(s, "nvidia")
58+
}
59+
if defaultRuntime := cfg.DefaultRuntime(); isValidDefault(defaultRuntime) {
60+
runtimes = append(runtimes, defaultRuntime)
61+
}
62+
return append(runtimes, "runc", "crun")
63+
}

pkg/config/toml/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func FromFile(path string) Loader {
3535
}
3636

3737
// FromCommandLine creates a TOML source from the output of a shell command and its corresponding args.
38-
// If an empty slice is passed an empty toml config is used.
38+
// If the command is empty, an empty config is returned.
3939
func FromCommandLine(cmd string, args ...string) Loader {
4040
if len(cmd) == 0 {
4141
return Empty

tools/container/runtime/containerd/containerd.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,14 @@ func (o *Options) runtimeConfigOverride() (map[string]interface{}, error) {
168168
return runtimeOptions, nil
169169
}
170170

171-
func GetLowlevelRuntimePaths(hostRoot string) ([]string, error) {
171+
func GetLowlevelRuntimePaths(o *container.Options, co *Options) ([]string, error) {
172172
cfg, err := containerd.New(
173-
containerd.WithConfigSource(containerd.CommandLineSource(hostRoot)),
173+
containerd.WithConfigSource(containerd.CommandLineSource(o.HostRootMount)),
174+
containerd.WithRuntimeType(co.runtimeType),
175+
containerd.WithUseLegacyConfig(co.useLegacyConfig),
174176
)
175177
if err != nil {
176178
return nil, fmt.Errorf("unable to load containerd config: %w", err)
177179
}
178-
var runtimes []string
179-
runtimes = append(runtimes, "runc")
180-
if defaultRuntime := cfg.DefaultRuntime(); defaultRuntime != "" {
181-
runtimes = append(runtimes, defaultRuntime)
182-
}
183-
184-
return engine.GetBinaryPathsForRuntimes(cfg, runtimes...), nil
180+
return engine.GetBinaryPathsForRuntimes(cfg), nil
185181
}

tools/container/runtime/crio/crio.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,12 @@ func RestartCrio(o *container.Options) error {
194194
return o.Restart("crio", func(string) error { return fmt.Errorf("supporting crio via signal is unsupported") })
195195
}
196196

197-
func GetLowlevelRuntimePaths(hostRoot string) ([]string, error) {
197+
func GetLowlevelRuntimePaths(o *container.Options) ([]string, error) {
198198
cfg, err := crio.New(
199-
crio.WithConfigSource(crio.CommandLineSource(hostRoot)),
199+
crio.WithConfigSource(crio.CommandLineSource(o.HostRootMount)),
200200
)
201201
if err != nil {
202-
return nil, fmt.Errorf("unable to load containerd config: %w", err)
203-
}
204-
var runtimes []string
205-
if defaultRuntime := cfg.DefaultRuntime(); defaultRuntime != "" {
206-
runtimes = append(runtimes, defaultRuntime)
202+
return nil, fmt.Errorf("unable to load crio config: %w", err)
207203
}
208-
runtimes = append(runtimes, "runc", "crun")
209-
210-
return engine.GetBinaryPathsForRuntimes(cfg, runtimes...), nil
204+
return engine.GetBinaryPathsForRuntimes(cfg), nil
211205
}

tools/container/runtime/docker/docker.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,12 @@ func RestartDocker(o *container.Options) error {
9898
return o.Restart("docker", SignalDocker)
9999
}
100100

101-
func GetLowlevelRuntimePaths() ([]string, error) {
101+
func GetLowlevelRuntimePaths(o *container.Options) ([]string, error) {
102102
cfg, err := docker.New(
103-
docker.WithPath(DefaultConfig),
103+
docker.WithPath(o.Config),
104104
)
105105
if err != nil {
106-
return nil, fmt.Errorf("unable to load containerd config: %w", err)
107-
}
108-
var runtimes []string
109-
if defaultRuntime := cfg.DefaultRuntime(); defaultRuntime != "" {
110-
runtimes = append(runtimes, defaultRuntime)
106+
return nil, fmt.Errorf("unable to load docker config: %w", err)
111107
}
112-
113-
return engine.GetBinaryPathsForRuntimes(cfg, runtimes...), nil
108+
return engine.GetBinaryPathsForRuntimes(cfg), nil
114109
}

tools/container/runtime/runtime.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ func Cleanup(c *cli.Context, opts *Options, runtime string) error {
170170
func GetLowlevelRuntimePaths(opts *Options, runtime string) ([]string, error) {
171171
switch runtime {
172172
case containerd.Name:
173-
return containerd.GetLowlevelRuntimePaths(opts.HostRootMount)
173+
return containerd.GetLowlevelRuntimePaths(&opts.Options, &opts.containerdOptions)
174174
case crio.Name:
175-
return crio.GetLowlevelRuntimePaths(opts.HostRootMount)
175+
return crio.GetLowlevelRuntimePaths(&opts.Options)
176176
case docker.Name:
177-
return docker.GetLowlevelRuntimePaths()
177+
return docker.GetLowlevelRuntimePaths(&opts.Options)
178178
default:
179179
return nil, fmt.Errorf("undefined runtime %v", runtime)
180180
}

0 commit comments

Comments
 (0)