Helm 从多个配置 YAML 文件构建动态 configmap
Helm Construct dynamic configmap from multiple configuration YAML file
我有2个文件如下:
_config-dev.yaml
frontend:
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
database:
DB_USER: admin-dev
DB_PASSWORD: password-dev
_config-stag.yaml
frontend:
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
database:
DB_USER: admin-stag
DB_PASSWORD: password-stag
myConfigMap.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: {{ .Release.Name }}-frontend
namespace: {{ .Values.global.namespace }}
data:
# Here I want to insert only frontend data from _config-dev.yaml file if my {{ eq .Values.global.environment "dev" }} like below
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
# if my {{ eq .Values.global.environment "stag" }} i want to get frontend values from _config-dev.yaml like below
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
任何人都可以弄清楚如何按照 myConfigMap.yaml 文件中提到的上述场景插入数据作为数据下的注释:.
我的测试项目
test
├── Chart.yaml
├── cfg
│ ├── _config-dev.yaml
│ └── _config-stag.yaml
├── templates
│ └── configmap.yaml
└── values.yaml
values.yaml
global:
environment: dev
test/cfg/_config-dev.yaml
frontend:
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
database:
DB_USER: admin-dev
DB_PASSWORD: password-dev
test/cfg/_config-stag.yaml
frontend:
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
database:
DB_USER: admin-stag
DB_PASSWORD: password-stag
test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "test.fullname" . }}
data:
{{- $data := .Files.Get "cfg/_config-stag.yaml" }}
{{- if eq .Values.global.environment "dev" }}
{{- $data = .Files.Get "cfg/_config-dev.yaml" }}
{{- end }}
{{- $cfg := fromYaml $data }}
{{- range $k, $v := $cfg.frontend }}
{{ $k }}: {{ $v }}
{{- end }}
输出
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
NODE_ENV: dev
REACT_APP_API_URL: https://my-dev-api-url/
我有2个文件如下:
_config-dev.yaml
frontend:
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
database:
DB_USER: admin-dev
DB_PASSWORD: password-dev
_config-stag.yaml
frontend:
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
database:
DB_USER: admin-stag
DB_PASSWORD: password-stag
myConfigMap.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: {{ .Release.Name }}-frontend
namespace: {{ .Values.global.namespace }}
data:
# Here I want to insert only frontend data from _config-dev.yaml file if my {{ eq .Values.global.environment "dev" }} like below
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
# if my {{ eq .Values.global.environment "stag" }} i want to get frontend values from _config-dev.yaml like below
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
任何人都可以弄清楚如何按照 myConfigMap.yaml 文件中提到的上述场景插入数据作为数据下的注释:.
我的测试项目
test
├── Chart.yaml
├── cfg
│ ├── _config-dev.yaml
│ └── _config-stag.yaml
├── templates
│ └── configmap.yaml
└── values.yaml
values.yaml
global:
environment: dev
test/cfg/_config-dev.yaml
frontend:
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
database:
DB_USER: admin-dev
DB_PASSWORD: password-dev
test/cfg/_config-stag.yaml
frontend:
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
database:
DB_USER: admin-stag
DB_PASSWORD: password-stag
test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "test.fullname" . }}
data:
{{- $data := .Files.Get "cfg/_config-stag.yaml" }}
{{- if eq .Values.global.environment "dev" }}
{{- $data = .Files.Get "cfg/_config-dev.yaml" }}
{{- end }}
{{- $cfg := fromYaml $data }}
{{- range $k, $v := $cfg.frontend }}
{{ $k }}: {{ $v }}
{{- end }}
输出
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
NODE_ENV: dev
REACT_APP_API_URL: https://my-dev-api-url/