2

I have 2 files as follows:

_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/'

Can anyone figure out how to insert the data as per above scenario mentioned in myConfigMap.yaml file as a comment under data:.

Encycode
  • 530
  • 2
  • 7
  • 18
  • You should be able to pass one of these config files when you install the chart, `helm install -f _config-dev.yaml ...`; you don't need to make it conditional on some other value settings. – David Maze Jan 01 '22 at 12:02
  • My Values files I am already passing it.. I need to generate config map based on environment in values.yaml.. I know that i can pass the files during install which does not solve this problem. – Encycode Jan 01 '22 at 14:25
  • Say you're in the development environment; you can refer to `{{ .Values.REACT_APP_API_URL }}`, and it will get the files from `_config-dev.yaml` (via the `helm install -f` flag), and not see `_config-stag.yaml` at all (because it's not mentioned in command-line arguments). Unless I'm missing something about your setup? – David Maze Jan 01 '22 at 14:29
  • Yes i know about this but there are tones of variables and would lime to dynamically generate configmap based on the yaml file.. So i dont need to come configmap and set the variable each time add a value.. As there are more than 10 technology stack like front-end backend redis rds mongo apis seo and all.. So i simply need a way to manage variables in one file and get it autoset in config maps and secrets.. – Encycode Jan 01 '22 at 16:50
  • I am also able to take a whole _config-dev.yaml with .Files.Get function but not able to take the specic part of a file say frontend variables – Encycode Jan 01 '22 at 16:52

1 Answers1

1

my test project

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 }}

output

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
data:
  NODE_ENV: dev
  REACT_APP_API_URL: https://my-dev-api-url/
z.x
  • 1,905
  • 7
  • 11