🍣
HelmチャートでRangeのループ中に.Files.Getを使う方法
.Files.Getを使ってファイルを読み込みつつValues.yamlに定義した値を渡す場合
ディレクトリ構造
.
├── Chart.yaml
├── charts
├── files
│ └── test.txt
├── templates
│ └── configmap.yaml
└── values.yaml
template/configmap.yaml
{{- $currentScope := . -}}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: {{ $.Values.namespace }}
data:
test.txt: |
{{ tpl ($currentScope.Files.Get "files/test.txt") $currentScope | indent 4 }}
files/test.txt
helloworld
helloworld
{{ .Values.namespace }}
helm templateコマンド実行結果
yuta yutanoAir-2 ~ vamdemic helmfiles charts 19 % helm template -f values.yaml app .
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /Users/yuta/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /Users/yuta/.kube/config
---
# Source: charts/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: testnamespace
data:
test.txt: |
helloworld
helloworld
testnamespace
.Files.Getを使ってファイルを読み込みつつValues.yamlに定義した値を渡しつつ、Rangeでループ中の値も渡す場合
- tplの第二引数にdictに変換したループ中の変数を与えてあげれば良い
- .Filesはループ中のスコープには存在しないため、$.Filesで呼び出す必要がある
ディレクトリ構造
.
├── Chart.yaml
├── charts
├── files
│ └── test.txt
├── templates
│ └── configmap.yaml
└── values.yaml
template/configmap.yaml
{{- range $name := .Values.configs }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ printf "%s-config" $name }}
namespace: {{ $.Values.namespace }}
data:
test.txt: |
{{ tpl ($.Files.Get "files/test.txt") (dict "Values" $.Values "name" $name) | indent 4 }}
{{- end }}
files/test.txt
helloworld
helloworld
{{ .Values.namespace }}
{{ .name }}
values.yaml
namespace: testnamespace
configs:
config1: test1.txt
config2: test2.txt
config3: test3.txt
helm templateコマンド実行結果
yuta yutanoAir-2 ~ vamdemic helmfiles charts 19 % helm template -f values.yaml app . --debug
WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /Users/yuta/.kube/config
WARNING: Kubernetes configuration file is world-readable. This is insecure. Location: /Users/yuta/.kube/config
install.go:222: [debug] Original chart version: ""
install.go:239: [debug] CHART PATH: /Users/yuta/vamdemic/helmfiles/charts
---
# Source: charts/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test1.txt-config
namespace: testnamespace
data:
test.txt: |
helloworld
helloworld
testnamespace
test1.txt
---
# Source: charts/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test2.txt-config
namespace: testnamespace
data:
test.txt: |
helloworld
helloworld
testnamespace
test2.txt
---
# Source: charts/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test3.txt-config
namespace: testnamespace
data:
test.txt: |
helloworld
helloworld
testnamespace
test3.txt
Discussion