Change:
<!-- Please use this template while reporting a bug and provide as much info as possible. Not doing so may result in your bug not being addressed in a timely manner. Thanks!--What happened: In-cluster namespace isn't overridden by kubeconfig if the context is specified via kubectl flag. When using , the override works as expected. What you expected to happen: If I specify for a context in kubeconfig, then I expect to use as the default namespace, regardless of whatever's in . How to reproduce it (as minimally and precisely as possible): Anything else we need to know?: supposedly fixed this issue, but not for the case where the context is specified via a command line flag (). This is because this line checks for . Environment: Kubernetes version (use ): v1.10.5 (details above) Cloud provider or hardware configuration: - OS (e.g. from /etc/os-release): - Kernel (e.g. ): - Install tools: - Others: - <!-- DO NOT EDIT BELOW THIS LINE --/kind bug
Reiterating the mentions to trigger a notification: <detailsIn response to : Instructions for interacting with me using PR comments are available . If you have questions or suggestions related to my behavior, please file an issue against the repository. </details
fixed in

Discussion:
// if we got a default namespace, determine whether it was explicit or implicit if raw, err := mergedKubeConfig.RawConfig(); err == nil { <del> if context := raw.Contexts[raw.CurrentContext]; context != nil && len(context.Namespace) > 0 { </del> <ins> // determine the current context currentContext := raw.CurrentContext if config.overrides != nil && len(config.overrides.CurrentContext) > 0 { currentContext = config.overrides.CurrentContext } if context := raw.Contexts[currentContext]; context != nil && len(context.Namespace) > 0 { </ins> return ns, false, nil } }

} type testClientConfig struct { <ins> rawconfig          *clientcmdapi.Config </ins> config             *restclient.Config namespace          string namespaceSpecified bool

} func (c *testClientConfig) RawConfig() (clientcmdapi.Config, error) { <del> return clientcmdapi.Config{}, fmt.Errorf("unexpected call") </del> <ins> if c.rawconfig == nil { return clientcmdapi.Config{}, fmt.Errorf("unexpected call") } return *c.rawconfig, nil </ins> } func (c *testClientConfig) ClientConfig() (*restclient.Config, error) { return c.config, c.err

testCases := map[string]struct { clientConfig *testClientConfig icc          *testICC <ins> overrides    *ConfigOverrides </ins> checkedICC bool result     string <del> ok         bool </del> <ins> overridden bool </ins> err        error }{ "in-cluster checked on empty error": {

clientConfig: &testClientConfig{namespace: "test", namespaceSpecified: true}, icc:          &testICC{}, <del> result: "test", ok:     true, </del> <ins> result:     "test", overridden: true, </ins> }, "in-cluster checked when namespace is not specified, but is defaulted": {

checkedICC: true, result:     "test", <del> ok:         false, </del> <ins> overridden: false, </ins> }, "in-cluster error returned when config is empty": {

checkedICC: true, result:     "test", <del> ok:         true, </del> <ins> overridden: true, </ins> }, "in-cluster config returned when config is empty and namespace is defaulted but not explicitly set": {

checkedICC: true, result:     "test", <del> ok:         false, </del> <ins> overridden: false, }, "overridden context used to verify explicit namespace in config": { clientConfig: &testClientConfig{ namespace:          "default", namespaceSpecified: false, // a namespace that comes from a context is not considered overridden rawconfig:          &clientcmdapi.Config{Contexts: map[string]*clientcmdapi.Context{"overridden-context": {Namespace: "default"}}}, }, overrides: &ConfigOverrides{CurrentContext: "overridden-context"}, icc: &testICC{ possible: true, testClientConfig: testClientConfig{ namespace:          "icc", namespaceSpecified: false, // a namespace that comes from icc is not considered overridden }, }, checkedICC: true, result:     "default", overridden: false, // a namespace that comes from a context is not considered overridden </ins> }, } for name, test := range testCases { <del> c := &DeferredLoadingClientConfig{icc: test.icc} c.clientConfig = test.clientConfig ns, ok, err := c.Namespace() if test.icc.called != test.checkedICC { t.Errorf("%s: unexpected in-cluster-config call %t", name, test.icc.called) } if err != test.err || ns != test.result || ok != test.ok { t.Errorf("%s: unexpected result: %v %s %t", name, err, ns, ok) } </del> <ins> t.Run(name, func(t *testing.T) { c := &DeferredLoadingClientConfig{icc: test.icc, overrides: test.overrides} c.clientConfig = test.clientConfig ns, overridden, err := c.Namespace() if test.icc.called != test.checkedICC { t.Errorf("%s: unexpected in-cluster-config call %t", name, test.icc.called) } if err != test.err || ns != test.result || overridden != test.overridden { t.Errorf("%s: unexpected result: %v %s %t", name, err, ns, overridden) } }) </ins> } }