“.Files.Get” 将 json 导入配置映射不起作用

问题描述

这是我的目录结构:

+xyz:
     + custom-dashboard
        - abc.json
     +templates
        + grafana
            +abc.yaml  

abc.yaml 是我的配置映射。我尝试了很多不同的尝试

apiVersion: v1
kind: ConfigMap
Metadata:
  name: abc-configmap
data:
  abc.json: |-
    {{- range $mypath,$mybytes := .Files.Glob "custom-dashboards/serilog.json" }}
      {{ $mypath }}
    {{- end }}
#    {{ .Files.Get "custom-dashboards/abc.json" | indent 4}}
#  {{- $.Files.Get "custom-dashboards/abc.json" | indent 6 -}}
#  {{- (.Files.Glob "custom-dashboards/abc.json").AsConfig | indent 6 -}}

和许多其他人。没有实际加载文件内容

k get configmap serilog-configmap -o yaml

它看起来像:

Metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"serilog.json":"{{- range $mypath,$mybytes := .Files.Glob \"custom-dashboards/serilog.json\" }}\n  {{ $mypath }}\n{{- end }}"},"kind":"ConfigMap","Metadata":{"annotations":{},"name":"serilog-configmap","namespace":"monitoring"}}

一些在线帖子甚至说文档可能不正确。我从网站上复制了示例应用程序,但也没有用。不确定,我做错了什么。 https://helm.sh/docs/chart_template_guide/accessing_files/

请注意,如果我只是将我的 json 的内容复制到 abc.json 下:|-,它可以正常工作

apiVersion: v1
kind: ConfigMap
Metadata:
  name: abc-configmap
data:
  abc.json: |-

解决方法

我不知道是不是打字错误,但你写的是: {{ .Files.Get "custom-dashboards/abc.json" |缩进 4}} 至于结构 - 您编写了自定义仪表板。

无论如何,我正在使用:{{ tpl (.Files.Get $filename) 。 }} 并且完美运行