<pre class="lang-golang prettyprint-override"><code>    var kubeconfig *string
    if home := homedir.HomeDir(); home != &quot;&quot; {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
    } else {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
    }
    flag.Parse()

    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
    if err != nil {
        panic(err)
    }
</code></pre>
<p>The above code snippet demonstrates building the kubernetes config from input flags using <code>client-go</code>, but is there a way to construct the config from a kubeconfig in memory?</p>

Weikun Lin
<p>Try <a href="https://pkg.go.dev/k8s.io/client-go/tools/clientcmd#RESTConfigFromKubeConfig" rel="nofollow noreferrer">RESTConfigFromKubeConfig</a>.</p>
<pre class="lang-golang prettyprint-override"><code>// RESTConfigFromKubeConfig is a convenience method to give back a restconfig from your kubeconfig bytes.
// For programmatic access, this is what you want 80% of the time
func RESTConfigFromKubeConfig(configBytes []byte) (*restclient.Config, error)
</code></pre>
<pre class="lang-golang prettyprint-override"><code>// Get the kubeconfig bytes from somewhere.
kubeConfigBytes := []byte(&quot;...&quot;)

config, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigBytes)
if err != nil {
    log.Fatal(err)
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
   log.Fatal(err)
}

// Do something with clientset.
fmt.Println(clientset)
</code></pre>
