Skip to content

Commit c574312

Browse files
committed
Make release template to work on needs
It turned out that Helmfile has never had support for release template on `needs`. This adds that, along with the new end-to-end test suite to verify helmfile template output with snapshot testing involving a real `helmfile build` command. Ref #2098
1 parent 1d70130 commit c574312

File tree

9 files changed

+123
-3
lines changed

9 files changed

+123
-3
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ jobs:
7373
- go-mod-cache-v1-
7474
- run: make check
7575
- run: make pristine
76+
- run: make -C .circleci helm
7677
- run: make test
7778

7879
# thanks to https://raw.githubusercontent.com/weaveworks/launcher/master/.circleci/config.yml

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
dist/
22
.idea/
3-
helmfile
4-
helmfile.lock
3+
/helmfile
4+
/helmfile.lock
55
/diff-yamls
66
/yamldiff
7-
test/integration/tmp
7+
test/integration/tmp$
88
vendor/
99
*.log
1010
.vagrant/

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ build-test-tools:
2323
.PHONY: build-test-tools
2424

2525
test:
26+
go build -o helmfile .
2627
go test -v ${PKGS} -cover -race -p=1
2728
.PHONY: test
2829

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/pierrec/lz4 v2.3.0+incompatible // indirect
2626
github.com/r3labs/diff v1.1.0
2727
github.com/spf13/cobra v1.1.1
28+
github.com/stretchr/testify v1.7.0 // indirect
2829
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939
2930
github.com/urfave/cli v1.22.5
3031
github.com/variantdev/chartify v0.9.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
655655
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
656656
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
657657
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
658+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
659+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
658660
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
659661
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939 h1:BhIUXV2ySTLrKgh/Hnts+QTQlIbWtomXt3LMdzME0A0=
660662
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939/go.mod h1:omGxs4/6hNjxPKUTjmaNkPzehSnNJOJN6pMEbrlYIT4=

pkg/state/release.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ func (r ReleaseSpec) ExecuteTemplateExpressions(renderer *tmpl.FileRenderer) (*R
188188
result.SetValues = append(newvals, result.SetValues...)
189189
}
190190

191+
for i, n := range result.Needs {
192+
s, err := renderer.RenderTemplateContentToBuffer([]byte(n))
193+
if err != nil {
194+
return nil, fmt.Errorf("failed executing template expressions in release \"%s\".needs[%d] = \"%s\": %v", r.Name, i, n, err)
195+
}
196+
result.Needs[i] = s.String()
197+
}
198+
191199
return result, nil
192200
}
193201

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package helmfile
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"runtime"
9+
"testing"
10+
"time"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestHelmfileTemplateWithBuildCommand(t *testing.T) {
16+
_, filename, _, _ := runtime.Caller(0)
17+
projectRoot := filepath.Join(filepath.Dir(filename), "..", "..", "..", "..")
18+
helmfileBin := filepath.Join(projectRoot, "helmfile")
19+
testdataDir := "testdata"
20+
21+
entries, err := os.ReadDir(testdataDir)
22+
require.NoError(t, err)
23+
24+
for _, e := range entries {
25+
if !e.IsDir() {
26+
t.Fatalf("Unexpected type of entry at %s", e.Name())
27+
}
28+
29+
name := e.Name()
30+
31+
t.Run(name, func(t *testing.T) {
32+
inputFile := filepath.Join(testdataDir, name, "input.yaml")
33+
34+
want, err := os.ReadFile(filepath.Join(testdataDir, name, "output.yaml"))
35+
require.NoError(t, err)
36+
37+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
38+
defer cancel()
39+
40+
cmd := exec.CommandContext(ctx, helmfileBin, "-f", inputFile, "build")
41+
got, err := cmd.CombinedOutput()
42+
if err != nil {
43+
t.Logf("%s", string(got))
44+
}
45+
require.NoError(t, err)
46+
47+
require.Equal(t, string(want), string(got))
48+
})
49+
}
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
repositories:
2+
- name: aservo
3+
url: https://aservo.github.io/charts
4+
5+
environments:
6+
default:
7+
8+
---
9+
10+
templates:
11+
defaults: &defaults
12+
name: "{{ .Environment.Name }}-{{`{{ .Release.Labels.service }}`}}"
13+
namespace: "{{ .Environment.Name }}"
14+
15+
releases:
16+
- chart: aservo/util
17+
version: 0.0.1
18+
labels:
19+
service: shared-resources
20+
<<: *defaults
21+
- chart: aservo/util
22+
version: 0.0.1
23+
labels:
24+
service: release-resources
25+
<<: *defaults
26+
needs:
27+
- "{{`{{ .Release.Namespace }}`}}-shared-resources"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
# Source: input.yaml
3+
4+
filepath: input.yaml
5+
helmBinary: helm
6+
environments:
7+
default: {}
8+
repositories:
9+
- name: aservo
10+
url: https://aservo.github.io/charts
11+
releases:
12+
- chart: aservo/util
13+
version: 0.0.1
14+
name: default-shared-resources
15+
namespace: default
16+
labels:
17+
service: shared-resources
18+
- chart: aservo/util
19+
version: 0.0.1
20+
needs:
21+
- default-shared-resources
22+
name: default-release-resources
23+
namespace: default
24+
labels:
25+
service: release-resources
26+
templates:
27+
defaults:
28+
name: default-{{ .Release.Labels.service }}
29+
namespace: default
30+
renderedvalues: {}

0 commit comments

Comments
 (0)