首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gomplate:在<.Values.tpl.organization>:map上执行"<arg>“时没有关键字"Values”的条目

gomplate:在<.Values.tpl.organization>:map上执行"<arg>“时没有关键字"Values”的条目
EN

Stack Overflow用户
提问于 2021-08-03 11:40:59
回答 1查看 71关注 0票数 0

这是我的yaml文件

代码语言:javascript
复制
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中的相应值。以下是我的代码

代码语言:javascript
复制
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命令生成的结果

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-08-04 03:29:51

使用Contexts而不是DataSource

代码语言:javascript
复制
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")),
        },
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68635386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档