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

How can I get the namespace of the context I'm using? #1359

Closed
davidmichaelkarr opened this issue Jun 7, 2024 · 4 comments
Closed

How can I get the namespace of the context I'm using? #1359

davidmichaelkarr opened this issue Jun 7, 2024 · 4 comments

Comments

@davidmichaelkarr
Copy link

I'm trying to use client-go v0.24.0 to talk to my v1.24.x Kubernetes cluster. I can hack the code in the "out-of-cluster-client-configuration" sample to hardcode the namespace, and that works, but I really need to use the namespace specified in the "namespace" property of the context from the ~/.kube/config . I've been looking through the "Config" type returned from "BuildConfigFromFlags", but I can't see any indication in that of the namespace for the context. I find it hard to believe that I'd have to get this info by separately parsing the config file. That is certainly doable, but I would think it should be available through a more direct interface.

@ldemailly
Copy link

This seems to work

	cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
		&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfigPath}, nil)

	rCfg, err := cfg.RawConfig()
	if err != nil {
		fmt.Printf("error getting raw config: %v\n", err)
		os.Exit(1)
	}
	namespace := rCfg.Contexts[rCfg.CurrentContext].Namespace
	kubeConfig, err := cfg.ClientConfig()

(ie opening up BuildConfigFromFlags and making it do return the internal config object instead of just the ClientConfig)

@ldemailly
Copy link

@ldemailly
Copy link

you can also even more simply call:

	namespace, nsOver, err := cfg.Namespace()

but that one won't be empty, it defaults to default (not sure when the override is true)

@davidmichaelkarr
Copy link
Author

This all works fine, thanks. I also wonder what the override flag is for. I'll get to that at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants