[template]: Add hostPort/serverName templating#53
Open
pseudomuto wants to merge 1 commit into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #53 +/- ##
==========================================
+ Coverage 85.12% 85.91% +0.79%
==========================================
Files 33 34 +1
Lines 1042 1065 +23
==========================================
+ Hits 887 915 +28
+ Misses 131 128 -3
+ Partials 24 22 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds a small internal templating helper to support request-scoped rendering of upstream hostPort / serverName fields (and routing-time templating) while keeping templates bound to the specific context available at each phase.
Changes:
- Introduces
internal/templatewith typedRoutingContextandUpstreamContextplus a generic compiledTemplate[T]. - Adds parse-time validation by probing templates during
ParseRouting/ParseUpstream, and provides aMusthelper for tests/package-level vars. - Adds unit tests and package documentation describing the supported fields and scoping rules.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| internal/template/template.go | Implements typed template parsing/rendering and probe-based validation. |
| internal/template/template_test.go | Adds coverage for upstream vs routing context rendering and validation behavior. |
| internal/template/doc.go | Documents template usage, contexts, and scoping expectations. |
Upstream definitions (RFC #32) template their hostPort and serverName with request-derived values so one upstream can serve many namespaces, e.g. "{{ .RemoteNamespace }}.acme-cloud.tmprl.cloud:7233". This adds internal/template, a thin wrapper over text/template that compiles a template once and renders it against a per-request context. Templates are bound to the context they render against. ParseRouting renders against a RoutingContext (local namespace and metadata), used while routing rules pick an upstream; ParseUpstream adds RemoteNamespace, which is only known after an upstream is selected since translation is per-upstream. Binding the type means a routing template that references RemoteNamespace fails at parse time rather than silently rendering empty. Parsing validates by rendering against a probe context, so an unknown field (a typo) is caught at config-load. Absent metadata keys render empty rather than erroring, since the available keys are only known at request time. Must wraps a Parse call for package-level vars and tests.
a3791cd to
a190449
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Upstream definitions (RFC #32) template their hostPort and serverName with request-derived values so one upstream can serve many namespaces, e.g. "{{ .RemoteNamespace }}.acme-cloud.tmprl.cloud:7233". This adds internal/template, a thin wrapper over text/template that compiles a template once and renders it against a per-request context.
Templates are bound to the context they render against. ParseRouting renders against a RoutingContext (local namespace and metadata), used while routing rules pick an upstream; ParseUpstream adds RemoteNamespace, which is only known after an upstream is selected since translation is per-upstream. Binding the type means a routing template that references RemoteNamespace fails at parse time rather than silently rendering empty.
Parsing validates by rendering against a probe context, so an unknown field (a typo) is caught at config-load. Absent metadata keys render empty rather than erroring, since the available keys are only known at request time. Must wraps a Parse call for package-level vars and tests.