-1

必须根据树中某些文件的存在来生成模板。我发现这样做的最好方法是在 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??
4

1 回答 1

-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 }}
于 2021-01-11T20:18:03.117 回答