<p>I already found the way using <code>gcloud</code> CLI.</p>

<pre><code>gcloud auth activate-service-account --key-file=serviceaccount.json
gcloud container clusters get-credentials $clusterName \
    --zone=$zone --project=$project
kubectl config view --minify --flatten
</code></pre>

<p>However, to eliminate dependency to gcloud cli, Is there any programmatic way to achieve a similar result as above? Preferably using API exposed in Google's python client library.</p>

<p>My expected result is a portable config file that can be passed to any <code>kubectl --kubeconfig=...</code> command.</p>

<hr>

<p><strong>update</strong>: I have found that the commands I showed above results in a kube config file that still depends on gcloud cli as auth helper, probably to automatically handle token expiration. So, any workarounds are welcome.</p>

akhy
<p>I wrote a shell script which basically does exactly what you are expecting.</p>

<p><a href="https://gitlab.com/workshop21/open-source/rbac" rel="nofollow noreferrer">https://gitlab.com/workshop21/open-source/rbac</a></p>
