feat(gator): add compile subcommand for ConstraintTemplate source files#4641
feat(gator): add compile subcommand for ConstraintTemplate source files#4641venkatapgummadi wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new gator compile subcommand that renders ConstraintTemplate manifests from separate policy source files, matching the gatekeeper-library authoring layout (separate src.rego/src.cel files embedded into constraint.tmpl). This addresses issues #4250 and #4329, which requested a way to keep Rego/CEL separate from the template for easier maintenance and unit testing. It fits into the existing cmd/gator command suite alongside test, expand, etc.
Changes:
- Adds
pkg/gator/compilecore logic supporting three modes: rendering gomplate-stylefile.Readsnippets (--source-dir), explicit template files (--filename+--working-dir), and injecting Rego/CEL into a template scaffold (--rego/--cel). - Wires up the new
gator compilecobra command incmd/gator/compileand registers it incmd/gator/gator.go. - Adds unit tests, testdata fixtures, and documentation in
website/docs/gator.md.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
pkg/gator/compile/compile.go |
Core compilation logic; the scaffold-injection path drops unmodeled YAML fields (critical). |
pkg/gator/compile/compile_test.go |
Unit tests; scaffold test does not assert field preservation, so the data-loss bug is uncaught. |
cmd/gator/compile/compile.go |
New cobra command wiring and flags; consistent with existing subcommands. |
cmd/gator/gator.go |
Registers the new compile command. |
pkg/gator/compile/testdata/... |
Fixtures for gomplate-rendering and scaffold-injection tests. |
website/docs/gator.md |
Documents the new gator compile subcommand and its flags. |
The key concern is in the --filename + --rego/--cel injection path: it serializes a partial typed struct, which silently drops metadata and spec.crd, producing an invalid ConstraintTemplate, and validation does not detect it.
| var buf bytes.Buffer | ||
| encoder := yaml.NewEncoder(&buf) | ||
| encoder.SetIndent(2) | ||
| if err := encoder.Encode(&doc); err != nil { | ||
| return "", fmt.Errorf("marshaling compiled template: %w", err) | ||
| } |
| if !strings.Contains(output, "package scaffoldpolicy") { | ||
| t.Fatalf("expected injected rego in output, got:\n%s", output) | ||
| } |
Signed-off-by: Venkata Pavan Kumar Gummadi <venkata.p.gummadi@ieee.org>
Signed-off-by: Venkata Pavan Kumar Gummadi <venkata.p.gummadi@ieee.org>
3590b8c to
a2c12be
Compare
Closes #4250.
Summary
Adds
gator compileto renderConstraintTemplatemanifests from separate policy source files, matching the gatekeeper-library authoring layout.--source-dircompilesconstraint.tmplwith gomplate-stylefile.Readsnippets--filename+--regoinject Rego into a template scaffold--working-dirresolvesfile.Readpaths (auto-inferred fromsrc/layout when omitted)-owrites compiled output to a filepkg/gator/compilewebsite/docs/gator.mdRelated: #4329
Test plan
go test ./pkg/gator/compile/... -vgo build -o gator.exe ./cmd/gatorgator compileagainst gatekeeper-libraryrequiredlabels(Rego + CEL embedded correctly)