【问题标题】:Nested ranges from directorys in helmhelm中目录的嵌套范围
【发布时间】:2023-04-10 07:50:02
【问题描述】:

必须根据树中某些文件的存在来生成模板。 我发现最好的方法是在 helm 文件中使用多个范围和一些 ifs 来检查,但它表明 helm 不喜欢这种嵌套。

图表树

├── ms-values
│   ├── shared
│   │   ├── ms1.yml
│   │   └── ms2.yml
│   └── devops
│       ├── test
│       │   └── ms3.yml
│       ├── ms3.yml
│       └── ms4.yml
├── family-values
│   ├── test
│   │   └── shared.yml
│   ├── devops.yml
│   └── shared.yml
├── global-values
│   ├── test.yml
│   └── dev.yml
├── templates
│   └── micoservices.yml
└── values.yml

我在模板中有一个文件 microservices.yml,内容如下:

  {{ $enviroment := trimSuffix ".yml" (base $globalPath) }}
    enviroment: {{ $enviroment }}
    {{- range $familyPath, $familyBytes := $.Files.Glob "family-values/*.yml" -}}
    {{ $family := trimSuffix ".yml" (base $familyPath) }}
    {{- $familyMsPath := print "ms-values/" $family "/*.yml" }}
      {{- range $microservicePath, $microserviceBytes := $.Files.Glob $familyMsPath -}}
        {{- $microservice := trimSuffix ".yml" (base $microservicePath) -}}
        {{- $microserviceExt := base $microservicePath -}}
        {{- $envMSPath := print "ms-values/" $family "/" $enviroment "/" $microserviceExt -}}
        {{- $microserviceEnv := $.Files.Glob $envMSPath }}
    familys: {{ $family }}
    enviroment: {{ $enviroment }}
    MS: {{ $microservice -}} 
        {{- if $microserviceEnv }}
    MS_PATCH: {{- $envMSPath -}}
        {{- end -}}
        {{- $FamilyENVPath := print  "family-values/" $enviroment "/" $family ".yml" -}}
        {{- $FamilyENV := $.Files.Glob $FamilyENVPath }}
        {{- if $FamilyENV }}
    familyENVpatch: {{ $FamilyENVPath }}
        {{ end }}
    {{ end }}  
  {{ end }}
{{ end }}

运行helm template . --debug i我已经渲染了图表,但也出现了这个错误:

helm.go:81: [debug] error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context
YAML parse error on argo-apps/templates/microservices.yml
helm.sh/helm/v3/pkg/releaseutil.(*manifestFile).sort
        /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:146
helm.sh/helm/v3/pkg/releaseutil.SortManifests
        /home/circleci/helm.sh/helm/pkg/releaseutil/manifest_sorter.go:106
helm.sh/helm/v3/pkg/action.(*Configuration).renderResources
        /home/circleci/helm.sh/helm/pkg/action/action.go:165
helm.sh/helm/v3/pkg/action.(*Install).Run
        /home/circleci/helm.sh/helm/pkg/action/install.go:239
main.runInstall
        /home/circleci/helm.sh/helm/cmd/helm/install.go:241
main.newTemplateCmd.func2
        /home/circleci/helm.sh/helm/cmd/helm/template.go:70
github.com/spf13/cobra.(*Command).execute
        /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:842
github.com/spf13/cobra.(*Command).ExecuteC
        /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:950
github.com/spf13/cobra.(*Command).Execute
        /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:887
main.main
        /home/circleci/helm.sh/helm/cmd/helm/helm.go:80
runtime.main
        /usr/local/go/src/runtime/proc.go:203
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1373```

Any ideas??

【问题讨论】:

    标签: kubernetes-helm go-templates helmfile


    【解决方案1】:

    helm 文件缩进的问题(我认为)。我这里解决的是代码

    {{- $enviroment := trimSuffix ".yml" (base $globalPath) }}
    {{ range $familyPath, $familyBytes := $.Files.Glob "family-values/*.yml" }}
    {{ $family := trimSuffix ".yml" (base $familyPath) -}}
    {{ $familyMsPath := print "ms-values/" $family "/*.yml" }}
    {{ range $microservicePath, $microserviceBytes := $.Files.Glob $familyMsPath }}
    {{ $microservice := trimSuffix ".yml" (base $microservicePath) -}}
    {{ $envMSPath := print "ms-values/" $family "/" $enviroment "/" $microservice ".yml" }}
    {{ $microserviceEnv := $.Files.Glob $envMSPath }}
    {{ $microserviceData := $.Files.Get $microservicePath | fromYaml}}
    {{ if has $enviroment $microserviceData.enviromentEnabled }}
    ---
    apiVersion: argoproj.io/v1alpha1
    kind: Application
    metadata:
      name: {{ $family }}-{{ $microservice }}
      namespace: argos
    spec:
      project: {{ $enviroment }}-{{ $family }}-{{ $microservice }}
      source:
        path: microservice
        repoURL: 'git@example:infra/devops-charts.git'
        targetRevision: HEAD
        helm:
          valueFiles:
          - global-values/{{ $enviroment }}.yml
          - family-values/{{ $family }}.yml
    {{- $FamilyENVPath := print  "family-values/" $enviroment "/" $family ".yml" }}
    {{- $FamilyENV := $.Files.Glob $FamilyENVPath }}
    {{- if $FamilyENV }}
          - {{ $FamilyENVPath }}
    {{- end }}
          - ms-values/{{ $microservice }}.yml
    {{- if $microserviceEnv }}
          - {{ $envMSPath }}
    {{- end }}
    {{- range $key, $value := $.Values.clusters -}}
    {{- if eq $key $enviroment }}
      destination:
        server: {{ $value }}
        namespace: "{{ $family }}-{{ $microservice }}"
    {{ end }}
    {{ end }}
    {{ end }}
    {{ end }}
    {{ end }}
    {{ end }}
    

    【讨论】:

      猜你喜欢
      • 2023-01-15
      • 2016-03-13
      • 1970-01-01
      • 2010-12-18
      • 2011-01-01
      • 2018-05-23
      • 1970-01-01
      • 2017-11-30
      相关资源
      最近更新 更多