'How do I remove a specific block from the value of a config map?
I have a config map that looks like this:
kubectl describe configmap example-config --namespace kube-system
Name: example-config
Namespace: kube-system
Labels: <none>
Annotations: <none>
Data
====
mapRoles:
----
- rolearn: arn:aws:iam::0123456789012:role/user-role-1
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
- rolearn: arn:aws:iam::0123456789012:role/user-role-2
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
I want to remove user-role-2 from the configmap. I think I need to use kubectl patch with a "remove" operation. What is the syntax to remove a section from a config map?
Here is an example command I can use to append to the config map:
kubectl patch -n=kube-system cm/aws-auth --patch "{\"data\":{\"mapRoles\": \"- rolearn: "arn:aws:iam::0123456789012:role/user-role-3" \n username: system:node:{{EC2PrivateDNSName}}\n groups:\n - system:bootstrappers\n - system:nodes\n\"}}"
Solution 1:[1]
According to kubernetes official docs: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/
There is not such syntax in kubectl patch to remove a section from an api object like a config map.
Here is an example command I can use to append to the config map:
kubectl patch -n=kube-system cm/aws-auth --patch "{\"data\":{\"mapRoles\": \"- rolearn: "arn:aws:iam::0123456789012:role/user-role-3" \n username: system:node:{{EC2PrivateDNSName}}\n groups:\n - system:bootstrappers\n - system:nodes\n\"}}"
The command above is used to replace the whole data field in configmap. So you can simply use it to patch update with the data you want:
kubectl patch -n=kube-system cm/example-config --patch '{"data":{"mapRoles": "- rolearn: arn:aws:iam::0123456789012:role/user-role-1\n username: system:node:{{EC2PrivateDNSName}}\n groups:\n - system:bootstrappers\
n - system:nodes"}}'
Solution 2:[2]
I think the simplest way is like this:
kubectl get configmaps foo -o yaml > foo.yaml
Now edit foo.yaml the way you want
Then apply your new config:
kubectl apply -f foo.yaml
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | YwH |
| Solution 2 | guettli |
