Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Commit bc3d7b2

Browse files
committed
Add support for the sprig functions library
All functions are not supported, The following are discarded: // Date functions "date", "date_in_zone", "date_modify", "now", "htmlDate", "htmlDateInZone", "dateInZone", "dateModify", // Strings "randAlphaNum", "randAlpha", "randAscii", "randNumeric", "uuidv4", // OS "env", "expandenv", // Network "getHostByName", Signed-off-by: Soule BA <[email protected]>
1 parent b8f6d3d commit bc3d7b2

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

controllers/imageupdateautomation_controller.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"text/template"
3030
"time"
3131

32+
"github.com/Masterminds/sprig/v3"
33+
3234
gogit "github.com/go-git/go-git/v5"
3335
libgit2 "github.com/libgit2/git2go/v31"
3436

@@ -291,17 +293,9 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
291293
}
292294

293295
// construct the commit message from template and values
294-
msgTmpl := gitSpec.Commit.MessageTemplate
295-
if msgTmpl == "" {
296-
msgTmpl = defaultMessageTemplate
297-
}
298-
tmpl, err := template.New("commit message").Parse(msgTmpl)
296+
message, err := templateMsg(gitSpec.Commit.MessageTemplate, &templateValues)
299297
if err != nil {
300-
return failWithError(fmt.Errorf("unable to create commit message template from spec: %w", err))
301-
}
302-
messageBuf := &strings.Builder{}
303-
if err := tmpl.Execute(messageBuf, templateValues); err != nil {
304-
return failWithError(fmt.Errorf("failed to run template from spec: %w", err))
298+
return failWithError(err)
305299
}
306300

307301
// The status message depends on what happens next. Since there's
@@ -313,7 +307,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
313307
When: time.Now(),
314308
}
315309

316-
if rev, err := commitChangedManifests(tracelog, repo, tmp, signingEntity, author, messageBuf.String()); err != nil {
310+
if rev, err := commitChangedManifests(tracelog, repo, tmp, signingEntity, author, message); err != nil {
317311
if err == errNoChanges {
318312
r.event(ctx, auto, events.EventSeverityInfo, "no updates made")
319313
debuglog.Info("no changes made in working directory; no commit")
@@ -784,3 +778,24 @@ func (r *ImageUpdateAutomationReconciler) recordSuspension(ctx context.Context,
784778
r.MetricsRecorder.RecordSuspend(*objRef, auto.Spec.Suspend)
785779
}
786780
}
781+
782+
// templateMsg renders a msg template, returning the message or an error.
783+
func templateMsg(messageTemplate string, templateValues *TemplateData) (string, error) {
784+
if messageTemplate == "" {
785+
messageTemplate = defaultMessageTemplate
786+
}
787+
788+
// Includes only functions that are guaranteed to always evaluate to the same result for given input.
789+
// This removes the possibility of accidentally relying on where or when the template runs.
790+
// https://github.com/Masterminds/sprig/blob/3ac42c7bc5e4be6aa534e036fb19dde4a996da2e/functions.go#L70
791+
t, err := template.New("commit message").Funcs(sprig.HermeticTxtFuncMap()).Parse(messageTemplate)
792+
if err != nil {
793+
return "", fmt.Errorf("unable to create commit message template from spec: %w", err)
794+
}
795+
796+
b := &strings.Builder{}
797+
if err := t.Execute(b, *templateValues); err != nil {
798+
return "", fmt.Errorf("failed to run template from spec: %w", err)
799+
}
800+
return b.String(), nil
801+
}

controllers/update_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,12 @@ Files:
135135
136136
Objects:
137137
{{ range $resource, $_ := .Updated.Objects -}}
138+
{{ if eq $resource.Kind "Deployment" -}}
139+
- {{ $resource.Kind | lower }} {{ $resource.Name | lower }}
140+
{{ else -}}
138141
- {{ $resource.Kind }} {{ $resource.Name }}
139142
{{ end -}}
143+
{{ end -}}
140144
141145
Images:
142146
{{ range .Updated.Images -}}
@@ -150,7 +154,7 @@ Automation: %s/update-test
150154
Files:
151155
- deploy.yaml
152156
Objects:
153-
- Deployment test
157+
- deployment test
154158
Images:
155159
- helloworld:v1.0.0 (%s)
156160
`

docs/spec/v1beta1/imageupdateautomations.md

+38
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,45 @@ spec:
315315
- {{.}}
316316
{{ end -}}
317317
```
318+
#### Commit Message with Template functions
318319

320+
With template functions, it is possible to manipulate and transform the supplied data in order to generate more complex commit messages.
321+
322+
```yaml
323+
kind: ImageUpdateAutomation
324+
metadata:
325+
name: flux-system
326+
spec:
327+
git:
328+
commit:
329+
messageTemplate: |
330+
Automated image update
331+
332+
Automation name: {{ .AutomationObject }}
333+
334+
Files:
335+
{{ range $filename, $_ := .Updated.Files -}}
336+
- {{ $filename }}
337+
{{ end -}}
338+
339+
Objects:
340+
{{ range $resource, $_ := .Updated.Objects -}}
341+
- {{ $resource.Kind | lower }} {{ $resource.Name | lower }}
342+
{{ end -}}
343+
344+
Images:
345+
{{ range $image, $_ := .Updated.Images -}}
346+
{{ if contains "1.0.0" $image -}}
347+
- {{ $image }}
348+
{{ else -}}
349+
[skip ci] wrong image
350+
{{ end -}}
351+
{{ end -}}
352+
author:
353+
354+
name: fluxcdbot
355+
```
356+
There are over 70 available functions. Some of them are defined by the [Go template language](https://pkg.go.dev/text/template) itself. Most of the others are part of the [Sprig template library](http://masterminds.github.io/sprig/).
319357
### Push
320358

321359
The optional `push` field defines how commits are pushed to the origin.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
replace github.com/fluxcd/image-automation-controller/api => ./api
66

77
require (
8+
github.com/Masterminds/sprig/v3 v3.2.2
89
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7
910
github.com/cyphar/filepath-securejoin v0.2.2
1011
github.com/fluxcd/image-automation-controller/api v0.15.0

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
5858
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
5959
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
6060
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
61+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
6162
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
6263
github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
6364
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
6465
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
66+
github.com/Masterminds/sprig/v3 v3.2.2 h1:17jRggJu518dr3QaafizSXOjKYp94wKfABxUmyxvxX8=
6567
github.com/Masterminds/sprig/v3 v3.2.2/go.mod h1:UoaO7Yp8KlPnJIYWTFkMaqPUYKTfGFPhxNuwnnxkKlk=
6668
github.com/Masterminds/squirrel v1.5.0/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
6769
github.com/Masterminds/vcs v1.13.1/go.mod h1:N09YCmOQr6RLxC6UNHzuVwAdodYbbnycGHSmwVJjcKA=
@@ -618,6 +620,7 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m
618620
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
619621
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
620622
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
623+
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
621624
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
622625
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
623626
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -725,6 +728,7 @@ github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl
725728
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4=
726729
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
727730
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
731+
github.com/mitchellh/copystructure v1.1.1 h1:Bp6x9R1Wn16SIz3OfeDr0b7RnCG2OB66Y7PQyC/cvq4=
728732
github.com/mitchellh/copystructure v1.1.1/go.mod h1:EBArHfARyrSWO/+Wyr9zwEkc6XMFB9XyNgFNmRkZZU4=
729733
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
730734
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
@@ -739,6 +743,7 @@ github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxd
739743
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
740744
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
741745
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
746+
github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE=
742747
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
743748
github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc=
744749
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
@@ -924,6 +929,7 @@ github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvW
924929
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
925930
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
926931
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
932+
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
927933
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
928934
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
929935
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
@@ -946,6 +952,7 @@ github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B
946952
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
947953
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
948954
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
955+
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
949956
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
950957
github.com/spf13/cobra v0.0.2-0.20171109065643-2da4a54c5cee/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
951958
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=

0 commit comments

Comments
 (0)