英文:
gomplate: executing "<arg>" at <.Values.tpl.organization>: map has no entry for key "Values"
问题
这是我的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)
处。
起初,当我运行命令 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只能使用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
英文:
Here is my yaml file
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
I use gomplate to replace all {{xxx}} to corresponding values in Chart.yaml or Values.yaml. Here is my code
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)
}
}
but I receive an error like this panic: template: <arg>:4:18: executing "<arg>" at <.Values.tpl.organization>: map has no entry for key "Values"
. at err := gomplate.RunTemplates(cfg)
At first, when I run cmd gomplate -f .ci/chart/templates/template.yaml -d Chart=.ci/chart/Chart.yaml -d Values=.ci/chart/values.yaml -o result.yaml
, I receive the save error.
I searched the internet and in github, the author suggest us use -c
instead of -d
But the gomplate in go can only use gomplate.Config to run templates and do not support -c
. What can I do?
result generated by -c cmd
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
答案1
得分: 1
使用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")),
},
}
英文:
use Contexts
not 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")),
},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论