How to update YAML/conf files in K8s ConfigMap that contain `\n` and have garbled formatting

This may be caused by extra line breaks such as `\n` at the end of a certain line in the content, leading to abnormal recognition by K8s. You can export the corresponding file, modify it manually, and then patch it back.

Back up the current ConfigMap
kubectl -n xxx get configmap xxx -o yaml > xxx.backup.yaml

Export bws.conf
kubectl -n xxx get configmap xxx \
-o jsonpath='{.data.bws\.conf}’ > bws.conf

vi bws.conf

Generate a patch from the modified file, automatically handling newlines and quotes
jq -n –rawfile conf bws.conf ‘{“data”:{“bws.conf”:$conf}}’ > bws.patch.json

Apply the patch to the ConfigMap
kubectl -n xxx patch configmap xxx \
–type=merge –patch-file=bws.patch.json

Restart pods for changes to take effect
kubectl -n xxx delete pod -l app=xxx

Leave a Comment