<p>When I run <code>kubectl</code> inside of a pod it defaults to "in-cluster config" (defined by files in <code>/var/run/secrets/kubernetes.io/serviceaccount</code>). If I want to wrap <code>kubectl</code> inside of a call to Python subprocess with <code>shell=False</code>, how do I tell <code>kubectl</code> where to find the in-cluster config?</p>

<p>Since when I run <code>shell=False</code> none of the environment makes it into the subprocess. It seems I need to explicitly pass some environment variables or other system state to the subprocess call for <code>kubectl</code> to discover the in-cluster config. </p>

<p>How does <code>kubectl</code> discover this config? Are there a simple few variables to pass through?</p>

<p>You will have to construct a <code>KUBECONFIG</code> by hand, given those values, since that's more-or-less exactly what the <a href="https://github.com/kubernetes-client/python-base/blob/474e9fb32293fa05098e920967bb0e0645182d5b/config/incluster_config.py#L81-L86" rel="nofollow noreferrer">python client does anyway</a>. In short, either in python or via the following commands:</p>

<pre class="lang-sh prettyprint-override"><code>kubectl config set-cluster the-cluster --server="https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}" --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
kubectl config set-credentials pod-token --token="$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"
kubectl config set-context pod-context --cluster=the-cluster --user=pod-token
kubectl config use-context pod-context
</code></pre>

<p>and then you're off to the races</p>
