Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't override Kubernetes config in Kubernetes Go client

Tags:

I want to use Kubernetes Go client to execute various actions in a cluster. I am loading my local kubeconfig which contains multiple clusters and contexts. The default context is prod and one of the config values I want to override is the CurrentContext

    clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
        &clientcmd.ClientConfigLoadingRules{ExplicitPath: "/Users/me/.kube/config"},
        &clientcmd.ConfigOverrides{
            CurrentContext: "stage",
        })

    rawConfig, _ := clientConfig.RawConfig()
    log.Printf(rawConfig.CurrentContext) // outputs "prod" instead of "stage"

When I inspect RawConfig() the current context is still "prod" instead of "stage". Why does the config override not work?

Also how does the override for AuthInfo etc. work? The override accepts only a single AuthInfo whereas the configuration contains a map of AuthInfo etc.

GitHub related issue https://github.com/kubernetes/client-go/issues/735

like image 737
Hedge Avatar asked Jan 12 '20 21:01

Hedge


People also ask

Does kubectl use client go?

client-go is the official client library of Kubernetes, which is used internally by Kubernetes as well. The Kubernetes command line tool kubectl is built on client-go.

What is Kubernetes Clientset?

Clientset contains the clients for groups. Each group has exactly one version included in a Clientset.

How do I change the context in Kubernetes?

After your clusters, users, and contexts are defined in one or more configuration files, you can quickly switch between clusters by using the kubectl config use-context command. Note: A file that is used to configure access to a cluster is sometimes called a kubeconfig file.


1 Answers

Why does the config override not work?

according to

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L57-L58

// ClientConfig is used to make it easy to get an api server client
type ClientConfig interface {
    // RawConfig returns the merged result of all overrides
    RawConfig() (clientcmdapi.Config, error)

RawConfig should return config with overrides, but it really doesn't

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L122-L124

func (config *DirectClientConfig) RawConfig() (clientcmdapi.Config, error) {
    return config.config, nil
}

just return config without overrides. You could see a possible solution here in my patch

https://github.com/vvelikodny/kubernetes-client-go/pull/1/files

Also how does the override for AuthInfo etc. work? The override accepts only a single AuthInfo whereas the configuration contains a map of AuthInfo etc.

override only AuthInfo with the key of username which presented in context.AuthInfo (string).

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L424-L437

https://github.com/kubernetes/client-go/blob/a432bd9ba7da427ae0a38a6889d72136bce4c4ea/tools/clientcmd/client_config.go#L388-L394

// getAuthInfoName returns a string containing the current authinfo name for the current context,
// and a boolean indicating  whether the default authInfo name is overwritten by a user-set flag, or
// left as its default value
func (config *DirectClientConfig) getAuthInfoName() (string, bool) {
    if len(config.overrides.Context.AuthInfo) != 0 {
        return config.overrides.Context.AuthInfo, true
    }
    context, _ := config.getContext()
    return context.AuthInfo, false
}
like image 115
Vitaly Velikodny Avatar answered Sep 30 '22 19:09

Vitaly Velikodny