<p>How do you change the <code>name</code> of a user in a kube config file with kubectl (no text editor)?</p>
<p>Example kube config file <code>stage_config.yaml</code>:</p>
<pre><code>apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: REDACTED
    server: https://127.0.0.1:6443
  name: cluster.local
contexts:
- context:
    cluster: cluster.local
    user: kubernetes-admin
  name: kubernetes-admin@cluster.local
current-context: kubernetes-admin@cluster.local
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

</code></pre>
<p>I want to do something like <code>kubectl config rename-user --kubeconfig ~/.kube/stage_config.yaml kubernetes-admin kubernetes-admin-1</code></p>
<p>With the output like:</p>
<pre><code>apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: REDACTED
    server: https://127.0.0.1:6443
  name: cluster.local
contexts:
- context:
    cluster: cluster.local
    user: kubernetes-admin
  name: kubernetes-admin@cluster.local
current-context: kubernetes-admin@cluster.local
kind: Config
preferences: {}
users:
- name: kubernetes-admin-1
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
</code></pre>
<p>I've tried <code>kubectl config set</code> but receive the following error:</p>
<pre><code>kubectl config set --kubeconfig ~/.kube/stage_config.yaml users.name.kubernetes-admin kubernetes-admin-1
error: can't set a map to a value: map[kubernetes-admin:0xc000c53100]
</code></pre>
<p><code>kubectl config --help</code> shows that the <code>rename-context</code> command exists, but nothing like <code>rename-user</code> nor <code>rename-cluster</code> exists.</p>

SlyGuy
<p>Use JQ to do the magic (just make sure your KubeConfig is in JSON).
I'm sure is possible to do it with YQ for YAML but I let someone else fill that one in. Set <code>CLUSTER_NAME</code> to your desired value.</p>
<pre class="lang-bash prettyprint-override"><code>jq '.clusters[0].name=&quot;'$CLUSTER_NAME'&quot; | .contexts[0].context.cluster=&quot;'$CLUSTER_NAME'&quot;' source/kubeconfig &gt; target/kubeconfig
</code></pre>
