使用“> gt; quot”将撰写给文件的区别是什么。 VS使用lineFeed字符“ \ n \ n”在将文本加载到Kubernetes Secret时,在字符串中?

发布于 2025-01-26 15:54:23 字数 1437 浏览 2 评论 0原文

我已经观察到kubectl在使用- From-Literal选项时,将附加\插入到LineFeed字符上。从文件加载“相同”内容时,它可以按预期工作。显然,必须有区别,因为Stdout看起来有所不同,但我看不到原因。

echo "api_key" >> .tmp
echo "api_value" >> .tmp
cat -e .tmp
kubectl delete secrets api-env
kubectl create secret generic api-env --from-file=.api=.tmp
rm .tmp
kubectl get secret api-env -o json | jq '.data | map_values(@base64d)'

#prints:
#api_key$
#api_value$
#secret "api-env" deleted
#secret/api-env created
#{
#  ".api": "api_key\napi_value\n"
#}

上面的命令在每行上创建一个单线馈示字符。由CAT -E演示,文件中有两个线馈字符,每个字符在末尾。

使用字符串进行相同的操作结果,在要逃脱的\ n中。

api="api_key\napi_value\n"
echo $api
kubectl delete secrets api-env
kubectl create secret generic api-env --from-literal=.api=$api
kubectl get secret api-env -o json | jq '.data | map_values(@base64d)'

#prints:
#api_key\napi_value\n
#secret "api-env" deleted
#secret/api-env created
#{
#  ".api": "api_key\\napi_value\\n"
#}

echo命令显示了该字符串,因为将其提供给了变量,但是将其加载到Kubernetes后\ n被逃脱,并且内容被认为是一行。

这很重要,因为在我使用kubectl的几种情况下,我不允许写入本地文件系统。

这里发生了什么,以及如何阻止Kubernetes逃脱\ n字符?

环境:

  • ZSH 5.8(X86_64- -Apple-darwin21.0)
  • Darwin内核版本21.4.0:root:XNU-8020.101.4〜15/Release_x86_64 X86_64 X86_64
  • 客户端
  • kubectl :v1.25.2

I have observed that kubectl inserts an additional \ to linefeed characters when using the --from-literal option. It works as expected when loading "the same" content from a file. Clearly, there must be a difference because the stdout looks different but I fail to see why.

echo "api_key" >> .tmp
echo "api_value" >> .tmp
cat -e .tmp
kubectl delete secrets api-env
kubectl create secret generic api-env --from-file=.api=.tmp
rm .tmp
kubectl get secret api-env -o json | jq '.data | map_values(@base64d)'

#prints:
#api_key$
#api_value$
#secret "api-env" deleted
#secret/api-env created
#{
#  ".api": "api_key\napi_value\n"
#}

The commands above create a single linefeed character on each line. Demonstrated by cat -e there are two linefeed characters in the file, each at the end.

Doing the same using a string results in the \n to be escaped.

api="api_key\napi_value\n"
echo $api
kubectl delete secrets api-env
kubectl create secret generic api-env --from-literal=.api=$api
kubectl get secret api-env -o json | jq '.data | map_values(@base64d)'

#prints:
#api_key\napi_value\n
#secret "api-env" deleted
#secret/api-env created
#{
#  ".api": "api_key\\napi_value\\n"
#}

The echo command shows the string as it was supplied to the variable, however after loading that to kubernetes the \n are escaped and the content is considered to be a single line.

This is important because in several instances where I am working with kubectl I am not allowed to write to the local file system.

What is happening here and how to stop kubernetes from escaping the \n character?

Environment:

  • zsh 5.8 (x86_64-apple-darwin21.0)
  • Darwin Kernel Version 21.4.0: root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
  • kubectl Client:"v1.20.10"
  • kubectl Server: "v1.23.3"
  • minikube version: v1.25.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-02-02 15:54:23

当您使用echo $ api时,echo本身会更改内容:启用XSI扩展名的posix-Commiant shell(并且ZSH并不符合POSIX compliant,它, die 实现此方面),\ n s被字面的新线代替。

- from-literal = .api = $ api;在那里,您的\ n S仍然是两个字符的序列,首先是反斜击,然后是n

鉴于您正在使用$'\ n'来支持直接表示newline字面的方法,请考虑- from-literal = .api =“ api_key” $ '\ n'“ api_value”

When you use echo $api, echo itself changes the contents: On POSIX-compliant shells with XSI extensions enabled (and while zsh isn't POSIX-compliant in general, it does implement this aspect), the \ns are replaced with literal newlines.

That doesn't happen with --from-literal=.api=$api; there, your \ns are still two-character sequences, first a backslash, then a n.

Given that you're on a shell that supports using $'\n' as a way to represent a newline literal directly, consider --from-literal=.api="api_key"$'\n'"api_value"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文