动态ConfigMap掌舵模板

时间:2019-02-01 07:39:58

标签: templates kubernetes kubernetes-helm

我想用一个舵形模板创建服务器配置的ConfiMap。因此,我为config / values创建了一个文件夹,并为每个ConfigMap创建了一个配置文件。我已经阅读了头盔模板指南,发现对我的问题没有帮助。也许我误解了掌舵的可能性。

之后,可以从服务器文件创建一个配置映射:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap
data:
  {{- $files := .Files }}
  {{- range tuple "file1.yaml" "file2.yaml" }}
  {{ $files.Get . }}
  {{- end }}

任何建议都会有所帮助, 谢谢,

最美好的祝愿

2 个答案:

答案 0 :(得分:0)

通常Tiller呈现templates/目录中的所有模板。 因此,如果我正确理解您的问题-您可以从下面的简单示例开始:

1)创建测试图,并删除所有预定义的模板

helm create testchart
rm -rf testchart/templates/*

2)中创建2个Configmaps YAML文件templates/

configmap1.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap-1
data:
  myvalue: "My Drinks:"
  drink1: {{ .Values.config1test.drink1 }}
  drink2: {{ .Values.config1test.drink2 }}
  drink3: {{ .Values.config1test.drink3 }}

configmap2.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-configmap-2
data:
  myvalue: "My food:"
  food1: {{ .Values.config2test.food1 }}
  food2: {{ .Values.config2test.food2 }}
  food3: {{ .Values.config2test.food3 }}

3)创建值的文件(1或更高,这取决于您的实现。我创建2)

myvals1.yaml:

config1test:
  drink1: coffee
  drink2: tea
  drink3: juice

myvals2.yaml:

config2test:
  food1: meat
  food2: fish
  food3: salad

4)在应用之前测试模板渲染:

helm install --dry-run --debug -f ./testchart/myvals1.yaml -f ./testchart/myvals2.yaml ./testchart

[debug] Created tunnel using local port: '37605'

[debug] SERVER: "127.0.0.1:37605"

[debug] Original chart version: ""
[debug] CHART PATH: /home/vkryvoruchko/testchart

NAME:   tan-frog
REVISION: 1
RELEASED: Fri Feb  1 13:10:46 2019
CHART: testchart-0.1.0
USER-SUPPLIED VALUES:
config1test:
  drink1: coffee
  drink2: tea
  drink3: juice
config2test:
  food1: meat
  food2: fish
  food3: salad

COMPUTED VALUES:
affinity: {}
config1test:
  drink1: coffee
  drink2: tea
  drink3: juice
config2test:
  food1: meat
  food2: fish
  food3: salad
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: nginx
  tag: stable
ingress:
  annotations: {}
  enabled: false
  hosts:
  - chart-example.local
  paths: []
  tls: []
nameOverride: ""
nodeSelector: {}
replicaCount: 1
resources: {}
service:
  port: 80
  type: ClusterIP
tolerations: []

HOOKS:
MANIFEST:


---
# Source: testchart/templates/configmap1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: tan-frog-configmap-1
data:
  myvalue: "My Drinks:"
  drink1: coffee
  drink2: tea
  drink3: juice
---
# Source: testchart/templates/configmap2.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: tan-frog-configmap-2
data:
  myvalue: "My food:"
  food1: meat
  food2: fish
  food3: salad

5)安装图表

helm install -f ./testchart/myvals1.yaml -f ./testchart/myvals2.yaml ./testchart

NAME:   unsung-grizzly
LAST DEPLOYED: Fri Feb  1 13:13:15 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME                        DATA  AGE
unsung-grizzly-configmap-1  4     0s
unsung-grizzly-configmap-2  4     0s

6)验证ConfigMap:

kubectl get configmaps -o wide

NAME                         DATA   AGE
unsung-grizzly-configmap-1   4      61s
unsung-grizzly-configmap-2   4      61s

答案 1 :(得分:0)

感谢您的答复。我有不同的想法。我的新代码使它更加清晰。

{{ range $k, $v :=  .Values.configs }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap
  namespace: {{ $.Values.namespace }}
  labels:
    app: "{{base $v}}"
data:
  key: {{$k}}
  value: {{$v}}
{{ $.Files.Get  $v }}
{{ end }}

我在ConfigMap上有一个循环。我的values.yaml看起来像

configs
   name: configs/file1
   name: configs/file2

这些值位于单独的文件夹配置中,每个configmap一个文件。

当前的问题是,结果是一个具有File2值的ConfigMap。我希望有两个ConfigMap。我的模板在这里出了什么问题。

非常感谢您。