问题描述
必须根据树中某些文件的存在来生成模板。 我发现这样做的最好方法是在 helm 文件中有多个范围和一些要检查的 ifs,但它接缝表明 helm 不喜欢这种嵌套。
图表树
$signup = new Signup();
$errors = $signup->validate(
$_POST['username'] ?? '',$_POST['email'] ?? '',$_POST['password'] ?? ''
);
if ($errors) {
echo '<ul><li>' . implode('</li><li>',$errors) . '</li></ul>';
} else {
echo 'No errors';
}
我在模板中有一个包含以下内容的文件 microservices.yml:
├── 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
运行 {{ $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 文件的缩进。我在这里解决的是代码
{{- $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 }}