gomplate: 在<.Values.tpl.organization>处执行”<arg>”:map没有”Values”键的条目

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

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: &quot;# Please do not use the template\ntest modify run.sh&quot;
  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: &quot;# Please do not use the template\ntest modify run.sh&quot;
  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: &quot;# Please do not use the template\ntest modify run.sh&quot;
  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(&quot;hello&quot;)
	//BasicTemplate()
	Gomplate()
}

func Gomplate() {
	workspace := &quot;/Users/i517131/code/mkytemplatetemplate/.ci/chart&quot;
	inBs, _ := ioutil.ReadFile(path.Join(workspace, &quot;templates/template.yaml&quot;))
	in := string(inBs)
	cfg := &amp;gomplate.Config{
		Input: in,
		OutputFiles: []string{path.Join(workspace, &quot;result.yaml&quot;)},
		DataSources: []string{
			fmt.Sprintf(&quot;%s=%s&quot;, &quot;Chart&quot;, path.Join(workspace, &quot;Chart.yaml&quot;)),
			fmt.Sprintf(&quot;%s=%s&quot;, &quot;Values&quot;, path.Join(workspace, &quot;values.yaml&quot;)),
		},
	}
	err := gomplate.RunTemplates(cfg)
	if err != nil {
		panic(err)
	}
}

but I receive an error like this panic: template: &lt;arg&gt;:4:18: executing &quot;&lt;arg&gt;&quot; at &lt;.Values.tpl.organization&gt;: map has no entry for key &quot;Values&quot;. 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: &quot;# Please do not use the template\ntest modify run.sh&quot;
  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 := &amp;gomplate.Config{
		Input: in,
		OutputFiles: []string{path.Join(workspace, &quot;result.yaml&quot;)},
		Contexts: []string{
			fmt.Sprintf(&quot;%s=%s&quot;, &quot;Chart&quot;, path.Join(workspace, &quot;Chart.yaml&quot;)),
			fmt.Sprintf(&quot;%s=%s&quot;, &quot;Values&quot;, path.Join(workspace, &quot;values.yaml&quot;)),
		},
	}

huangapple
  • 本文由 发表于 2021年8月3日 19:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/68635386.html
匿名

发表评论

匿名网友

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

确定