使用Helm Go SDK安装图表时如何传递YAML文件?

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

How to pass YAML files while installing charts using Helm Go SDK

问题

我们正在使用Go的helm.sh/helm/v3/pkg/action包来安装Helm charts。
我们可以通过map[string]interface{}将安装值传递给Helm chart的Run函数,例如:https://stackoverflow.com/questions/45692719/samples-on-kubernetes-helm-golang-client

rel, err := client.Run(myChart, vals)
if err != nil {
    log.Println(err)
    panic(err)
}

但是我们也有一个values.yaml文件,在从命令行界面安装charts时,会通过-f values.yaml进行传递。
在安装过程中(client.Run()),是否有办法通过action Go包传递这些values.yaml文件?

或者我们需要将YAML文件解析并将其作为map传递:

data2 := make(map[string]interface{})
yfile2, err := ioutil.ReadFile("./utils/values.yaml")

fmt.Println(err)
err = yaml.Unmarshal(yfile2, &data2)
英文:

We are using the Go helm.sh/helm/v3/pkg/action package to install Helm charts.
We are able to pass installation values to Helm chart Run functions via map[string]interface{}, as in <https://stackoverflow.com/questions/45692719/samples-on-kubernetes-helm-golang-client>:

<!-- language: go -->

rel, err := client.Run(myChart, vals)
if err != nil {
	log.Println(err)
	panic(err)
}

But we have a values.yaml file also which was passed as -f values.yaml when installing charts from the CLI.
Is there a way to pass these values.yaml files via the action Go package during installation (client.Run())?

Or do we need to unmarshal the YAML file and pass that also as map:

<!-- language: go -->

data2 := make(map[string]interface{})
yfile2, err := ioutil.ReadFile(&quot;./utils/values.yaml&quot;)

fmt.Println(err)
err = yaml.Unmarshal(yfile2, &amp;data2)

答案1

得分: 2

一个直接的做法是重用helm.sh/helm/v3/pkg/cli/values包。该包具有处理-f选项(以及--set及其变体)并返回统一的map[string]interface{}的逻辑。代码示例如下:

import (
        "helm.sh/helm/v3/pkg/cli"
        "helm.sh/helm/v3/pkg/cli/values"
        "helm.sh/helm/v3/pkg/getter"
)

envSettings := cli.New()
providers := getter.All(envSettings)
options := values.Options{
        ValueFiles: []string{"./utils/values.yaml"},
}
theValues := options.MergeValues(providers)

现在,theValues是从这些文件中读取的map[string]interface{}。如果需要,您可以进一步自定义值(作为Go数据),然后将其传递给install.Run()

英文:

One straightforward thing to do could be to reuse the helm.sh/helm/v3/pkg/cli/values package. This has the logic to handle the -f option (and also --set and its variants) and return a unified map[string]interface{}. This could look like:

import (
        &quot;helm.sh/helm/v3/pkg/cli&quot;
        &quot;helm.sh/helm/v3/pkg/cli/values&quot;
        &quot;helm.sh/helm/v3/pkg/getter&quot;
)

envSettings := cli.New()
providers := getter.All(envSettings)
options := values.Options{
        ValueFiles: []string{&quot;./utils/values.yaml&quot;},
}
theValues := options.MergeValues(providers)

Now theValues is the map[string]interface{} that results from reading those files. You can customize the values further if required (as Go data) and then pass that to install.Run().

huangapple
  • 本文由 发表于 2022年8月12日 14:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/73330116.html
匿名

发表评论

匿名网友

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

确定