Skip to content

Commit

Permalink
Fix MustFindString returning override flags on external CLI commands
Browse files Browse the repository at this point in the history
External CLI actions cannot short-circuit on --help or --version, so we
cannot skip loading the config file if these flags are present when
running these wrapped commands. The behavior of just returning the
override flag name instead of the requested flag value was breaking
data-dir lookup when running wrapped commands.

Signed-off-by: Brad Davidson <[email protected]>
(cherry picked from commit ff5c633)
Signed-off-by: Brad Davidson <[email protected]>
  • Loading branch information
brandond committed Nov 6, 2024
1 parent ef79544 commit 48750ae
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
11 changes: 6 additions & 5 deletions cmd/k3s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"

Expand All @@ -27,6 +28,7 @@ import (
)

var criDefaultConfigPath = "/etc/crictl.yaml"
var externalCLIActions = []string{"crictl", "ctr", "kubectl"}

// main entrypoint for the k3s multicall binary
func main() {
Expand Down Expand Up @@ -105,7 +107,7 @@ func findDebug(args []string) bool {
if debug {
return debug
}
debug, _ = strconv.ParseBool(configfilearg.MustFindString(args, "debug"))
debug, _ = strconv.ParseBool(configfilearg.MustFindString(args, "debug", externalCLIActions...))
return debug
}

Expand All @@ -125,7 +127,7 @@ func findDataDir(args []string) string {
if dataDir != "" {
return dataDir
}
dataDir = configfilearg.MustFindString(args, "data-dir")
dataDir = configfilearg.MustFindString(args, "data-dir", externalCLIActions...)
if d, err := datadir.Resolve(dataDir); err == nil {
dataDir = d
} else {
Expand All @@ -143,7 +145,7 @@ func findPreferBundledBin(args []string) bool {
fs.SetOutput(io.Discard)
fs.BoolVar(&preferBundledBin, "prefer-bundled-bin", false, "Prefer bundled binaries")

preferRes := configfilearg.MustFindString(args, "prefer-bundled-bin")
preferRes := configfilearg.MustFindString(args, "prefer-bundled-bin", externalCLIActions...)
if preferRes != "" {
preferBundledBin, _ = strconv.ParseBool(preferRes)
}
Expand All @@ -158,8 +160,7 @@ func findPreferBundledBin(args []string) bool {
// it returns false so that standard CLI wrapping can occur.
func runCLIs(dataDir string) bool {
progName := filepath.Base(os.Args[0])
switch progName {
case "crictl", "ctr", "kubectl":
if slices.Contains(externalCLIActions, progName) {
if err := externalCLI(progName, dataDir, os.Args[1:]); err != nil && !errors.Is(err, context.Canceled) {
logrus.Fatal(err)
}
Expand Down
24 changes: 21 additions & 3 deletions pkg/configfilearg/defaultparser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package configfilearg

import (
"slices"

"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/version"
"github.com/sirupsen/logrus"
Expand All @@ -23,15 +25,31 @@ func MustParse(args []string) []string {
return result
}

func MustFindString(args []string, target string) string {
func MustFindString(args []string, target string, commandsWithoutOverride ...string) string {
overrideFlags := []string{"--help", "-h", "--version", "-v"}
// Check to see if the command or subcommand being executed supports override flags.
// Some subcommands such as `k3s ctr` or just `ctr` need to be extracted out even to
// provide version or help text, and we cannot short-circuit loading the config file. For
// these commands, treat failure to load the config file as a warning instead of a fatal.
if len(args) > 0 && args[0] == version.Program {
args = args[1:]
}
if len(args) > 0 && slices.Contains(commandsWithoutOverride, args[0]) {
overrideFlags = nil
}

parser := &Parser{
OverrideFlags: []string{"--help", "-h", "--version", "-v"},
OverrideFlags: overrideFlags,
EnvName: version.ProgramUpper + "_CONFIG_FILE",
DefaultConfig: "/etc/rancher/" + version.Program + "/config.yaml",
}
result, err := parser.FindString(args, target)
if err != nil {
logrus.Fatal(err)
if len(overrideFlags) > 0 {
logrus.Fatal(err)
} else {
logrus.Warn(err)
}
}
return result
}
34 changes: 28 additions & 6 deletions pkg/configfilearg/defaultparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Test_UnitMustFindString(t *testing.T) {
}{
{
name: "Target not found in config file",
args: []string{"--foo", "bar"},
args: []string{"k3s", "--foo", "bar"},
target: "token",

want: "",
Expand All @@ -95,7 +95,7 @@ func Test_UnitMustFindString(t *testing.T) {
},
{
name: "Target found in config file",
args: []string{"--foo", "bar"},
args: []string{"k3s", "--foo", "bar"},
target: "token",

want: "12345",
Expand All @@ -104,11 +104,31 @@ func Test_UnitMustFindString(t *testing.T) {
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
{
name: "Override flag found, function is short-circuited",
args: []string{"--foo", "bar", "-h"},
name: "Override flag is returned if found",
args: []string{"k3s", "--foo", "bar", "--version"},
target: "token",

want: "-h",
want: "--version",

setup: func() error { return os.Setenv("K3S_CONFIG_FILE", "./testdata/defaultdata.yaml") },
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
{
name: "Override flag is not returned for specific subcommands",
args: []string{"k3s", "ctr", "--foo", "bar", "--version"},
target: "token",

want: "12345",

setup: func() error { return os.Setenv("K3S_CONFIG_FILE", "./testdata/defaultdata.yaml") },
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
{
name: "Override flag is not returned for specific subcommands",
args: []string{"kubectl", "--foo", "bar", "--help"},
target: "token",

want: "12345",

setup: func() error { return os.Setenv("K3S_CONFIG_FILE", "./testdata/defaultdata.yaml") },
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
Expand All @@ -121,7 +141,9 @@ func Test_UnitMustFindString(t *testing.T) {
t.Errorf("Setup for MustFindString() failed = %v", err)
return
}
if got := MustFindString(tt.args, tt.target); got != tt.want {
got := MustFindString(tt.args, tt.target, "crictl", "ctr", "kubectl")
t.Logf("MustFindString(%+v, %+v) = %s", tt.args, tt.target, got)
if got != tt.want {
t.Errorf("MustFindString() = %+v\nWant = %+v", got, tt.want)
}
})
Expand Down

0 comments on commit 48750ae

Please sign in to comment.