<p><strong>THE PLOT:</strong></p>
<p>I am working on a kubernetes environment where we have PROD and ITG setup. The ITG setup has multi-cluster environment whereas PROD setup is a single-cluster environment.
I am trying to automate some process using Python where I have to deal with kubeconfig file and I am using the <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">kubernetes</a> library for it.</p>
<p><strong>THE PROBLEM:</strong></p>
<p>The kubeconfig file for PROD has &quot;current-context&quot; key available but the same is <strong>missing</strong> from the kubeconfig file for ITG.</p>
<p><em>prdconfig</em>:</p>
<pre><code>apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://cluster3.url.com:3600
  name: cluster-ABC
contexts:
- context:
    cluster: cluster-LMN
    user: cluster-user
  name: cluster-LMN-context
current-context: cluster-LMN-context
kind: Config
preferences: {}
users:
- name: cluster-user
  user:
    exec:
      command: kubectl
      apiVersion: &lt;clientauth/version&gt;
      args:
      - kubectl-custom-plugin
      - authenticate
      - https://cluster.url.com:8080
      - --user=user
      - --token=/api/v2/session/xxxx
      - --token-expiry=1000000000
      - --force-reauth=false
      - --insecure-skip-tls-verify=true
</code></pre>
<p><em>itgconfig</em>:</p>
<pre><code>apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://cluster1.url.com:3600
  name: cluster-ABC
- cluster:
    insecure-skip-tls-verify: true
    server: https://cluster2.url.com:3601
  name: cluster-XYZ
contexts:
- context:
    cluster: cluster-ABC
    user: cluster-user
  name: cluster-ABC-context
- context:
    cluster: cluster-XYZ
    user: cluster-user
  name: cluster-XYZ-context
kind: Config
preferences: {}
users:
- name: cluster-user
  user:
    exec:
      command: kubectl
      apiVersion: &lt;clientauth/version&gt;
      args:
      - kubectl-custom-plugin
      - authenticate
      - https://cluster.url.com:8080
      - --user=user
      - --token=/api/v2/session/xxxx
      - --token-expiry=1000000000
      - --force-reauth=false
      - --insecure-skip-tls-verify=true
</code></pre>
<p>When I try loading the kubeconfig file for PROD using <code>config.load_kube_config(os.path.expanduser('~/.kube/prdconfig'))</code> it works.</p>
<p>And when I try loading the kubeconfig file for ITG using <code>config.load_kube_config(os.path.expanduser('~/.kube/itgconfig'))</code>, I get the following error:</p>
<blockquote>
<p>ConfigException: Invalid kube-config file. Expected key
current-context in C:\Users&lt;username&gt;/.kube/itgconfig</p>
</blockquote>
<p>Although it is very clear from the error message that it is considering the kubeconfig file as <strong>invalid</strong>, as it does not have &quot;current-context&quot; key in it.</p>
<p><strong>THE SUB-PLOT:</strong></p>
<p>When working with kubectl, the missing &quot;current-context&quot; does not make any difference as we can always specify context along with the command. But the 'load_kube_config()' function makes it mandatory to have &quot;current-context&quot; available.</p>
<p><strong>THE QUESTION:</strong></p>
<p>So, is &quot;current-context&quot; a mandatory key in kubeconfig file?</p>
<p><em><strong>THE DISCLAIMER:</strong></em></p>
<p>I am very new to kubernetes and have very little experience working with it.</p>

<p>As described in the comments:
If we want to use <code>kubeconfig</code> file to work out of the box by default, with specific cluster using kubectl or python script we can mark one of the contexts in our <code>kubeconfig</code> file as the default by specifying <code>current-context</code>.</p>
<p>Note about <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#context" rel="nofollow noreferrer">Context</a>:</p>
<blockquote>
<p>A <code>context</code> element in a kubeconfig file <code>is used to group access parameters</code> under a convenient name. Each context has three parameters: cluster, namespace, and user. <code>By default, the kubectl command-line tool uses parameters from the current context to communicate with the cluster</code>.</p>
</blockquote>
<p>In order to mark one of our contexts (f.e. dev-fronted) in our kubeconfig file as the default one please run:</p>
<pre><code>kubectl config use-context dev-fronted
</code></pre>
<blockquote>
<p>Now whenever you run a kubectl command, the action will apply to the cluster, and namespace listed in the dev-frontend context. And the command will use the credentials of the user listed in the dev-frontend context</p>
</blockquote>
<p>Please take a look at:</p>
<p><strong>-</strong> <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#merging-kubeconfig-files" rel="nofollow noreferrer">Mering kubeconfig files</a>:</p>
<blockquote>
<ol start="2">
<li><p>determine the context to use based on the first hit in this chain:</p>
<p>Use the --context command-line flag if it exists.
Use the current-context from the merged kubeconfig files.</p>
</li>
</ol>
<p><strong>An empty context is allowed at this point</strong>.</p>
<ol start="3">
<li><p>determine the cluster and user. At this point, there might or might not be a context. Determine the cluster and user based on the first hit in this chain, which is run twice: once for user and once for cluster:</p>
<p>Use a command-line flag if it exists: --user or --cluster.
If the context is non-empty, take the user or cluster from the context.</p>
</li>
</ol>
<p>The user and cluster can be empty at this point.</p>
</blockquote>
<p>Whenever we run <code>kubectl</code> commands without specified <code>current-context</code> we should provide additional configuration parameters to tell kubectl which configuration to use, in your example it could be f.e.:</p>
<pre><code>kubectl --kubeconfig=/your_directory/itgconfig get pods --context cluster-ABC-context
</code></pre>
<p>As described earlier - to simplify this task we can use configure <code>current-context</code> in <code>kubeconfig</code> file configuration:</p>
<pre><code>kubectl config --kubeconfig=c/your_directory/itgconfig use-context cluster-ABC-context
</code></pre>
<p>Going further into errors generated by your script we should notice errors from <a href="https://github.com/kubernetes-client/python-base/blob/6b0104ffb9dd2f96d47d075ea3d30f69ea124ce4/config/kube_config.py#L255" rel="nofollow noreferrer">config/kube_config.py</a>:</p>
<pre><code>config/kube_config.py&quot;, line 257, in set_active_context context_name = self._config['current-context']

kubernetes.config.config_exception.ConfigException:: Invalid kube-config file. Expected key current-context in ...
</code></pre>
<p>Here is an example with additional <code>context=&quot;cluster-ABC-context&quot;</code> parameter:</p>
<pre><code>from kubernetes import client, config

config.load_kube_config(config_file='/example/data/merged/itgconfig', context=&quot;cluster-ABC-context&quot;)

v1 = client.CoreV1Api()
print(&quot;Listing pods with their IPs:&quot;)
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print(&quot;%s\t%s\t%s&quot; % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

...
Listing pods with their IPs:
10.200.xxx.xxx  kube-system coredns-558bd4d5db-qpzb8
192.168.xxx.xxx kube-system etcd-debian-test
...
</code></pre>
<p>Additional information</p>
<ul>
<li><a href="https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" rel="nofollow noreferrer">Configure Access to Multiple Clusters</a></li>
<li><a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/" rel="nofollow noreferrer">Organizing Cluster Access Using kubeconfig Files</a></li>
</ul>
