我有一个k8s yaml文件,其下面的块
spec:
replicas: 1
strategy:
type: Recreate
我想在“ spec:”之后添加以下代码块:
selector:
matchLabels:
app: test-app
该文件很大,并且有许多“ spec:”字段,因此应在第一次匹配时添加。
最终文件内容应如下所示:
spec:
selector:
matchLabels:
app: test-app
replicas: 1
strategy:
type: Recreate
我想出了使用带有正确缩进的yq的工作解决方案,但它附加在文件末尾,维护和读取类似100的文件非常麻烦。
yq -i -y '.spec += {selector:{matchLabels:{app:"test-app"}}}' filename.yaml
欢迎使用sed或awk之类的工具进行解答。
我不熟悉yq
,但我知道它支持有限JSON I / O。这是解决结构性问题的方法jq
:
.spec |= ({selector: {matchLabels: {app: "test-app"}}} + .)
也许值得一试yq
?
样本管道(未试用):
yq r -j k8s.yaml | jq "$script" | yq r --prettyPrint
Jeff Mercado还提供了这些jq yamlifiers。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句