Skip to content

Commit

Permalink
Merge pull request #756 from NVIDIA/cli-source-fallback
Browse files Browse the repository at this point in the history
[TOML ConfigSource] add support for executing fallback CLI commands
  • Loading branch information
tariq1890 authored Oct 23, 2024
2 parents 3c07ea0 + 0f7aba9 commit 771ac6b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
3 changes: 1 addition & 2 deletions pkg/config/engine/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {

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

func chrootIfRequired(hostRoot string, commandLine ...string) []string {
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/engine/crio/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {

// CommandLineSource returns the CLI-based crio config loader
func CommandLineSource(hostRoot string) toml.Loader {
commandLine := chrootIfRequired(hostRoot, "crio", "status", "config")
return toml.FromCommandLine(commandLine[0], commandLine[1:]...)
return toml.LoadFirst(
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio", "status", "config")...),
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio-status", "config")...),
)
}

func chrootIfRequired(hostRoot string, commandLine ...string) []string {
Expand Down
41 changes: 41 additions & 0 deletions pkg/config/toml/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
# Copyright 2024 NVIDIA CORPORATION
#
# 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 toml

import "errors"

type firstOf []Loader

func LoadFirst(loaders ...Loader) Loader {
return firstOf(loaders)
}

func (loaders firstOf) Load() (*Tree, error) {
var errs error
for _, loader := range loaders {
if loader == nil {
continue
}
tree, err := loader.Load()
if err != nil {
errs = errors.Join(errs, err)
continue
}
return tree, nil
}
return nil, errs
}
8 changes: 4 additions & 4 deletions pkg/config/toml/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func FromFile(path string) Loader {

// FromCommandLine creates a TOML source from the output of a shell command and its corresponding args.
// If the command is empty, an empty config is returned.
func FromCommandLine(cmd string, args ...string) Loader {
if len(cmd) == 0 {
func FromCommandLine(cmds ...string) Loader {
if len(cmds) == 0 {
return Empty
}
return &tomlCliSource{
command: cmd,
args: args,
command: cmds[0],
args: cmds[1:],
}
}

0 comments on commit 771ac6b

Please sign in to comment.