Skip to content

Commit 2881d54

Browse files
authored
Merge pull request #217 from cozystack/fix/template-no-templates-guard
fix(engine): require a template before rendering the chart
2 parents cb340ab + d320cc6 commit 2881d54

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

pkg/engine/contract_render_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ func TestContract_Render_NoTemplateFilesError(t *testing.T) {
5656
}
5757
}
5858

59+
// Contract: the empty-TemplateFiles guard fires BEFORE any chart
60+
// rendering. When no template is requested, talm must surface the
61+
// actionable "templates are not set" error without first rendering the
62+
// chart — otherwise a render-time failure (here a `fail` directive,
63+
// online a live `lookup` against an unreachable node) masks the real
64+
// problem with a misleading error. The chart body below errors if it is
65+
// ever executed; the guard must short-circuit before that happens.
66+
func TestContract_Render_NoTemplateFilesShortCircuitsBeforeRender(t *testing.T) {
67+
chartRoot := createTestChart(t, "tc", "config.yaml",
68+
`{{ fail "chart body must not render when no templates are requested" }}`)
69+
_, err := Render(context.Background(), nil, Options{
70+
Offline: true,
71+
Root: chartRoot,
72+
// TemplateFiles intentionally empty
73+
})
74+
if err == nil {
75+
t.Fatal("expected error for empty TemplateFiles")
76+
}
77+
msg := err.Error()
78+
if strings.Contains(msg, "chart body must not render") {
79+
t.Errorf("chart was rendered before the no-templates guard fired; the guard must run first. got: %s", msg)
80+
}
81+
if !strings.Contains(msg, "templates are not set") {
82+
t.Errorf("expected the 'templates are not set' guard error, got: %s", msg)
83+
}
84+
}
85+
5986
// Contract: Render with a TemplateFiles entry that does not exist in
6087
// the chart surfaces an error naming the missing template. Operators
6188
// hit this when they typo a path or when a template file is renamed

pkg/engine/engine.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,17 @@ func Render(ctx context.Context, c *client.Client, opts Options) ([]byte, error)
16021602
helmEngine.LookupFunc = newLookupFunction(ctx, c, cmdName, opts.TalosEndpoints)
16031603
}
16041604

1605+
// Require at least one template before loading and rendering the chart.
1606+
// Rendering it first would be wasted (the output is discarded when no
1607+
// template is selected) and, worse, the render fires live lookups whose
1608+
// "connection refused" then masks the real problem: the operator forgot
1609+
// --file / --template. Fail fast with the actionable error instead. This
1610+
// runs after the online multi-node guard, which is a cheaper precondition
1611+
// with no network I/O.
1612+
if len(opts.TemplateFiles) == 0 {
1613+
return nil, errors.New("templates are not set for the command: please use `--file` or `--template` flag")
1614+
}
1615+
16051616
chartPath, err := os.Getwd()
16061617
if err != nil {
16071618
return nil, errors.Wrap(err, "resolving working directory")
@@ -1633,10 +1644,6 @@ func Render(ctx context.Context, c *client.Client, opts Options) ([]byte, error)
16331644
return nil, errors.Wrap(err, "rendering chart")
16341645
}
16351646

1636-
if len(opts.TemplateFiles) == 0 {
1637-
return nil, errors.New("templates are not set for the command: please use `--file` or `--template` flag")
1638-
}
1639-
16401647
configPatches := []string{}
16411648

16421649
for _, templateFile := range opts.TemplateFiles {

0 commit comments

Comments
 (0)