-
Notifications
You must be signed in to change notification settings - Fork 263
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
Set current namespace from kubeconfig by default #172
Conversation
Hi @nak3. Thanks for your PR. I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
} | ||
|
||
// test with all-namespace flag not defined and no namespace given | ||
func TestGetNamespaceDefaultAllNamespacesNotDefined(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed this test case because it is almost same with TestGetNamespaceDefault
. (Please let me know if I should not remove it.)
Coolio, thanks a lot ! 'was just about to fix that on my own, but even better so :-) I will have a look later today ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nak3 ! Looks good for me with some minor comments. Especially getting rid of KubeCfgFile as a global variable which is initalized in one init method and used in another init method is calling for a race sometime in the future.
pkg/kn/commands/types.go
Outdated
@@ -31,12 +31,17 @@ var KubeCfgFile string | |||
type KnParams struct { | |||
Output io.Writer | |||
ServingFactory func() (serving.ServingV1alpha1Interface, error) | |||
// CurrentNamespace is the namespace set in the current context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's clear what CurrentNamespace is. So please either remove the comment (recommended) or comment on the other parameters, too.
pkg/kn/commands/types.go
Outdated
if c.ServingFactory == nil { | ||
c.ServingFactory = GetConfig | ||
} | ||
var err error | ||
c.CurrentNamespace, err = GetCurrentNamespace(KubeCfgFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether we could get rid of sharing that global variable and use a function instead which could cache a lookup. This would free us for knowing the order when this variable was initialized (which I just checked, looks good, but you don't know what happens when people refactor code).
I.e. introducing a GetKubeCfgFile()
function which checks a private variable kubeCfgFile and, if empty, does the actual lookup as it's done in InitializeConfig
:
func GetKubeCfgFile() string {
if kubeCfgFile == "" {
kubeCfgFile = initKubeConfig()
}
return kubeCfgFile
}
(and maybe improve the error handling over there, as initKubeConfig() must not call os.Exit()
but should return an error which should be propagated here, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, I made it public and hated that. Better to go back to old Java-style getters for this one and hide the var. Thanks for suggesting this.
pkg/kn/commands/types.go
Outdated
@@ -50,3 +55,11 @@ func GetConfig() (serving.ServingV1alpha1Interface, error) { | |||
} | |||
return client, nil | |||
} | |||
|
|||
// GetCurrentNamespace returns current namespace from kubeconfig. | |||
func GetCurrentNamespace(kubeconfig string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't really comment on the logic here as I don't know anything about how to extract a current context from the configuration. Looks complicated though ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn’t it one line in the KUBECONFIG
file? Maybe my clusters are super simple...
/ok-to-test |
Thank you @rhuss Updated (and moved |
pkg/kn/core/root.go
Outdated
p.Initialize() | ||
err := p.Initialize() | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to initialize command %v", err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if panic
is the right thing to do here? I can understand the rational but the error to user will be kind of ugly. Returning error with message will be handled like all errors in main.go
...
Though I can also see the argument that this is not a common error and panicking is warranted. Conflicted a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for not panicing but exiting with a "normal" error. I think it's not impossible that initialization fails, e.g. when some .kube config file is not readable. We always strive for nice error messages.
Thank you @maximilien and @rhuss. Updated. I realized that |
pkg/kn/commands/types.go
Outdated
} | ||
} | ||
rules := clientcmd.NewDefaultClientConfigLoadingRules() | ||
rules.ExplicitPath = c.KubeCfgFile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see https://github.com/k14s/kapp/blob/0c91e8a2208ff9a8f9d92f8c01d20dc9dd21256b/pkg/kapp/cmd/core/config_factory.go#L60 for example. we dont need to specify ExplicitPath unless it's given as an explicit option. also KUBECONFIG env var will be picked up by default.
Thank you @cppforlife Updated. |
pkg/kn/commands/types.go
Outdated
return nil, err | ||
} | ||
} | ||
config, err := clientcmd.BuildConfigFromFlags("", c.KubeCfgFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also get ClientConfig()
from clientcmd.NewNonInteractiveDeferredLoadingClientConfig()
instead of doing initKubeConfig()
. i dont think we actually need initKubeConfig at all.
/test pull-knative-client-go-coverage |
@cppforlife Thank you! updated. |
pkg/kn/commands/namespaced_test.go
Outdated
testCmd.Execute() | ||
actualNamespace, err := GetNamespace(testCmd) | ||
kp := &KnParams{CurrentNamespace: "current"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you should add this line to every test case since it's implied that CurrentNamespace() from clientcmd is set. additionally is there an easy way to test GetConfig(). if not in unit test land, can it be done in e2e?
pkg/kn/commands/types.go
Outdated
} | ||
c.ClientConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) | ||
var err error | ||
c.CurrentNamespace, _, err = c.ClientConfig.Namespace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not idea that GetConfig() mutates KnParams. is there a better pattern we can have?
Thank you @cppforlife ! Updated. |
Currently kn command does not pick up namespace from kubeconfig but hardcorded default namespace. This patch fixes to get namespace from kubeconfig. Fixes #7
@nak3 : oops
|
The following is the coverage report on pkg/.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
@navidshaikh: changing LGTM is restricted to assignees, and only knative/client repo collaborators may be assigned issues. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Well! |
/retest |
/lgtm |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: cppforlife, nak3, navidshaikh The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
* Set current namespace from kubeconfig by default Currently kn command does not pick up namespace from kubeconfig but hardcorded default namespace. This patch fixes to get namespace from kubeconfig. Fixes knative#7 * Use NamespaceFactory to get current namespace * Update unit tests * Add nil check for ClientConfig
Let the caller decide. Also clarify that arguments can be flags for go test (as long as they come first).
kn
command does not pick up namespace from kubeconfig but hardcordeddefault
namespace.This patch fixes to get namespace from kubeconfig.
Fixes #7