Fix potential redefined error with kubeconfig flag (#2855)

This commit is contained in:
David Simansky 2023-10-09 15:40:48 +02:00 committed by GitHub
parent d0a82f9cbb
commit bae23eb357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -44,8 +44,12 @@ func (c *ClientConfig) InitFlags(fs *flag.FlagSet) {
fs.StringVar(&c.ServerURL, "server", "", fs.StringVar(&c.ServerURL, "server", "",
"The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") "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"), if f := fs.Lookup("kubeconfig"); f != nil {
"Path to a kubeconfig. Only required if out-of-cluster.") c.Kubeconfig = f.Value.String()
} 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.") fs.IntVar(&c.Burst, "kube-api-burst", int(envVarOrDefault("KUBE_API_BURST", 0)), "Maximum burst for throttle.")

View File

@ -48,3 +48,24 @@ func TestInitFlag(t *testing.T) {
t.Errorf("ClientConfig mismatch: diff(-want,+got):\n%s", cmp.Diff(expect, c)) 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))
}
}