如何覆盖 Helm 值的YAML文件?

huangapple go评论68阅读模式
英文:

How to over write helm values yaml file?

问题

在我的值文件中,我有以下内容:

image:
  tag: '123465';

我想要从我的 GitHub CI/CD 中进行覆盖,以便 Argocd 可以获取到它。

我正在编写这个命令:

sed -i 's|image.tag:.*|image.tag: test|g' dev-*.yaml

我得到了以下错误:

sed: 1: "dev.yaml": d 命令末尾多余的字符

我漏掉了什么?

英文:

In my values file I have

image:
  tag: '123465'

I want to overwrite this from my github ci/cd so Argocd can pick it up

I'm writing this command

sed -i 's|image.tag:.*|image.tag: test|g' dev-*.yaml

I'm getting

> sed: 1: "dev.yaml": extra characters at the end of d command

error.

What am I missing?

答案1

得分: 2

sed在这种情况下是否必要? helm支持通过命令行覆盖值,所以你可以这样做:

helm install some-name some-chart --set "image.tag=test"

这将用值test覆盖values.yaml文件中的值。

上述内容在文档中有详细说明。

使用 sed

你分享的错误消息似乎与命令不符,也许弄混了?

假设你有一个名为dev.yaml的文件,其中包含:

image:
  tag: '123456'

你可以使用以下命令将tag值替换为foobar

sed -i 's|^  tag:.*|  tag: "foobar"|' dev.yaml

请注意,sed命令中tag前面的两个空格是有意的。

然而,如果文件中还有另一个tag:,比如:

image:
  tag: "123456"

someOtherImage:
  tag: "baz"

运行上述命令将导致文件中的两个tag:值都被替换。为了解决这个问题,你可以使用分支:

sed -i '/^image:$/ { n; s|^  tag:.*$|  tag: "foobar"|g }' dev.yaml

在这里,/^image:$/匹配包含image:的行,n;指示sed加载紧接着的下一行,而最后的s|^ tag:.*$| tag: "foobar"|g sed命令只对image:后面的行进行标签替换。

上述内容受到这个博客的启发。

英文:

Is it necessary for you to use sed? helm supports overriding values via the command line, so you can do something like:

helm install some-name some-chart --set "image.tag=test"

This will override the value from the values.yaml file with the value test.

Above is detailed out in the documentation.


Using sed

The error message you've shared does not match the command, maybe it got mixed up?

Assuming you have a dev.yaml file with:

image:
  tag: '123456'

You can replace the tag value with foobar using:

sed -i 's|^  tag:.*|  tag: "foobar"|' dev.yaml

Keep in mind that the two spaces before tag within the sed command are intentional.

This however breaks down rather quickly if you have another tag: somewhere in your file, say:

image:
  tag: "123456"

someOtherImage:
  tag: "baz"

Running the above command will yield a file that has both tag: values replaced. To get around this, you can use branching:

sed -i '/^image:$/{ n; s|^  tag:.*$|  tag: "foobar"|g }' dev.yaml

Here, /^image:$/ matches lines that contain exactly image:, n; directs sed to load the immediate next line, while the final s|^ tag:.*$| tag: "foobar"|g sed command does the tag replacement, only for the line after image:.

Above is inspired from this blog.

huangapple
  • 本文由 发表于 2023年3月1日 15:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定