Updating a ConfigMap
If you have created a Kubernetes Secret or ConfigMap with
kubectl create secret|configmap, you may have expected there to be a similar Secret/ConfigMap helper command under
kubectl apply. If so, you would have been wrong. Fortunately, there is a workaround. The trick is to use the dry-run feature of
kubectl and then pipe the output of that to
kubectl apply. Using this trick to create and/or update a ConfigMap:
kubectl create configmap my-config --from-literal=foo=bar --dry-run=client -o yaml | kubectl apply -f -
It is best to create your Secrets and ConfigMaps using the above approach so
kubectl can record its annotation for tracking changes to the resource in the spec. You can also achieve this using the
--save-config command-line option when running
kubectl create secret|configmap.
When updating Secrets and ConfigMaps, note that since
kubectl apply keeps track of deletions, you will need to specify all key/value pairs you want in the Secret or ConfigMap each time you run the command.
If you are running kubectl version 1.17.0 or older, replace –dry-run=client with –dry-run. Starting in version 1.18, both client- and service-side dry runs are supported.