警告
本文最后更新于 2023-11-08 17:06,文中内容可能已过时。
问题描述
在K8s中,ConfigMap可能会因为配置中存在制表符和行尾有空格导致格式混乱,这会影响配置的可读性和维护性。
原来的解决方法是使用以下命令:
1
2
3
4
5
| k get cm nginx-config -o jsonpath='{.data.nginx\.conf}' > nginx.conf
sed -i -E -e 's/[[:space:]]+$//g' -e 's/\t/ /g' nginx.conf
k create cm nginx-config --from-file nginx.conf --dry-run=client -o yaml | kubectl replace -f -
|
kubectl-strip-cm 插件
为了简化这个过程,我开发了一个kubectl插件kubectl-strip-cm
,可以一键净化ConfigMap中的内容。
安装方法
1
2
3
| curl -LO https://github.com/SoulChildTc/kubectl-strip-cm/raw/main/kubectl-strip_cm
chmod +x ./kubectl-strip_cm
sudo mv ./kubectl-strip_cm /usr/local/bin/kubectl-strip_cm
|
使用方法
净化指定的ConfigMap:
1
| kubectl strip-cm -n kube-system filebeat-config
|
使用-f
选项忽略提示:
1
| kubectl strip-cm -n kube-system filebeat-config -f
|
项目地址
项目已开源在GitHub: https://github.com/SoulChildTc/kubectl-strip-cm