Skip to content

Commit

Permalink
charts: bypass unqouted Jinja dump to YAML
Browse files Browse the repository at this point in the history
This hack is required to bypass the fact that dumping Jinja
to YAML does not work with the render script because of the presence
of unquote special characters e.g `{%`.

If we try to escape thess special characters, we end up with type(str)
always and in some special conditions, we need for example replicas count
to be of type(int)
  • Loading branch information
gdemonet authored and Ebaneck committed Feb 26, 2020
1 parent 95c6e6d commit 55cdbc2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
27 changes: 17 additions & 10 deletions charts/prometheus-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ commonLabels:
alertmanager:
alertmanagerSpec:
image:
repository: '''{%- endraw -%}{{ build_image_name("alertmanager", False) }}{%- raw -%}'''
repository: '__image__(alertmanager)'

nodeSelector:
node-role.kubernetes.io/infra: ''
Expand All @@ -24,6 +24,8 @@ alertmanager:
operator: 'Exists'
effect: 'NoSchedule'

replicas: '__var__(alertmanager.spec.deployment.replicas)'

storage:
volumeClaimTemplate:
spec:
Expand Down Expand Up @@ -56,22 +58,22 @@ prometheusOperator:
effect: 'NoSchedule'

image:
repository: '{%- endraw -%}{{ build_image_name(\"prometheus-operator\", False) }}{%- raw -%}'
repository: '__image__(prometheus-operator)'

configmapReloadImage:
repository: '{%- endraw -%}{{ build_image_name("configmap-reload", False) }}{%- raw -%}'
repository: '__image__(configmap-reload)'

prometheusConfigReloaderImage:
repository: '{%- endraw -%}{{ build_image_name("prometheus-config-reloader", False) }}{%- raw -%}'
repository: '__image__(prometheus-config-reloader)'

hyperkubeImage:
repository: '''{%- endraw -%}{{ build_image_name("hyperkube", False) }}{%- raw -%}'''
repository: '__image__(hyperkube)'


prometheus:
prometheusSpec:
image:
repository: '''{%- endraw -%}{{ build_image_name("prometheus", False) }}{%- raw -%}'''
repository: '__image__(prometheus)'

tolerations:
- key: 'node-role.kubernetes.io/bootstrap'
Expand All @@ -81,6 +83,8 @@ prometheus:
operator: 'Exists'
effect: 'NoSchedule'

replicas: '__var__(prometheus.spec.deployment.replicas)'

nodeSelector:
node-role.kubernetes.io/infra: ''

Expand All @@ -102,10 +106,10 @@ grafana:
adminPassword: admin

image:
repository: '{%- endraw -%}{{ build_image_name(\"grafana\", False) }}{%- raw -%}'
repository: '__image__(grafana)'

sidecar:
image: '{%- endraw -%}{{ build_image_name(\"k8s-sidecar\", False) }}{%- raw -%}:0.1.20'
image: '__image__(k8s-sidecar):0.1.20'

nodeSelector:
node-role.kubernetes.io/infra: ''
Expand All @@ -118,6 +122,9 @@ grafana:
operator: 'Exists'
effect: 'NoSchedule'

replicas: '__var__(grafana.spec.deployment.replicas)'


ingress:
enabled: true
annotations:
Expand All @@ -140,7 +147,7 @@ grafana:

kube-state-metrics:
image:
repository: '{%- endraw -%}{{ build_image_name(\"kube-state-metrics\", False) }}{%- raw -%}'
repository: '__image__(kube-state-metrics)'

nodeSelector:
node-role.kubernetes.io/infra: ''
Expand All @@ -156,7 +163,7 @@ kube-state-metrics:

prometheus-node-exporter:
image:
repository: '{%- endraw -%}{{ build_image_name(\"node-exporter\", False) }}{%- raw -%}'
repository: '__image__(node-exporter)'


kubeEtcd:
Expand Down
50 changes: 44 additions & 6 deletions charts/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
`app.kubernetes.io/managed-by` to `salt`, and copy any `app` and
`component` fields to the canonical `app.kubernetes.io/name` and
`app.kubernetes.io/component` fields
- Replace YAML-safe special strings (used in Helm values definitions) with the
appropriate Jinja syntax. Supports:
- "__var__(<varname>)", to replace with "{{ <varname> }}" (useful when
retrieving variables from service configuration ConfigMaps)
- "__image__(<imgname>)", to replace with
"{{ build_image_name("<imgname>", False) }}"
- "__full_image__(<imgname>)", to replace with
"{{ build_image_name("<imgname>") }}"
'''

import argparse
import io
import re
import sys
import subprocess
Expand All @@ -28,18 +37,18 @@
from yaml.representer import SafeRepresenter


START_BLOCK = '''
START_BLOCK = """
#!jinja | metalk8s_kubernetes
{{%- from "metalk8s/repo/macro.sls" import build_image_name with context %}}
{{%- from "metalk8s/repo/macro.sls" import build_image_name with context %}}
{configlines}
{{% raw %}}
'''
"""

END_BLOCK = '''
END_BLOCK = """
{% endraw %}
'''
"""


def fixup_metadata(namespace, doc):
Expand Down Expand Up @@ -117,6 +126,31 @@ def keep_doc(doc):
return True


def replace_magic_strings(rendered_yaml):
# Handle __var__
result = re.sub(
r'__var__\((?P<varname>[\w\-_]+(?:\.[\w\-_]+)*)\)',
r'{% endraw -%}{{ \g<varname> }}{%- raw %}',
rendered_yaml,
)

# Handle __image__
result = re.sub(
r'__image__\((?P<imgname>[\w\-]+)\)',
r'{% endraw -%}{{ build_image_name("\g<imgname>", False) }}{%- raw %}',
result,
)

# Handle __full_image__ (include version tag in the rendered name)
result = re.sub(
r'__full_image__\((?P<imgname>[\w\-]+)\)',
r'{% endraw -%}{{ build_image_name("\g<imgname>") }}{%- raw %}',
result,
)

return result


def main():
parser = argparse.ArgumentParser()
parser.add_argument('name', help="Denotes the name of the chart")
Expand Down Expand Up @@ -175,13 +209,17 @@ def main():
)
sys.stdout.write('\n')

stream = io.StringIO()
yaml.safe_dump_all(
(fixup(doc)
for doc in yaml.safe_load_all(template)
if keep_doc(doc)),
sys.stdout,
stream,
default_flow_style=False,
)
stream.seek(0)

sys.stdout.write(replace_magic_strings(stream.read()))

sys.stdout.write(END_BLOCK)

Expand Down

0 comments on commit 55cdbc2

Please sign in to comment.