英文:
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("./utils/values.yaml")
fmt.Println(err)
err = yaml.Unmarshal(yfile2, &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 (
"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)
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()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论