<p>I need to get a list of all namespaces in a specifc Kubernetes cluster, using the Kubernetes API.  Because I need to loop through multiple clusters in my Python program, I need to specify the cluster every time I call the API.</p>

<p>One option is to use list_namespace(), as described in <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md" rel="noreferrer">https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md</a></p>

<p>However, this API doesn't allow me to specify the cluster.  It picks up the cluster from the current-context in my .kube config file.  If I remove or rename the config file, the API call fails completely.</p>

<p>I also found an extensions API at <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/ExtensionsV1beta1Api.md" rel="noreferrer">https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/ExtensionsV1beta1Api.md</a></p>

<p>Unfortunately, there is no API there to retrieve a list of namespaces.  Is there some other API that I am unaware of?</p>

<p>If you see the <a href="https://github.com/kubernetes-client/python-base/blob/master/config/kube_config.py" rel="noreferrer">source code</a> of the <em>kube_config</em> module you can use different arguments with the method <em>load_kube_config</em> to select your cluster:</p>

<blockquote>
<pre><code>def load_kube_config(config_file=None, context=None,
                     client_configuration=None,
                     persist_config=True):
    """Loads authentication and cluster information from kube-config file
    and stores them in kubernetes.client.configuration.
    :param config_file: Name of the kube-config file.
    :param context: set the active context. If is set to None, current_context
        from config file will be used.
    :param client_configuration: The kubernetes.client.Configuration to
        set configs to.
    :param persist_config: If True, config file will be updated when changed
        (e.g GCP token refresh).
    """
</code></pre>
</blockquote>

<p>If I understood the code correctly, you can do somewhat like the following:</p>

<pre><code>from kubernetes import client, config
for file in files:
    config.load_kube_config(config_file=file)
    v1 = client.CoreV1Api()
    response = v1.list_namespace()
    print(response)
</code></pre>

<p>EDIT: <a href="https://github.com/kubernetes-client/python/blob/e057f273069de445a2d5a250ac5fe37d79671f3b/examples/example4.py" rel="noreferrer">This</a> is an example that uses the context argument <strong>with a single kubeconfig file</strong> to iterate over multiple clusters. In the kubernetes docs there is an entry on <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files" rel="noreferrer">Merging kubeconfig files</a>. Basically after having a config file with multiple contexts you can load the file with <code>config.load_kube_config(config_file=file)</code> and load contexts with <code>client.load_kube_config(context="context2')</code></p>

<p>P.S. You don't need to use <em>config.load_kube_config()</em> if you want to use a config file in the default path (<em>'~/.kube/config'</em>) or if you set a path in the KUBECONFIG environment variable.</p>
