Skip to content

Commit 1180608

Browse files
adityathebemoshloop
authored andcommitted
fix: slack template functions in notification playbook action
1 parent e9af47f commit 1180608

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ junit-report.xml
2121
nohup.out
2222
.envrc
2323
.creds
24-
24+
msg.tpl
25+
.windsurfrules
2526
test-reports
26-
msg.tpl
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
apiVersion: mission-control.flanksource.com/v1
3+
kind: Playbook
4+
metadata:
5+
name: backup-postgres
6+
spec:
7+
actions:
8+
- name: backup-postgres
9+
exec:
10+
env:
11+
- name: CONN_STRING
12+
valueFrom:
13+
secretKeyRef:
14+
name: flanksource_postgres
15+
key: DB_URL
16+
script: pg_dump --dbname "$CONN_STRING" -F c -b -v -f /mnt/backup/postgres.dump
17+
- name: notify
18+
if: 'failure()'
19+
notification:
20+
title: "Postgres backup failed"
21+
message: |
22+
{
23+
"blocks": [
24+
{{slackSectionTextMD (printf `:rotating_light: *Postgres backup failed*`)}},
25+
{{slackSectionTextMD (printf "*Error:* %s" getLastAction.error)}},
26+
{{slackURLAction "Report" "https://console.flanksource.com/mission-control/playbooks/postgres-backup"}}
27+
]
28+
}
29+
connection: connection://mc/flanksource-slack

playbook/runner/template.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/samber/oops"
1515

1616
v1 "github.com/flanksource/incident-commander/api/v1"
17+
"github.com/flanksource/incident-commander/notification"
1718
"github.com/flanksource/incident-commander/playbook/actions"
1819
)
1920

@@ -204,16 +205,17 @@ func TemplateEnv(ctx context.Context, env actions.TemplateEnv, template string)
204205

205206
// TemplateAction all the go templates in the action
206207
func TemplateAction(ctx context.Context, actionSpec *v1.PlaybookAction, env actions.TemplateEnv) error {
207-
templater := ctx.NewStructTemplater(env.AsMap(ctx), "template", getGomplateFuncs(ctx, env))
208+
templateFuncs := collections.MergeMap(getGomplateFuncs(ctx, env), notification.TemplateFuncs)
209+
templater := ctx.NewStructTemplater(env.AsMap(ctx), "template", templateFuncs)
208210
if err := templater.Walk(&actionSpec); err != nil {
209-
return err
211+
return ctx.Oops().Wrapf(err, "failed to template action")
210212
}
211213

212214
// TODO: make this work with template.Walk()
213215
if actionSpec.Exec != nil && actionSpec.Exec.Connections.FromConfigItem != nil {
214-
if v, err := ctx.NewStructTemplater(env.AsMap(ctx), "", getGomplateFuncs(ctx, env)).
216+
if v, err := ctx.NewStructTemplater(env.AsMap(ctx), "", templateFuncs).
215217
Template(*actionSpec.Exec.Connections.FromConfigItem); err != nil {
216-
return err
218+
return ctx.Oops().Wrapf(err, "failed to template exec action connection from config item")
217219
} else {
218220
actionSpec.Exec.Connections.FromConfigItem = &v
219221
}

0 commit comments

Comments
 (0)