Includes¶
adeploy brings support to include and optionally render YAML parts or arbitrary files into k8s manifests.
This allows you to define repeating YAML parts (i.e. environments, affinities) at a single place and include them into all relevant manifest files.
Further it is now possible to add all files from a directory i.e. into a ConfigMap in order to create config files in your deployment.
Include YAML Parts¶
Jinja
If you want to include a common environment YAML part (i.e. env) into multiple k8s manifest files, you can create your
environment block at a single place:
env:
- name: VAR1
value: "my_common_var"
- name: VAR2
value: "{{ common.my_env_var }}"
- name: VAR2
value: "{{ common.another_env_var }}"
Tip
Make sure to start the part files intended to include in another YAML document start with a _ in order to exclude
them from being directly rendered by adeploy.
This part file can now be included in the manifest files as follows:
template:
metadata:
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: nginx
annotations:
checksum/config: |
{{ include_file("common/configmap.yml") | sha256sum }}
spec:
{{ include_file('common/__affinity.yml', direct=True, indent=6) }}
containers:
- name: main
image: {{ nginx.image }}:{{ version('nginx') }}
imagePullPolicy: Always
{{ include_file('common/__env.yml', direct=True, indent=10) }}
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: config
subPath: nginx.conf
readOnly: true
volumes:
- name: config
configMap:
name: {{ name }}-{{ release }}-config
template:
metadata:
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: redis
annotations:
checksum/config: |
{{ include_file("common/configmap.yml") | sha256sum }}
spec:
{{ include_file('common/__affinity.yml', direct=True, indent=6) }}
containers:
- name: main
image: {{ redis.image }}:{{ version('redis') }}
imagePullPolicy: Always
{{ include_file('common/__env.yml', direct=True, indent=10) }}
ports:
- name: redis
containerPort: 6379
livenessProbe:
exec:
command:
- redis-cli
- ping
initialDelaySeconds: 5
timeoutSeconds: 5
readinessProbe:
exec:
command:
- redis-cli
- ping
initialDelaySeconds: 5
timeoutSeconds: 5
See include_file() for usage and parameters.
Include Arbitrary Files¶
Jinja
To include an arbitrary file (i.e. nginx.conf) to a deployment you can use a ConfigMap as follows:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ name }}-{{ release }}-config
namespace: {{ namespace }}
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: config
data:
nginx.conf: {{ include_file('files/nginx.conf', render=False) }}
{% for name, content in list_dir('files/conf.d', render=False).items() %}
{{ name }}: {{ content }}
{% endfor %}
files/nginx.conf as well as scan the directory files/conf.d for files, read the
content and include them into the ConfigMap using the filename as key.
Expect dir, you can pass the same parameters for list_dir()
as for include_file() to control how the files
inside dir should be handled.
You can now mount these files into your deployment as usual:
template:
metadata:
labels:
app.kubernetes.io/part-of: {{ name }}
app.kubernetes.io/instance: {{ release }}
app.kubernetes.io/component: nginx
annotations:
checksum/config: |
{{ include_file("common/configmap.yml") | sha256sum }}
spec:
{{ include_file('common/__affinity.yml', direct=True, indent=6) }}
containers:
- name: main
image: {{ nginx.image }}:{{ version('nginx') }}
imagePullPolicy: Always
{{ include_file('common/__env.yml', direct=True, indent=10) }}
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: config
subPath: nginx.conf
readOnly: true
volumes:
- name: config
configMap:
name: {{ name }}-{{ release }}-config
Tip
You can use include_file() and the filter
sha256sum to hash the config map and thus trigger
adeploy (i.e. kubectl) to also re-create the deployments whenever a file in the config map has changed.
Download Remote Files¶
You can also use external URLs with include_file(path=https://....) to download and render definitions e.g. from GitHub.
For example the following will download and CRDs for ServerMonitors from prometheus-operator and add them to your deployment:
{{ include_file('https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/main/example/prometheus-operator-crd/monitoring.coreos.com_servicemonitors.yaml', direct=True, render=False, indent=0) }}
Skip & Escape¶
If you include the file content as a string, you can use skip to remove characters and escape to escape them as follows:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ name }}-{{ release }}-config
namespace: {{ namespace }}
labels:
{{ create_labels(component='config') }}
data:
single-line-string: {{ include_file('files/my-config-sccript.gohtml', direct=True, render=False, indent=0, skip=['\n', '\r'], escape=['\"'])) }}
This will include the content of my-config-script.gohtml while removing newlines and escape quotes " as \".
Find more examples in the examples folder
or see include_file() and
list_dir() for usage and parameters.