From 244d1affa6bde8ee6e13a2f37155baca062e13f4 Mon Sep 17 00:00:00 2001 From: Marie Roque Date: Fri, 22 Sep 2023 13:17:04 +0200 Subject: [PATCH] Add hasPrefix function to rendering template --- files/templates/alertmanager/notification-template.tmpl | 2 +- pkg/template/template.go | 2 ++ .../test/notification-template/case-1-awsconfig.golden | 2 +- .../test/notification-template/case-2-azureconfig.golden | 2 +- .../test/notification-template/case-3-kvmconfig.golden | 2 +- .../test/notification-template/case-4-control-plane.golden | 2 +- .../notification-template/case-5-cluster-api-v1alpha3.golden | 2 +- .../test/notification-template/case-6-cluster-api-eks.golden | 2 +- 8 files changed, 9 insertions(+), 7 deletions(-) diff --git a/files/templates/alertmanager/notification-template.tmpl b/files/templates/alertmanager/notification-template.tmpl index a45d56b99..29a868a0e 100644 --- a/files/templates/alertmanager/notification-template.tmpl +++ b/files/templates/alertmanager/notification-template.tmpl @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}[[ .GrafanaAddress ]]/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}[[ .GrafanaAddress ]]/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }} diff --git a/pkg/template/template.go b/pkg/template/template.go index 48c8f77a6..5065aac8e 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -25,6 +25,8 @@ func RenderTemplate(templateData interface{}, templateLocation string) ([]byte, return buf.String(), nil } + funcMap["hasPrefix"] = strings.HasPrefix + tpl, err := tpl.Funcs(sprig.FuncMap()).Funcs(funcMap).ParseGlob(templateLocation) if err != nil { return nil, microerror.Mask(err) diff --git a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-1-awsconfig.golden b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-1-awsconfig.golden index 12482b656..7e8e54c13 100644 --- a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-1-awsconfig.golden +++ b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-1-awsconfig.golden @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }} diff --git a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-2-azureconfig.golden b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-2-azureconfig.golden index 12482b656..7e8e54c13 100644 --- a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-2-azureconfig.golden +++ b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-2-azureconfig.golden @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }} diff --git a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-3-kvmconfig.golden b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-3-kvmconfig.golden index 12482b656..7e8e54c13 100644 --- a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-3-kvmconfig.golden +++ b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-3-kvmconfig.golden @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }} diff --git a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-4-control-plane.golden b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-4-control-plane.golden index 12482b656..7e8e54c13 100644 --- a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-4-control-plane.golden +++ b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-4-control-plane.golden @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }} diff --git a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-5-cluster-api-v1alpha3.golden b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-5-cluster-api-v1alpha3.golden index 12482b656..7e8e54c13 100644 --- a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-5-cluster-api-v1alpha3.golden +++ b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-5-cluster-api-v1alpha3.golden @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }} diff --git a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-6-cluster-api-eks.golden b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-6-cluster-api-eks.golden index 12482b656..7e8e54c13 100644 --- a/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-6-cluster-api-eks.golden +++ b/service/controller/resource/alerting/alertmanagerconfig/test/notification-template/case-6-cluster-api-eks.golden @@ -1,6 +1,6 @@ {{ define "__alertmanager" }}Alertmanager{{ end }} {{ define "__alertmanagerurl" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}&silenced=false&inhibited=false&active=true&filter=%7Balertname%3D%22{{ .CommonLabels.alertname }}%22%7D{{ end }} -{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" "(index .Alerts 0).Annotations.dashboard" }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} +{{ define "__dashboardurl" -}}{{ if hasPrefix "https://" (index .Alerts 0).Annotations.dashboard }}{{ (index .Alerts 0).Annotations.dashboard }}{{ else }}https://grafana/d/{{ (index .Alerts 0).Annotations.dashboard }}{{ end }}{{- end }} {{ define "__runbookurl" -}}https://intranet.giantswarm.io/docs/support-and-ops/ops-recipes/{{ (index .Alerts 0).Annotations.opsrecipe }}{{- end }} {{ define "slack.default.title" }}{{ .Status | toUpper }}[{{ if eq .Status "firing" }}{{ .Alerts.Firing | len }}{{- else }}{{ .Alerts.Resolved | len }}{{- end }}] {{ (index .Alerts 0).Labels.alertname }} - Team {{ (index .Alerts 0).Labels.team }}{{ end }}