alertmanager icon indicating copy to clipboard operation
alertmanager copied to clipboard

Add support for custom Go template delimiters

Open siavashs opened this issue 2 years ago • 4 comments

Alertmanager uses Go templates in different configuration parameters. Many users generate the configuration using tools like Helm, ExternalSecret, etc. Most of these tools are written in Go, and therfore use Go templates. This creates problems when the configuration tool needs to skip the Alertmanager templates. As a result users usually have to add escaping to the templates:

{{ `{{ ... }}` }}

This change allows setting the template delimiters used in Alertmanager configuration. This is already configurable in Go templates using Delims() functions for both text and html. The newly added global configuration parameters allows users to set the left and right delimiters used in Alertmanager templates and therfore avoid conflict with other configuration management tools:

template_config:
  delim_left: "[["
  delim_right: "]]"

This change is backward compatible as empty delimiter parameters stands for the corresponding default: {{ or }}. The builtin templates are not affected by this change as custom delimiters are only applied to:

  • <tmpl_string>
  • <tmpl_secret>
  • custom templates loaded by templates: []

siavashs avatar May 04 '22 07:05 siavashs

I was planning a similar change for Prometheus but usually we don't use any secrets there and most of the scrape config is generated by the operator and ServiceMonitors, etc.

Also there is a similar change in this PR for Helm https://github.com/helm/helm/pull/10299

siavashs avatar May 04 '22 07:05 siavashs

Let's change from

global:
  template_config:
    delim_left: "[["
    delim_right: "]]"

to

template_config:
  delim_left: "[["
  delim_right: "]]"

I am concerned because this will break all built-in templates.

roidelapluie avatar May 06 '22 12:05 roidelapluie

I am concerned because this will break all built-in templates.

Ouch, I missed the internal templates. I'll try to find a solution for that. If you have any suggestions please let me know.

siavashs avatar May 06 '22 12:05 siavashs

I just did a test and we can keep using {{ }} for default templates and custom delimiters for other templates https://go.dev/play/p/l_wElt_dYfw

I'll update the PR accordingly to make sure we only apply the configured delimiters to non-default templates.

siavashs avatar May 06 '22 13:05 siavashs

Closing this as there was not enough interest from the community and maintainers.

siavashs avatar Jun 14 '23 13:06 siavashs