<p>I have a secret in my kubernetes cluster which contains a kubeconfig of another Cluster.
I want to be able to connect to that cluster in my Go code.</p>
<p>I only found ways to create a client via a kubeconfig FILE but not with a kubeconfig string.</p>
<p>This is my code so far:</p>
<pre><code>    // Read secret
    kubeconfigSecret := &amp;corev1.Secret{}
    err := apiReader.Get(context.Background(), client.ObjectKey{Namespace: namespace, Name: name}, kubeconfigSecret)
    if err != nil {
        // error handling
    }

    kubeconfigBytes, ok := kubeconfigSecret.Data[&quot;kubeconfig&quot;]
    if !ok {
        // error handling
    }

    kubeconfigString := string(kubeconfigBytes)

</code></pre>

mstruebing
<p>There are several ways for this purpose, for example:</p>
<pre class="lang-golang prettyprint-override"><code>content := `a kubeconfig string`
tmpfile, err := os.CreateTemp(&quot;&quot;, &quot;kubeconfig&quot;)
if err != nil {
    ...
}
defer os.Remove(tmpfile.Name())
if err := os.WriteFile(tmpfile.Name(), []byte(content), 0666); err != nil {
    ...
}
config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, tmpfile.Name())
if err != nil {
    ...
}

clientset,err := kubernetes.NewForConfig(config)
</code></pre>
<p>Another way is to use <code>clientcmd.RESTConfigFromKubeConfig</code>:</p>
<pre class="lang-golang prettyprint-override"><code>content := `a kubeconfig string`
config, err := clientcmd.RESTConfigFromKubeConfig([]byte(content))
if err != nil {
    ...
}
clientset, err := kubernetes.NewForConfig(config)
</code></pre>
