Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use kubectl context in kubernetes client-go

Tags:

go

kubernetes

How can I use a normal context to configure the kubernetes client-go?

    package kube

    import (
        "fmt"

        "k8s.io/client-go/kubernetes"
        "k8s.io/client-go/rest"
        "k8s.io/client-go/tools/clientcmd"
    )

    // GetKubeClient creates a Kubernetes config and client for a given kubeconfig context.
    func GetKubeClient(context string) (*rest.Config, kubernetes.Interface, error) {
        config, err := configForContext(context)
        if err != nil {
            return nil, nil, err
        }
        client, err := kubernetes.NewForConfig(config)
        if err != nil {
            return nil, nil, fmt.Errorf("could not get Kubernetes client: %s", err)
        }
        return config, client, nil
    }

    // configForContext creates a Kubernetes REST client configuration for a given kubeconfig context.
    func configForContext(context string) (*rest.Config, error) {
        config, err := getConfig(context).ClientConfig()
        if err != nil {
            return nil, fmt.Errorf("could not get Kubernetes config for context %q: %s", context, err)
        }
        return config, nil
    }

    // getConfig returns a Kubernetes client config for a given context.
    func getConfig(context string) clientcmd.ClientConfig {
        rules := clientcmd.NewDefaultClientConfigLoadingRules()
        rules.DefaultClientConfig = &clientcmd.DefaultClientConfig

        overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults}

        if context != "" {
            overrides.CurrentContext = context
        }
        return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides)
    }

If I try this code (got it from helm), the api server is not correctly set and the client wants to connect to the default host localhost:8080.

like image 210
Ben Keil Avatar asked May 20 '18 13:05

Ben Keil


People also ask

How do you get context from kubectl?

Kubectl current-context command shows the current context of kubectl. When you enter the 'kubectl config current-context' in the virtual machine environment, the following output will be displayed. The 'kubectl config use-context cluster-name' command is used to set the default context to the given cluster name.

How do you set-context in kubectl?

// in localhost cluster, create a context for accessing local cluster's default namespace$ kubectl config set-context default/local/myself --user=myself@local --namespace=default --cluster=local-clusterContext "default/local/myself" created.// furthermore, create another context for remote cluster$ kubectl config set- ...

How do I add context to Kubeconfig?

What this says is that you can create or modify contexts in your kubeconfig file with the command kubectl config set-context. This command also accepts the name of the context to be changed (or --current if you want to change the current context), as well as --user, --cluster, and --namespace options.

What is kubectl context?

A Kubernetes context is used to group access parameters under an easily recognizable name in a kubeconfig file – a file used to configure access to clusters. It is the connection to a particular cluster used by kubectl. This concept only applies in the place where the kubectl command is run.


1 Answers

Found the problem. The implementation of github.com/imdario/mergo changed in a newer version and breaks the actual behavior of generating the client config. So just only use revision 6633656539c1639d9d78127b7d47c622b5d7b6dc like in the official kubernetes cient-go repository.

https://github.com/kubernetes/client-go/issues/415

like image 69
Ben Keil Avatar answered Sep 24 '22 04:09

Ben Keil