[PROPOSAL] Migrate template string constants to embedded files using embed.FS
#5365
Replies: 2 comments 1 reply
-
|
Thanks a lot for the proposal — I appreciate the intent (better DX, cleaner diffs, easier editing). After looking at how Kubebuilder scaffolding works today (Machinery + plugins), I’m not in favor of moving templates to Why this is not just “template files”Kubebuilder scaffolding is built on the Machinery library. In practice we don’t only “render a file” — we run a pipeline:
Key point: Kubebuilder scaffolding is a combination of:
That coupling is why Kubebuilder has stayed maintainable and extensible. Why
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the detailed feedback! I'll close this proposal and shift my focus to contributing in areas that better align with the roadmap, like the AI workflows or CLI improvements you mentioned. Thanks again for the class on Machinery! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Note
The
go:embedfeature was suggested in #2067, but then the following thread took another direction and this enhancement seems to never have been properly discussed.Summary
Migrate Kubebuilder's scaffolding templates from inline Go string constants to separate template files using Go's
embedpackage. This improves maintainability, enables better tooling, and lowers the barrier for community contributions.Motivation
Current State
Templates are currently defined as large string constants within Go files:
Problems
Goals
Proposal
Embedded Filesystem with Helper Functions
All templates will be embedded into the binary using Go's
embed.FSand accessed through a centralized loader with helper functions. This provides a clean separation between template content and Go logic while maintaining the single-binary distribution model.Architecture
Template Loader (
loader.go)A single package-level
embed.FSvariable holds all templates, with helper functions providing convenient access:Usage Patterns
For templates that are always used exactly as-is:
For templates that need to be assembled from multiple parts based on configuration:
Key Design Decisions
All template access goes through a single loader package. This provides:
Templates are organized in subdirectories by domain:
api/- API-related templates (types, groups, conversions)webhooks/- Webhook-related templatescontrollers/- Controller-related templatesconfig/- Configuration templates (kustomize, RBAC, etc.)This mirrors the project structure and makes templates easy to locate.
All templates use
.tmplextension:Directory Structure
Benefits
Non-Go developers can now contribute template improvements:
Template content lives in dedicated
.tmplfiles with proper file extensions:Dockerfilesget full Docker syntax highlightingMakefilesget Makefile syntax highlightingYAMLfiles get YAML validation.gofiles focused purely on scaffolding behavior.Adding new templates is straightforward:
Conclusion
Migrating to
go:embedimproves Kubebuilder's maintainability, testability, and accessibility to contributors.The implementation is straightforward, well-supported by Go's standard library, and aligns with Go best practices.
References
Go embed package documentation
Go 1.16 Release Notes (embed feature)
Discussion
Please share your thoughts, concerns, or suggestions.
0 votes ·
Beta Was this translation helpful? Give feedback.
All reactions