This is my configmap template, it is taking data from my user-data file which is like values.yaml. I have two applications, App-A & App-B. The IPs are getting assigned by App-B, in App-A only interfaces are getting configured to which App-B is assigning IPs.
CONFIGMAP Template:
apiVersion: v1
kind: ConfigMap
metadata:
namespace: {{$namespace}}
name: {{$pod}}-configmap
data:
user-data: |
{{- $userdata := (cat $pod "-userdata"|nospace)}}
{{ $.Files.Get $userdata | nindent 4}}
App-A user-data
ICI1: &fpbond0
TYPE: bond
GATEWAY: 172.57.93.129
IFNAME: fpbond0
PREFIX: 26
SUBPORT: fpeth2,fpeth3
OPTS: "mode=active-backup miimon=100 fail_over_mac=none"
PORTTYPE: DP_PORT_TYPE_SIGNALING
VIRTUAL_LINK_ID: vnfc_vldid_ici1
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: vnfc_vldid_ici_pip
MTU: 1500
ICI2:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici2
VIRTUAL_IP_REQUIRED: True
.............till ICI60
App-B user-data:
vnfc_vldid_ici1:
- 172.7.93.132
vnfc_vldid_ici2:
- 172.7.93.133
vnfc_vldid_ici3:
- 172.7.93.134
vnfc_vldid_ici4:
- 172.7.93.135
.............till ICI60
I don't know whether your ip data source comes from the values.yaml file or is read from other files, the above is not clear, so here are two solutions.
A: from values.yaml
values.yaml
IPs:
ICI1: &fpbond0
TYPE: bond
GATEWAY: 172.57.93.1
IFNAME: fpbond0
PREFIX: 26
SUBPORT: fpeth2,fpeth3
OPTS: "mode=active-backup miimon=100 fail_over_mac=none"
PORTTYPE: DP_PORT_TYPE_SIGNALING
VIRTUAL_LINK_ID: vnfc_vldid_ici1
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.4
MTU: 1500
ICI2:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici2
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.5
ICI3:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici3
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.6
ICI4:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici4
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.7
template/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: text
data:
{{- range $.Values.IPs }}
{{ .VIRTUAL_LINK_ID }}: {{ .PIP_VIRTUAL_LINK_ID }}
{{- end }}
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
vnfc_vldid_ici1: 172.57.93.4
vnfc_vldid_ici2: 172.57.93.5
vnfc_vldid_ici3: 172.57.93.6
vnfc_vldid_ici4: 172.57.93.7
B: from file
tree
.
├── Chart.yaml
├── prop
│ └── ips
├── templates
│ ├── NOTES.txt
│ ├── _helpers.tpl
│ ├── configmap.yaml
│ └── deployment.yaml
└── values.yaml
prop/ips
ICI1: &fpbond0
TYPE: bond
GATEWAY: 172.57.93.1
IFNAME: fpbond0
PREFIX: 26
SUBPORT: fpeth2,fpeth3
OPTS: "mode=active-backup miimon=100 fail_over_mac=none"
PORTTYPE: DP_PORT_TYPE_SIGNALING
VIRTUAL_LINK_ID: vnfc_vldid_ici1
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.4
MTU: 1500
ICI2:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici2
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.5
ICI3:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici3
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.6
ICI4:
<<: *fpbond0
VIRTUAL_LINK_ID: vnfc_vldid_ici4
VIRTUAL_IP_REQUIRED: True
PIP_VIRTUAL_LINK_ID: 172.57.93.7
template/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
{{- $data := .Files.Get "prop/ips" }}
{{- $cfg := fromYaml $data }}
{{- range $cfg }}
{{ .VIRTUAL_LINK_ID }}: {{ .PIP_VIRTUAL_LINK_ID }}
{{- end }}
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
vnfc_vldid_ici1: 172.57.93.4
vnfc_vldid_ici2: 172.57.93.5
vnfc_vldid_ici3: 172.57.93.6
vnfc_vldid_ici4: 172.57.93.7
Related
I can not find a way to iterate over a range in helm templating. I have the next definition in my values.yaml
Variable dictionary to be consumed
projects:
- tenants: imc
namespaces:
- name: test-1
company: inter
environments:
- build
- dev
- stage
- test
- name: test-2
environments:
- build
- dev
- stage
- test
- name: test-3
environments:
- build
- dev
- stage
- test
Code snippet
{{- range $key, $value := .Values.tenants }}
{{- range $nkey, $nvalue := .namespaces }}
---
apiVersion: v1
kind: Namespace
metadata:
annotations:
parent_project_name: {{ $value.name }}
company: {{ $value.company }}
openshift.io/description: ""
openshift.io/display-name: ""
labels:
tenant: {{ $value.tenants }}
name: {{ $value.name }}-{{ $nvalue }}
spec: {}
status: {}
{{- end }}
{{- end }}
I need help in consuming the variable into the template
May you give the expected output? This is what I guess.
values.yaml
projects:
- tenants: imc
namespaces:
- name: test-1
company: inter
environments:
- build
- dev
- stage
- test
- name: test-2
environments:
- build
- dev
- stage
- test
- name: test-3
environments:
- build
- dev
- stage
- test
templates/ns.yaml
{{- range $key, $value := .Values.projects }}
{{- range $nkey, $nvalue := .namespaces }}
---
apiVersion: v1
kind: Namespace
metadata:
annotations:
parent_project_name: {{ $nvalue.name }}
company: {{ $nvalue.company }}
openshift.io/description: ""
openshift.io/display-name: ""
labels:
tenant: {{ $value.tenants }}
name: {{ $nvalue.name }}
spec: {}
status: {}
{{- end }}
{{- end }}
cmd
helm template --debug test .
output
---
apiVersion: v1
kind: Namespace
metadata:
annotations:
parent_project_name: test-1
company: inter
openshift.io/description: ""
openshift.io/display-name: ""
labels:
tenant: imc
name: test-1
spec: {}
status: {}
---
apiVersion: v1
kind: Namespace
metadata:
annotations:
parent_project_name: test-2
company:
openshift.io/description: ""
openshift.io/display-name: ""
labels:
tenant: imc
name: test-2
spec: {}
status: {}
---
apiVersion: v1
kind: Namespace
metadata:
annotations:
parent_project_name: test-3
company:
openshift.io/description: ""
openshift.io/display-name: ""
labels:
tenant: imc
name: test-3
spec: {}
status: {}
I have current implementation like below which takes the same configurations for each replicas.
Is there a possiblity to get the different values for each replica creation ?
statefulset file :
{{- $outer := . -}}
{{- range $idx, $app := .Values.appliance_type }}
{{- with $outer -}}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ $app.name }}
labels:
app: "{{ $app.appName }}"
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: "{{ .Release.Name }}"
spec:
replicas: {{ $app.replicaCount }}
serviceName: {{ $app.serviceName }}
selector:
matchLabels:
app: {{ $app.appName }}
template:
metadata:
labels:
app: {{ $app.appName }}
spec:
containers:
- name: selfcheck
image: {{ .Values.image.registry }}/{{ .Values.da.pod.selfcheck.repository}}
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: CDNHOSTNAME
value: '{{ $app.hostname }}'
- name: CUSER
value: '{{ .Values.da.conf.consoleUser }}'
- name: CPASSWORD
value: '{{ .Values.da.conf.consolePassword }}'
{{ end }}
{{- end -}}
===================
values.yaml
appliance_type:
- name: sethu
hostname : s1
replicaCount: 2
serviceName: da
appName: test-ac
- name: ram
hostname : r1
replicaCount: 1
serviceName: ida
appName: test-ia
===================
Actual Results :
it will create 3 PODs
sethu-0 => CDNHOSTNAME (s1)
sethu-1 => CDNHOSTNAME (s1)
ram-0 => CDNHOSTNAME (r1)
Needed Results :
sethu-0 => CDNHOSTNAME (s1)
sethu-1 => CDNHOSTNAME (s2)
ram-0 => CDNHOSTNAME (r1)
hostname of sethu-0 and sethu-1 need to be taken different values from values.yaml
kind of below configuration - but not working
appliance_type:
- name: sethu
hostname :
- s1
- s2
replicaCount: 2
serviceName: da
appName: test-ac
- name: ram
replicaCount: 1
serviceName: ida
appName: test-ia
I have 2 files as follows:
_config-dev.yaml
frontend:
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
database:
DB_USER: admin-dev
DB_PASSWORD: password-dev
_config-stag.yaml
frontend:
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
database:
DB_USER: admin-stag
DB_PASSWORD: password-stag
myConfigMap.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: {{ .Release.Name }}-frontend
namespace: {{ .Values.global.namespace }}
data:
# Here I want to insert only frontend data from _config-dev.yaml file if my {{ eq .Values.global.environment "dev" }} like below
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
# if my {{ eq .Values.global.environment "stag" }} i want to get frontend values from _config-dev.yaml like below
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
Can anyone figure out how to insert the data as per above scenario mentioned in myConfigMap.yaml file as a comment under data:.
my test project
test
├── Chart.yaml
├── cfg
│ ├── _config-dev.yaml
│ └── _config-stag.yaml
├── templates
│ └── configmap.yaml
└── values.yaml
values.yaml
global:
environment: dev
test/cfg/_config-dev.yaml
frontend:
NODE_ENV: dev
REACT_APP_API_URL: 'https://my-dev-apiurl/'
database:
DB_USER: admin-dev
DB_PASSWORD: password-dev
test/cfg/_config-stag.yaml
frontend:
NODE_ENV: stag
REACT_APP_API_URL: 'https://my-stag-api-url/'
database:
DB_USER: admin-stag
DB_PASSWORD: password-stag
test/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "test.fullname" . }}
data:
{{- $data := .Files.Get "cfg/_config-stag.yaml" }}
{{- if eq .Values.global.environment "dev" }}
{{- $data = .Files.Get "cfg/_config-dev.yaml" }}
{{- end }}
{{- $cfg := fromYaml $data }}
{{- range $k, $v := $cfg.frontend }}
{{ $k }}: {{ $v }}
{{- end }}
output
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
NODE_ENV: dev
REACT_APP_API_URL: https://my-dev-api-url/
I have defined the values.yaml like the following:
name: custom-streams
image: streams-docker-images
imagePullPolicy: Always
restartPolicy: Always
replicas: 1
port: 8080
nodeSelector:
nodetype: free
configHocon: |-
streams {
monitoring {
custom {
uri = ${?URI}
method = ${?METHOD}
}
}
}
And configmap.yaml like the following:
apiVersion: v1
kind: ConfigMap
metadata:
name: custom-streams-configmap
data:
config.hocon: {{ .Values.configHocon | indent 4}}
Lastly, I have defined the deployment.yaml like the following:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
spec:
replicas: {{ default 1 .Values.replicas }}
strategy: {}
template:
spec:
containers:
- env:
{{- range $key, $value := .Values.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
image: {{ .Values.image }}
name: {{ .Values.name }}
volumeMounts:
- name: config-hocon
mountPath: /config
ports:
- containerPort: {{ .Values.port }}
restartPolicy: {{ .Values.restartPolicy }}
volumes:
- name: config-hocon
configmap:
name: custom-streams-configmap
items:
- key: config.hocon
path: config.hocon
status: {}
When I run the container via:
helm install --name custom-streams custom-streams -f values.yaml --debug --namespace streaming
Then the pods are running fine, but I cannot see the config.hocon file in the container:
$ kubectl exec -it custom-streams-55b45b7756-fb292 sh -n streaming
/ # ls
...
config
...
/ # cd config/
/config # ls
/config #
I need the config.hocon written in the /config folder. Can anyone let me know what is wrong with the configurations?
I was able to resolve the issue. The issue was using configmap in place configMap in deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
spec:
replicas: {{ default 1 .Values.replicas }}
strategy: {}
template:
spec:
containers:
- env:
{{- range $key, $value := .Values.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
image: {{ .Values.image }}
name: {{ .Values.name }}
volumeMounts:
- name: config-hocon
mountPath: /config
ports:
- containerPort: {{ .Values.port }}
restartPolicy: {{ .Values.restartPolicy }}
volumes:
- name: config-hocon
configMap:
name: custom-streams-configmap
items:
- key: config.hocon
path: config.hocon
status: {}
At present I am creating a configmap from the file config.json by executing:
kubectl create configmap jksconfig --from-file=config.json
I would want the ConfigMap to be created as part of the deployment and tried to do this:
apiVersion: v1
kind: ConfigMap
metadata:
name: jksconfig
data:
config.json: |-
{{ .Files.Get "config.json" | indent 4 }}
But doesn't seem to work. What should be going into configmap.yaml so that the same configmap is created?
---UPDATE---
when I do a helm install dry run:
# Source: mychartv2/templates/jks-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: jksconfig
data:
config.json: |
Note: I am using minikube as my kubernetes cluster
Your config.json file should be inside your mychart/ directory, not inside mychart/templates
Chart Template Guide
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
config.json: |-
{{ .Files.Get "config.json" | indent 4}}
config.json
{
"val": "key"
}
helm install --dry-run --debug mychart
[debug] Created tunnel using local port: '52091'
[debug] SERVER: "127.0.0.1:52091"
...
NAME: dining-saola
REVISION: 1
RELEASED: Fri Nov 23 15:06:17 2018
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}
...
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: dining-saola-configmap
data:
config.json: |-
{
"val": "key"
}
EDIT:
But I want it the values in the config.json file to be taken from values.yaml. Is that possible?
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
config.json: |-
{
{{- range $key, $val := .Values.json }}
{{ $key | quote | indent 6}}: {{ $val | quote }}
{{- end}}
}
values.yaml
json:
key1: val1
key2: val2
key3: val3
helm install --dry-run --debug mychart
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mangy-hare-configmap
data:
config.json: |-
{
"key1": "val1"
"key2": "val2"
"key3": "val3"
}
Here is an example of a ConfigMap that is attached to a Deployment:
ConfigMap:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: jksconfig
data:
config.json: |-
{{ .Files.Get "config.json" | indent 4 }}
Deployment:
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: jksapp
labels:
app: jksapp
spec:
selector:
matchLabels:
app: jksapp
template:
metadata:
labels:
app: jksapp
containers:
- name: jksapp
image: jksapp:1.0.0
ports:
- containerPort: 8080
volumeMounts:
- name: config #The name(key) value must match pod volumes name(key) value
mountPath: /path/to/config.json
volumes:
- name: config
configMap:
name: jksconfig
Soln 01:
insert your config.json file content into a template
then use this template into your data against config.json
then run $ helm install command
finally,
{{define "config"}}
{
"a": "A",
"b": {
"b1": 1
}
}
{{end}}
apiVersion: v1
kind: ConfigMap
metadata:
name: jksconfig
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
app: "my-app"
heritage: "{{ .Release.Service }}"
release: "{{ .Release.Name }}"
data:
config.json: {{ (include "config" .) | trim | quote }}