How do you change the name
of a user in a kube config file with kubectl (no text editor)?
Example kube config file stage_config.yaml
:
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: [email protected]
current-context: [email protected]
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
I want to do something like kubectl config rename-user --kubeconfig ~/.kube/stage_config.yaml kubernetes-admin kubernetes-admin-1
With the output like:
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: [email protected]
current-context: [email protected]
kind: Config
preferences: {}
users:
- name: kubernetes-admin-1
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
I've tried kubectl config set
but receive the following error:
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]
kubectl config --help
shows that the rename-context
command exists, but nothing like rename-user
nor rename-cluster
exists.
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 CLUSTER_NAME
to your desired value.
jq '.clusters[0].name="'$CLUSTER_NAME'" | .contexts[0].context.cluster="'$CLUSTER_NAME'"' source/kubeconfig > target/kubeconfig