![](/img/trans.png)
[英]How to edit a helm chart (loki) config files for Grafana Cloud
[英]How do I load multiple templated config files into a helm chart?
所以我正在尝试构建一个舵图。
在我的模板文件中,我有一个文件,如:
apiVersion: v1
kind: ConfigMap
metadata:
name: config-map
data:
{{ Do something here to load up a set of files | indent 2 }}
我的图表中有另一个目录: configmaps
,其中包含一组 json 文件,它们本身将包含模板化变量:
a.json
b.json
c.json
最终,我想确定在我的图表中我可以参考:
volumes:
- name: config-a
configMap:
name: config-map
items:
- key: a.json
path: a.json
几周前,我在将文件和模板直接添加到容器时遇到了同样的问题。
查找示例语法:
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 }}
第二步是从部署中引用它:
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
目前是实际的。 在这里您可以找到 2 种导入类型:
请不要忘记阅读官方文档: https : //helm.sh/docs/chart_template_guide/accessing_files/
祝你好运!
包含目录config-dir/
所有文件,使用{{ range ..
:
我的configmap.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
{{- $files := .Files }}
{{- range $key, $value := .Files }}
{{- if hasPrefix "config-dir/" $key }} {{/* only when in config-dir/ */}}
{{ $key | trimPrefix "config-dir/" }}: {{ $files.Get $key | quote }} {{/* adapt $key as desired */}}
{{- end }}
{{- end }}
我的部署.yaml
apiVersion: apps/v1
kind: Deployment
...
spec:
template:
...
spec:
containers:
- name: my-pod-container
...
volumeMounts:
- name: my-volume
mountPath: /config
readOnly: true # is RO anyway for configMap
volumes:
- name: my-volume
configMap:
name: my-configmap
# defaultMode: 0555 # mode rx for all
我假设 a.json、b.json、c.json 等是一个已定义的列表,并且您知道所有内容(除了要通过模板化变量设置为值的位)。 我还假设您只想向用户公开文件的部分内容,而不是让用户配置整个文件内容。 (但如果我假设错误并且您确实想让用户设置整个文件内容,那么@hypnoglow 遵循数据狗图表的建议在我看来是一个很好的建议。)如果是这样,我建议使用最简单的方法它是做:
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 {{ }}
我猜你想挂载到同一个目录。 使用不同的 configmaps 会很吸引人,但是如果挂载到同一目录,这将是一个问题。 能够使用 .Files.Glob 独立加载文件以便能够引用文件而不必将整个内容放在 configmap 中也很好,但我认为您不能这样做并且仍然使用模板化变量在它们中......但是,您可以使用 Files.Get 将文件内容作为字符串读取,然后将其传递到 tpl 中以将其通过模板引擎,如@Oleg Mykolaichenko 在https://stackoverflow.com/ 中建议的那样a/52009992/9705485 。 我建议每个人都投票支持他的答案,因为这是更好的解决方案。 我只是在这里留下我的答案,因为它解释了为什么他的建议如此好,有些人可能更喜欢不那么抽象的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.