Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of wrapped subcommands when run with a path #11306

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/configfilearg/defaultparser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configfilearg

import (
"path/filepath"
"slices"

"github.com/k3s-io/k3s/pkg/cli/cmds"
Expand Down Expand Up @@ -31,10 +32,10 @@ func MustFindString(args []string, target string, commandsWithoutOverride ...str
// 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 {
if len(args) > 0 && filepath.Base(args[0]) == version.Program {
args = args[1:]
}
if len(args) > 0 && slices.Contains(commandsWithoutOverride, args[0]) {
if len(args) > 0 && slices.Contains(commandsWithoutOverride, filepath.Base(args[0])) {
overrideFlags = nil
}

Expand Down
22 changes: 21 additions & 1 deletion pkg/configfilearg/defaultparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,32 @@ func Test_UnitMustFindString(t *testing.T) {
teardown: func() error { return os.Unsetenv("K3S_CONFIG_FILE") },
},
{
name: "Override flag is not returned for specific subcommands",
name: "Override flag is not returned for specific subcommands with full path",
args: []string{"/usr/local/bin/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 wrapped commands",
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") },
},
{
name: "Override flag is not returned for wrapped commands with full path",
args: []string{"/usr/local/bin/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 Down
Loading