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 potential --kubeconfig redefined error #2855

Merged
merged 1 commit into from
Oct 9, 2023
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
8 changes: 6 additions & 2 deletions environment/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ func (c *ClientConfig) InitFlags(fs *flag.FlagSet) {
fs.StringVar(&c.ServerURL, "server", "",
"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")

fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"),
"Path to a kubeconfig. Only required if out-of-cluster.")
if f := fs.Lookup("kubeconfig"); f != nil {
c.Kubeconfig = f.Value.String()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually we call InitFlags method then a flags.Parse gets called

Given we're setting c.Kubeconfig here - i don't think it will update after flags.Parse is called.

So this config will have a different value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed c.Kubeconfig will set as default value of first defined --kubeconfig. Not reflecting values provided as args and flags.Parse() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer a revert here? There are several places in knative/pkg that call flags.Parse() and could be refactored to isolated flatset, but that's probably not worth it.

flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer a revert here?

I think so - otherwise it's a subtle bug that I will be difficult to discover.

There are several places in knative/pkg that call flags.Parse() and could be refactored to isolated flatset, but that's probably not worth it.

Yeah a lot of the test code had that - hence why I pulled out this resource into a separate package that doesn't call Parse

I don't have a good solution for you to mix this with kubebuilder. You may want to initialize your own flagset and configure each configuration yourself

} else {
fs.StringVar(&c.Kubeconfig, "kubeconfig", os.Getenv("KUBECONFIG"),
"Path to a kubeconfig. Only required if out-of-cluster.")
}

fs.IntVar(&c.Burst, "kube-api-burst", int(envVarOrDefault("KUBE_API_BURST", 0)), "Maximum burst for throttle.")

Expand Down
21 changes: 21 additions & 0 deletions environment/client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,24 @@ func TestInitFlag(t *testing.T) {
t.Errorf("ClientConfig mismatch: diff(-want,+got):\n%s", cmp.Diff(expect, c))
}
}

// TestInitFlagWithKubeconfing verify flag conflict error "flag redefined: kubeconfig"
func TestInitFlagWithKubeconfing(t *testing.T) {
t.Setenv("KUBECONFIG", "myconfig")

c := new(ClientConfig)
flags := flag.NewFlagSet("test", flag.ExitOnError)
flags.String("kubeconfig", "/test/path", "")
c.InitFlags(flags)

// Call parse() here as InitFlags does not call it.
flags.Parse([]string{})

expect := &ClientConfig{
Kubeconfig: "/test/path",
}

if !cmp.Equal(c, expect) {
t.Errorf("ClientConfig mismatch: diff(-want,+got):\n%s", cmp.Diff(expect, c))
}
}
Loading