这是我的yaml文件
apiVersion: myapi.com/v1
kind: Template
metadata:
name: {{ .Values.tpl.organization }}-template-mkytemplatetemplate
spec:
# Add fields here
organization: {{ .Values.tpl.organization }}
purpose: {{ .Values.tpl.purpose }}
version: {{ .Chart.appVersion }}
location: {{.Values.location}}/template.tgz
name: mkytemplatetemplate
namePattern: ^[a-z0-9\-]{3,25}$
description: "# Please do not use the template\ntest modify run.sh"
author: string我使用gomplate将所有{{xxx}}替换为Chart.yaml或Values.yaml中的相应值。以下是我的代码
func main() {
log.Println("hello")
//BasicTemplate()
Gomplate()
}
func Gomplate() {
workspace := "/Users/i517131/code/mkytemplatetemplate/.ci/chart"
inBs, _ := ioutil.ReadFile(path.Join(workspace, "templates/template.yaml"))
in := string(inBs)
cfg := &gomplate.Config{
Input: in,
OutputFiles: []string{path.Join(workspace, "result.yaml")},
DataSources: []string{
fmt.Sprintf("%s=%s", "Chart", path.Join(workspace, "Chart.yaml")),
fmt.Sprintf("%s=%s", "Values", path.Join(workspace, "values.yaml")),
},
}
err := gomplate.RunTemplates(cfg)
if err != nil {
panic(err)
}
}但是我收到一个类似于此panic: template: <arg>:4:18: executing "<arg>" at <.Values.tpl.organization>: map has no entry for key "Values"的错误。在err := gomplate.RunTemplates(cfg)
首先,当我运行cmd gomplate -f .ci/chart/templates/template.yaml -d Chart=.ci/chart/Chart.yaml -d Values=.ci/chart/values.yaml -o result.yaml时,我收到保存错误。我在网上搜索,在github上,作者建议我们使用-c而不是-d
但是go中的模板只能使用gomplate.Config来运行模板,不支持-c。我能做什么?
-c命令生成的结果
apiVersion: myapi.com/v1
kind: Template
metadata:
name: mky-template-mkytemplatetemplate
spec:
# Add fields here
organization: mky
purpose: prod
version: 1.0.0
location: _/template.tgz
name: mkytemplatetemplate
namePattern: ^[a-z0-9\-]{3,25}$
description: "# Please do not use the template\ntest modify run.sh"
author: string发布于 2021-08-04 03:29:51
使用Contexts而不是DataSource
cfg := &gomplate.Config{
Input: in,
OutputFiles: []string{path.Join(workspace, "result.yaml")},
Contexts: []string{
fmt.Sprintf("%s=%s", "Chart", path.Join(workspace, "Chart.yaml")),
fmt.Sprintf("%s=%s", "Values", path.Join(workspace, "values.yaml")),
},
}https://stackoverflow.com/questions/68635386
复制相似问题