Add Helm-compatible include template function#282
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository: openshift/coderabbit/.coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (12)
🚧 Files skipped from review as they are similar to previous changes (9)
WalkthroughChangesThe PR adds Helm-like late-bound Template Include Support
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant TemplateParser
participant InitInclude
participant includeFun
participant NamedTemplate
TemplateParser->>InitInclude: initialize parsed template
InitInclude->>includeFun: bind include function
includeFun->>NamedTemplate: execute referenced template
NamedTemplate-->>includeFun: return rendered content or error
Suggested reviewers: 🚥 Pre-merge checks | ✅ 14 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (14 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/docs/product/migration-guide for migration instructions Comment |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/reference-config-guide-v1.md`:
- Line 87: Update the Helm compatibility statement near “behavior” in the
documentation to limit the claim to the implemented Helm-style helpers: include,
toYaml, toJson, fromYaml, fromJson, lookupCR, lookupCRs, and doNotMatch, instead
of claiming support for all custom Helm functions.
In `@docs/reference-config-guide-v2.md`:
- Around line 285-287: Align the documented recursion limit with the guard in
funcmap.go: either change the recursionMaxNums comparison to enforce a true
maximum of 1000 active includes, or update this guide and its related tests to
state and verify the current 1001-include behavior. Keep the documentation,
implementation, and tests consistent.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 0fa5b6e5-1eec-44c9-85e6-8f40be6028e0
📒 Files selected for processing (2)
docs/reference-config-guide-v1.mddocs/reference-config-guide-v2.md
Add the "include" template function, matching Helm's semantics.
Unlike Go's built-in {{template}} action which writes directly to
the output, {{include}} executes a named template and returns its
result as a string. This allows the output to be piped through other
template functions like indent, upper, nindent, etc.
The implementation follows Helm's approach:
- A placeholder include is registered in FuncMap() so templates parse
without error
- After parsing, InitInclude() binds the real implementation as a
closure over the parsed *template.Template
- A recursion guard prevents infinite loops from templates that
include themselves
Example usage in reference templates:
{{- define "mychart.labels" -}}
app: my-app
chart: my-chart
{{- end -}}
metadata:
labels:
{{ include "mychart.labels" . | indent 4 }}
Assisted-by: Claude Opus 4.6 and pi.dev
Signed-off-by: Jim Ramsay <jramsay@redhat.com>
534afab to
fac4630
Compare
|
@lack: all tests passed! Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
What
Add the
includetemplate function, matching Helm's semantics. Unlike Go's built-in{{template}}action which writes directly to the output,{{include}}executes a named template and returns its result as a string. This allows the output to be piped through other template functions likeindent,upper,nindent, etc.Why
The
includefunction is one of the most commonly used Helm template functions. Reference authors coming from Helm expect it to be available, and it enables cleaner template composition — defining reusable snippets (e.g. standard labels, annotations) in_helpers.tplfiles and including them with post-processing.Without
include, authors are limited to Go's{{template}}which cannot be piped:How
The implementation follows the same pattern Helm uses in
pkg/engine/engine.go:includeis registered inFuncMap()so templates can parse without errorInitInclude()late-binds the real implementation as a closure over the parsed*template.Template, enabling it to callExecuteTemplate()The function was not imported directly from Helm's engine package because that would pull in the entire chart rendering pipeline (chart loading, Kubernetes client, values handling, etc.) as a dependency. The actual
includeimplementation is ~25 lines.Testing
funcmap_include_test.go): basic include, data passing, piping throughupper/indent, nested includes, missing template error, recursion guardTestCompareRun/Include_Function): end-to-end test with a_helpers.tpldefining labels and a ConfigMap template using{{ include "mychart.labels" . | indent 4 }}Summary by CodeRabbit
includesupport for templates, enabling named partial reuse, passing data context, and using included output with built-in template functions.include, including usage examples and the recursion-guard behavior.