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

genericclioptions: Inferring namespace from config flag or kubeconfig #9

Open
ahmetb opened this issue Dec 22, 2019 · 4 comments
Open
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.

Comments

@ahmetb
Copy link
Member

ahmetb commented Dec 22, 2019

I'm trying to implement a very simple "kubectl get" using pkg/genericclioptions.

In my command, I see --namespace option registered.
When I invoke my command, I don't specify any value for --namespace, because I expect that it will read the kubeconfig (using default loading rules) and find the current namespace name (if set), otherwise will assume "default".

However, when I create a client and query, I get no namespace segment in URLs, so my Get() requests are failing with 404.

How is someone supposed to automatically have namespace field populated while using genericclioptions?

Here's what I'm doing:

    cf = genericclioptions.NewConfigFlags(true)
    cf.AddFlags(rootCmd.Flags()) 

    restConfig, err := cf.ToRESTConfig()
	if err != nil {
		return err
	}

    dyn, err := dynamic.NewForConfig(config)
	if err != nil {
		return nil, fmt.Errorf("failed to construct dynamic client: %w", err)
	}

    result, err := dyn.Resource(apiRes).Get(name, metav1.GetOptions{})
	if err != nil {
		return fmt.Errorf("failed to get: %w", err)
	}
    // debugging shows Get() call above doesn't add a "namespace" segment to the URL

I have tried doing dyn.Resource(apiRes).Namespace(*cf.Namespace).Get(...) but since --namespace is not specified it won't do anything.

Since I use ToRESTConfig() method, I expect this package to automatically find my namespace (from kubeconfig) or assume "default" and inject that to the URL.

Why is this not happening?

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 21, 2020
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Apr 20, 2020
@ahmetb
Copy link
Member Author

ahmetb commented Apr 21, 2020

/lifecycle frozen

@k8s-ci-robot k8s-ci-robot added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. and removed lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. labels Apr 21, 2020
@vharsh
Copy link

vharsh commented Sep 27, 2021

Ah, this is likely happening because the methods like ToDiscoveryClient, ToRESTConfig() expect AddFlags method to have been called earlier(preferably with all the args passed to the binary) but this hasn't happened already in pkg/genericclioptions with the right set of pflags from taken from the os.Args.

IMO, shouldn't cf.AddFlags be called with all arguments(os.Args) as rootCmd might not have defined all the --token, --namespace, --password flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness.
Projects
None yet
Development

No branches or pull requests

4 participants