简体   繁体   English

使用 Helm 访问 k8s configmap 中的配置文件时数据为空

[英]Data is empty when accessing config file in k8s configmap with Helm

I am trying to use a configmap in my deployment with helm charts.我正在尝试在我的部署中使用 configmap 和 helm charts。 Now seems like files can be accessed with Helm according to the docs here: https://github.com/helm/helm/blob/master/docs/chart_template_guide/accessing_files.md现在似乎可以根据此处的文档使用 Helm 访问文件: https : //github.com/helm/helm/blob/master/docs/chart_template_guide/accessing_files.md

This is my deployment:这是我的部署:

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"

My configmap is accessing a config file.我的 configmap 正在访问一个配置文件。 Here's the configmap:这是配置图:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  {{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}

The charts directory looks like this:图表目录如下所示:

files/
--runtime-config.json
templates/
--configmap.yaml
--deployment.yaml
--ingress.yaml
--service.yaml
chart.value
vaues.yaml

And this is how my runtime-confi.json file looks like:这就是我的 runtime-confi.json 文件的样子:

{
    "GameModeConfiguration": {
        "command": "xx",
        "modeId": 10,
        "sessionId": 11            
    }
}

The problem is, when I install my chart (even with a dry-run mode), the data for my configmap is empty.问题是,当我安装图表时(即使使用试运行模式),我的 configmap 的数据是空的。 It doesn't add the data from the config file into my configmap declaration.它不会将配置文件中的数据添加到我的 configmap 声明中。 This is how it looks like when I do a dry-run:这是我进行试运行时的样子:

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

What am I doing wrong that I don't get data?我做错了什么,我没有得到数据?

The replacement of the variable within the string does not work:替换字符串中的变量不起作用:

{{ .Files.Get "files/{{ .Values.configmapName}}-config.json" | indent 2}}

But you can gerenate a string using the printf function like this:但是您可以使用printf函数生成一个字符串,如下所示:

{{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 2 }}

Apart from the syntax problem pointed by @adebasi, you still need to set this code inside a key to get a valid configmap yaml:除了@adebasi 指出的语法问题外,您仍然需要在键中设置此代码以获得有效的 configmap yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
  my-file: |
    {{ .Files.Get (printf "files/%s-config.json" .Values.configmapName) | indent 4}}

Or you can use the handy configmap helper:或者您可以使用方便的 configmap 助手:

apiVersion: v1
kind: ConfigMap
metadata:
  name: "{{ .Values.configmapName}}-configmap"
  labels:
    app: "{{ .Values.configmapName}}"
data:
{{ (.Files.Glob "files/*").AsConfig | indent 2 }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM