How do I load multiple templated config files into

2019-02-14 16:02发布

So I am trying to build a helm chart.

in my templates file I've got a file like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-map
data:
{{ Do something here to load up a set of files | indent 2 }}

I have another directory in my chart: configmaps where a set of json files, that themselves will have templated variables in them:

a.json
b.json
c.json

Ultimately I'd like to be sure in my chart I can reference:

volumes:
   - name: config-a
     configMap:
       name: config-map
       items:
       - key: a.json
         path: a.json

2条回答
该账号已被封号
2楼-- · 2019-02-14 16:42

I assume that a.json,b.json,c.json etc. is a defined list and you know all the contents (apart from the bits that you want to set as values through templated variables). I'm also assuming you only want to expose parts of the content of the files to users and not to let the user configure the whole file content. (But if I'm assuming wrong and you do want to let users set the whole file content then the suggestion from @hypnoglow of following the datadog chart seems to me a good one.) If so I'd suggest the simplest way to do it is to do:

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-map
data:
  a.json:
  # content of a.json in here, including any templated stuff with {{ }}
  b.json:
  # content of b.json in here, including any templated stuff with {{ }}
  c.json:
  # content of c.json in here, including any templated stuff with {{ }}

I guess you'd like to mount then to the same directory. It would be tempting for cleanliness to use different configmaps but that would then be a problem for mounting to the same directory. It would also be nice to be able to load the files independently using .Files.Glob to be able to reference the files without having to put the whole content in the configmap but I don't think you can do that and still use templated variables in them... However, you can do it with Files.Get to read the file content as a string and the pass that into tpl to put it through the templating engine as @Oleg Mykolaichenko suggests in https://stackoverflow.com/a/52009992/9705485. I suggest everyone votes for his answer as it is the better solution. I'm only leaving my answer here because it explains why his suggestion is so good and some people may prefer the less abstract approach.

查看更多
beautiful°
3楼-- · 2019-02-14 17:05

I had same problem for a few weeks ago with adding files and templates directly to container.

Look for the sample syntax:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap-{{ .Release.Name }}
  namespace: {{ .Release.Namespace }}
  labels:
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  nginx_conf: {{ tpl (.Files.Get "files/nginx.conf") . | quote }}
  ssl_conf: {{ tpl (.Files.Get "files/ssl.conf") . | quote }}
  dhparam_pem: {{ .Files.Get "files/dhparam.pem" | quote }}
  fastcgi_conf: {{ .Files.Get "files/fastcgi.conf" | quote }}
  mime_types: {{ .Files.Get "files/mime.types" | quote }}
  proxy_params_conf: {{ .Files.Get "files/proxy_params.conf" | quote }}

Second step is to reference it from deployment:

  volumes:
  - name: {{ $.Release.Name }}-configmap-volume
    configMap:
      name:nginx-configmap-{{ $.Release.Name }}
      items:
        - key: dhparam_pem
          path: dhparam.pem
        - key: fastcgi_conf
          path: fastcgi.conf
        - key: mime_types
          path: mime.types
        - key: nginx_conf
          path: nginx.conf
        - key: proxy_params_conf
          path: proxy_params.conf
        - key: ssl_conf
          path: ssl.conf 

It's actual for now. Here you can find 2 types of importing:

  • regular files without templating
  • configuration files with dynamic variables inside

Please do not forget to read official docs: https://github.com/helm/helm/blob/master/docs/chart_template_guide/accessing_files.md

Good luck!

查看更多
登录 后发表回答