kubernetes configmap 打印 而不是换行符 [英] kubernetes configmap prints instead of a newline

查看:112
本文介绍了kubernetes configmap 打印 而不是换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 configmap 部署到集群上

- 名称:制作/更新集群上的所有配置映射Kubernetes:api_endpoint:废话url_username:等等url_password:废话内联数据:api版本:v1种类:ConfigMap元数据:名称:布拉命名空间:blah数据:my-data.txt:{{数据}}"状态:现在数据:|一些= foofoo = 一些(使用大三角帆将其连接到吊舱)

当我进入 pod 并打开 my-data.txt 时,它会显示:

some = foo
 foo = some

我希望它看起来完全像文本并打印换行符而不是

如果我在文本中的某处放置 ' ' 单引号,它会按原样打印文本,但使用单引号:

数据:|一些= foofoo = 一些''

打印完全一样.

我试图研究,但我找不到任何东西,我已经坚持了一段时间.

解决方案

这个好像和类似kubernetes/kubernetes 问题 36222 从文件创建 configMap 时.

在您的情况下,从 data 块创建时会发生这种情况.

最近的kubernetes/kubernetes issue 63503 引用了所有印刷版.

评论提及:

<块引用>

我在 configMap 中添加了一个新行,使用 Tab 进行标识.更改为 Spaces 而不是 Tab 后,我能够按预期看到 configmap...

8 月 202 日:issue 36222 现在包括:

<块引用>

如果您只想要创建时读取的原始输出 --from-file,您可以使用 jq 获取原始字符串(没有转义的换行符等)

如果您从这样的文件创建了配置映射:

kubectl create configmap myconfigmap --from-file mydata.txt

获取数据:

kubectl get cm myconfigmap -o json |jq '.data."mydata.txt"""'-r

还有:

<块引用>

如果 cm 的格式变得奇怪,一个简单的 hack 可以让它恢复正常:

kubectl get cm configmap_name -o yaml >cm.yaml

现在复制 cm.yaml 文件的内容并将其粘贴到 yamllint.com.Yamllint.com 是检查 yaml 文件 linting 的强大工具.
这将为您提供正确格式的配置映射.

将输出粘贴到另一个 yaml 文件中(例如 - cm_ready.yaml)

 kubectl apply -f cm_ready.yaml


2020 年 11 月更新,相同问题包括:p><块引用>

我能够通过以下方式解决此问题:

  • 不要使用制表符,转换为空格

  • 要删除换行符前的空格,请使用:

     sed -i -E 's/[[:space:]]+$//g' File.ext

似乎也只会将 CRLF 转换为 LF.

I am trying to deploy a configmap onto a cluster

- name: Make/Update all configmaps on the cluster
 kubernetes:
 api_endpoint: blah
 url_username: blah
 url_password: blah
 inline_data:
 apiVersion: v1
 kind: ConfigMap
 metadata: 
 name: blah
namespace: blah
 data: my-data.txt: "{{ data }}"
 state: present
data: |
 some = foo
 foo = some
(using spinnaker to attach it to pods)

When I go into the pod and open my-data.txt it displays:

some = foo
 foo = some

I want it to look exactly like the text and print newline rather than

Weird thing if I put ' ' single quotes somewhere in the text it prints the text as is but with the single quotes so :

data: |
 some = foo
 foo = some
' '

prints exactly the same.

I have tried to research but I couldn't find anything and I have been stuck on this for a while now.

解决方案

This seems to be similar to kubernetes/kubernetes issue 36222 when creating configMap from files.

In your case, that happens when created from a data block.

The recent kubernetes/kubernetes issue 63503 references all printed issues.

A comment mentions:

I added a new line in a configMap using Tab for identation. After changing to Spaces instead of Tab, I was able to see the configmap as expected...

August 202: The issue 36222 now includes:

If you just want the raw output as it was read in when created --from-file, you can use jq to get the raw string (without escaped newlines etc)

If you created a configmap from a file like this:

kubectl create configmap myconfigmap --from-file mydata.txt

Get the data:

kubectl get cm myconfigmap -o json | jq '.data."mydata.txt""' -r

Also:

If the formatting of cm goes wierd a simple hack to get it back to normal is :

kubectl get cm configmap_name -o yaml > cm.yaml

Now copy the contents of cm.yaml file and past it on yamllint.com. Yamllint.com is powerful tool to check the linting of yaml files.
This will provide you with the configmap as expected with correct formatting.

Paste the output in another yaml file (for e.g - cm_ready.yaml)

 kubectl apply -f cm_ready.yaml


Update Nov. 2020, the same issue includes:

I was able to fix this behavior by:

  • Don't use tabs, convert to spaces

  • To remove spaces before a newline character, use this:

      sed -i -E 's/[[:space:]]+$//g' File.ext
    

It seems also will convert CRLF to LF only.

这篇关于kubernetes configmap 打印 而不是换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆