mirror of https://github.com/knative/pkg.git
Fix potential redefined error with kubeconfig flag (#2855)
This commit is contained in:
parent
d0a82f9cbb
commit
bae23eb357
|
|
@ -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.")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue