在templates/_helpers.tpl文件下添加辅助函数。
不幸的是,go 模板中没有 switch 功能,所以它必须是“脏”的,否则
{{/*
Environment name mapping
*/}}
{{- define "my-chart.environment" -}}
{{- if .Values.global.environment -}}
{{- if eq .Values.global.environment "production" -}}
{{- printf "prod" -}}
{{- else if eq .Values.global.environment "staging" -}}
{{- printf "stage" -}}
{{- else if eq .Values.global.environment "test" -}}
{{- printf "test" -}}
{{- else if eq .Values.global.environment "development" -}}
{{- printf "dev" -}}
{{- end -}}
{{- end -}}
{{- end -}}
然后,在my-chart.fullname 模板中使用此模板
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "my-chart.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
##### ADDITIONAL LINES
{{- if .Values.global.environment -}}
{{- printf "%s-%s-%s" (include "my-chart.environment" .) .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
##### END ADDITIONAL LINES
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
测试:
/apps/my-chart # helm version
version.BuildInfo{Version:"v3.1.2", GitCommit:"d878d4d45863e42fd5cff6743294a11d28a9abce", GitTreeState:"clean", GoVersion:"go1.13.8"}
/apps/my-chart # cat ./values.yaml
# Default values for my-chart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
global:
environment: development
正在运行helm template .
输出:
...
...
# Source: my-chart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: dev-RELEASE-NAME-my-chart
...
...