kubernetes - 使用 Helm 访问 k8s configmap 中的配置文件时数据为空
问题描述
我正在尝试在我的部署中使用带有 helm 图表的 configmap。现在似乎可以根据此处的文档使用 Helm 访问文件:https ://github.com/helm/helm/blob/master/docs/chart_template_guide/accessing_files.md
这是我的部署:
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: "{{ template "service.fullname" . }}"
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
replicas: {{ .Values.replicaCount }}
template:
metadata:
labels:
app: "{{ template "service.fullname" . }}"
spec:
containers:
- name: "{{ .Chart.Name }}"
image: "{{ .Values.registryHost }}/{{ .Values.userNamespace }}/{{ .Values.projectName }}/{{ .Values.serviceName }}:{{.Chart.Version}}"
volumeMounts:
- name: {{ .Values.configmapName}}configmap-volume
mountPath: /app/config
ports:
- containerPort: 80
name: http
livenessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 10
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 10
timeoutSeconds: 5
volumes:
- name: {{ .Values.configmapName}}configmap-volume
configMap:
name: "{{ .Values.configmapName}}-configmap"
我的 configmap 正在访问一个配置文件。这是配置图:
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{ .Values.configmapName}}-configmap"
labels:
app: "{{ .Values.configmapName}}"
data:
{{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}
图表目录如下所示:
files/
--runtime-config.json
templates/
--configmap.yaml
--deployment.yaml
--ingress.yaml
--service.yaml
chart.value
vaues.yaml
这就是我的 runtime-confi.json 文件的样子:
{
"GameModeConfiguration": {
"command": "xx",
"modeId": 10,
"sessionId": 11
}
}
问题是,当我安装我的图表时(即使是在试运行模式下),我的 configmap 的数据是空的。它不会将配置文件中的数据添加到我的 configmap 声明中。这是我进行试运行时的样子:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: "runtime-configmap"
labels:
app: "runtime"
data:
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: "whimsical-otter-runtime-service"
labels:
chart: "runtime-service-unknown/version"
spec:
replicas: 1
template:
metadata:
labels:
app: "whimsical-otter-runtime-service"
spec:
containers:
- name: "runtime-service"
image: "gcr.io/xxx-dev/xxx/runtime_service:unknown/version"
volumeMounts:
- name: runtimeconfigmap-volume
mountPath: /app/config
ports:
- containerPort: 80
name: http
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 100m
memory: 100Mi
livenessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 10
timeoutSeconds: 5
readinessProbe:
httpGet:
path: /health
port: http
initialDelaySeconds: 10
timeoutSeconds: 5
volumes:
- name: runtimeconfigmap-volume
configMap:
name: "runtime-configmap"
---
我做错了什么,我没有得到数据?
解决方案
替换字符串中的变量不起作用:
{{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}
但是您可以使用如下函数生成字符串printf
:
{{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 2 }}
推荐阅读
- ios - 为什么我使用 Swift 在屏幕上看不到 JSON 数据?
- pine-script - 更新指标的脚本不会影响所有图表 (pine-script)
- visual-studio-2010 - 桥接/连接旧 ASP.NET Webforms/.NET Framework 4 w/更新的 .NET Core Web API、MVC 和 Razor 页面时的兼容性问题和限制?
- python - IRIS 决策树分类问题
- c - 如何在 C 中通过 atof() 正确转换扫描的字符串?
- go - Go:通过 os.exec 执行命令时如何防止管道阻塞?
- c# - 如何在 .Net 中验证 Mongo ObjectId
- excel - 未出现在分配宏列表中的子
- haskell - 在谈论固定点时,“最小”和“最大”指的是什么顺序?
- lua - 如何记录/跟踪拼写下降的坐标