使用“> gt; quot”将撰写给文件的区别是什么。 VS使用lineFeed字符“ \ n \ n”在将文本加载到Kubernetes Secret时,在字符串中?
我已经观察到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
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\n
s are replaced with literal newlines.That doesn't happen with
--from-literal=.api=$api
; there, your\n
s are still two-character sequences, first a backslash, then an
.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"