sensu-go
sensu-go copied to clipboard
Add text/template support to core/v2.Handler "commands"
Feature Suggestion
Parse text/template
templates for the core/v2.Handler command
field. I haven't considered whether it might also be useful to support templates in other Handler fields.
Possible Implementation
Just do it ™
In practice, Handler templates would be evaluated just prior to command execution using the incoming Sensu event as the source data.
Context
The Sensu Plugin SDK added support for handler templating in April 2019. This functionality has been fully documented since September 2020[^1].
With a plugin that supports templating (e.g. the Sensu Pagerduty Handler), it is possible to do the following:
spec:
type: pipe
command: >-
sensu-pagerduty-handler
--dedup-key-template "{{.Entity.Namespace}}-{{.Entity.Name}}-{{.Check.Name}}"
--status-map "{\"info\":[0],\"warning\": [1],\"critical\": [2],\"error\": [3,127]}"
--summary-template "[{{.Entity.Namespace}}] {{.Entity.Name}}/{{.Check.Name}}: {{.Check.State}}"
--details-template "{{.Check.Output}}\n\n{{.Check}}"
However, the templates are limited to command flags which have enabled support for templating (via the sensu-plugin-sdk).
So in the following two examples using a text/template
conditional statement, only the first example works:
spec:
type: pipe
command: >-
sensu-slack-handler
--channel ${MATTERMOST_CHANNEL}
--username SensuGo
--webhook-url ${MATTERMOST_WEBHOOK_URL}
--description-template '{{ if (index .Check.Annotations "sensu.io/notifications/warning") }}{{ index .Check.Annotations "sensu.io/notifications/warning" }}\n\n[namespace: {{ .Entity.Namespace }}]{{ else }}{{ .Check.Output }}\n\n[namespace: {{ .Entity.Namespace }}]{{ end }}'
spec:
type: pipe
command: >-
sensu-slack-handler
--channel ${MATTERMOST_CHANNEL}
--username SensuGo
--webhook-url ${MATTERMOST_WEBHOOK_URL}
{{ if (index .Check.Annotations "sensu.io/notifications/warning") }}
--description-template {{ index .Check.Annotations "sensu.io/notifications/warning" }}\n\n[namespace: {{ .Entity.Namespace }}]
{{ else }}
--description-template {{ .Check.Output }}\n\n[namespace: {{ .Entity.Namespace }}]
{{ end }}
What's interesting is that, if we were to natively support text/template
for core/v2.Handler commands, we could effectively drop support for handler templates from the plugin SDK and both of these examples would "just work" ™.
[^1]: this documentation also helped clarify that Handler Templates are distinct from Sensu Tokens.