configmap中json或者yaml文件内容格式问题

configmapjson或者yaml文件内容格式问题

一、问题现象说明:

比如我有一个yaml格式的配置文件config.yaml,内容如下:

service:
  port: 8001
  # release
  mode: release
  url: kube-prometheus.monitoring
  port: 9090
common:
  file:
    upload: /opt/data/common/file/

把该配置文件使用configmap方式创建:

kubectl create configmap myconfig --from-file=config.yaml

我们希望在命令行终端查看到的内容如下

[root@node1 ~]# kubectl get configmaps myconfig -o yaml
apiVersion: v1
data:
  config.yaml: |
    service:
      port: 8001
      # release
      mode: release
      url: kube-prometheus.monitoring
      port: 9090
    common:
      file:
        upload: /opt/data/common/file/
kind: ConfigMap
metadata:
  creationTimestamp: 2019-04-24T05:59:52Z
  name: myconfig
  namespace: default
  resourceVersion: "1774515"
  selfLink: /api/v1/namespaces/default/configmaps/myconfig
  uid: 2d066fef-6656-11e9-96e0-005056bf291a
[root@node1 ~]# 

但是有时候我们看到如下格式错乱的configmap内容:

[root@intellif-0 ~]# kubectl get configmaps myconfig -o yaml
apiVersion: v1
data:
  config.yaml: "service:
  port: 8001
  # release
  mode: release
  url: kube-prometheus.monitoring

     port: 9090
common:
  file:
    upload: /opt/data/common/file/  
"
kind: ConfigMap
metadata:
  creationTimestamp: 2019-04-24T06:01:42Z
  name: myconfig
  namespace: default
  resourceVersion: "1774818"
  selfLink: /api/v1/namespaces/default/configmaps/myconfig
  uid: 6eac963e-6656-11e9-96e0-005056bf291a
[root@intellif-0 ~]# 

二、分析与解决

问题原因: 文件中某一行结尾有空格,示例中是最后一行(使用vim工具:set invlist
正常的yaml文件

# vim config.yaml
service:$
  port: 8001$
  # release$
  mode: release$
  url: kube-prometheus.monitoring$
  port: 9090$
common:$
  file:$
    upload: /opt/data/common/file/$                                                                                                                                                                                     
~                                                                                                                                                                                          
~                                                                                                                                                                                                                                                                                                                                                                                
~                                                                                                                                                                                          
~                                                                                                                                                                                          
~                                                                                                                                                                                          
~                                                                                                                                                                                          
:set invlist 

异常的yaml文件

# vim config.yaml
service:$
  port: 8001$
  # release$
  mode: release$
  url: kube-prometheus.monitoring$
  port: 9090$
common:$
  file:$
    upload: /opt/data/common/file/  $                                                                                                                                                                                     
~                                                                                                                                                                                          
~                                                                                                                                                                                                                                                                                                                                                                                
~                                                                                                                                                                                          
~                                                                                                                                                                                          
~                                                                                                                                                                                          
~                                                                                                                                                                                          
:set invlist

猜你喜欢

转载自blog.csdn.net/m0_67390969/article/details/124505272