The configmap format of kubernetes is wrong

1. Problems       

        Recently, it was found that the format of the configmap resource was wrong when viewing (-o yaml) or modifying (edit). Take the nginx configuration file as an example, create cm through <kubectl create cm nginx.cong>, as follows:

 Two, the reason

        It may be because the configuration file (such as nginx.conf) contains tabs or trailing spaces. Kubernetes ConfigMapIn loadthe process, it is found that the file contains tabindentation, which is directly converted to\n\t。

        We need to format the configuration file before creating cm

# 使用以下方法删除尾随空格
sed -i -E 's/[[:space:]]+$//g' file.txt
# 使用空格替换制表符
sed -i 's/\t/    /g' file.txt

        Then the creation of cm will display normally.

3. View

        If cm has been created, you only need to check the original format, which can be realized through jq

# kubectl get cm -n my-namespace my-cm -o json | jq '.data."nginx.conf"' -r

#  将my-cm这个configmap资源以json格式化输出,格式化范围是/data/nginx.conf内容

4. Modify

1. Direct modification (not yet verified)

# 下载yq工具
wget https://github.com/mikefarah/yq/releases/download/v4.16.2/yq_linux_amd64 \
  && chmod +x yq_linux_amd64 \
  && mv yq_linux_amd64 /usr/local/bin/yq

# kubectl edit cm my-cm | yq '.' -

# 将 ConfigMap 的内容输出为 YAML 格式,并通过 `yq` 工具进行格式化和修改。其中,`.` 表示当前节点,`-` 表示从标准输入读取数据,然后可以对其进行修改和调整,最后保存退出即可。
  注意:在编辑时需要按照正确的 YAML 语法进行操作,否则可能会导致格式错误等问题。

2. Regenerate cm

1. Re-create configmap after formatting the configuration file; (the original configuration file can be found)

2. jq 'xxx' -r re-create configmap after obtaining the current configuration file; (take it directly from the current cm)

Guess you like

Origin blog.csdn.net/weixin_39855998/article/details/129428109