Skip to content

Commit 771ac6b

Browse files
authored
Merge pull request #756 from NVIDIA/cli-source-fallback
[TOML ConfigSource] add support for executing fallback CLI commands
2 parents 3c07ea0 + 0f7aba9 commit 771ac6b

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

pkg/config/engine/containerd/containerd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {
128128

129129
// CommandLineSource returns the CLI-based containerd config loader
130130
func CommandLineSource(hostRoot string) toml.Loader {
131-
commandLine := chrootIfRequired(hostRoot, "containerd", "config", "dump")
132-
return toml.FromCommandLine(commandLine[0], commandLine[1:]...)
131+
return toml.FromCommandLine(chrootIfRequired(hostRoot, "containerd", "config", "dump")...)
133132
}
134133

135134
func chrootIfRequired(hostRoot string, commandLine ...string) []string {

pkg/config/engine/crio/crio.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {
155155

156156
// CommandLineSource returns the CLI-based crio config loader
157157
func CommandLineSource(hostRoot string) toml.Loader {
158-
commandLine := chrootIfRequired(hostRoot, "crio", "status", "config")
159-
return toml.FromCommandLine(commandLine[0], commandLine[1:]...)
158+
return toml.LoadFirst(
159+
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio", "status", "config")...),
160+
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio-status", "config")...),
161+
)
160162
}
161163

162164
func chrootIfRequired(hostRoot string, commandLine ...string) []string {

pkg/config/toml/list.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 toml
18+
19+
import "errors"
20+
21+
type firstOf []Loader
22+
23+
func LoadFirst(loaders ...Loader) Loader {
24+
return firstOf(loaders)
25+
}
26+
27+
func (loaders firstOf) Load() (*Tree, error) {
28+
var errs error
29+
for _, loader := range loaders {
30+
if loader == nil {
31+
continue
32+
}
33+
tree, err := loader.Load()
34+
if err != nil {
35+
errs = errors.Join(errs, err)
36+
continue
37+
}
38+
return tree, nil
39+
}
40+
return nil, errs
41+
}

pkg/config/toml/source.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ func FromFile(path string) Loader {
3636

3737
// FromCommandLine creates a TOML source from the output of a shell command and its corresponding args.
3838
// If the command is empty, an empty config is returned.
39-
func FromCommandLine(cmd string, args ...string) Loader {
40-
if len(cmd) == 0 {
39+
func FromCommandLine(cmds ...string) Loader {
40+
if len(cmds) == 0 {
4141
return Empty
4242
}
4343
return &tomlCliSource{
44-
command: cmd,
45-
args: args,
44+
command: cmds[0],
45+
args: cmds[1:],
4646
}
4747
}

0 commit comments

Comments
 (0)