Skip to content

feat(gator): add compile subcommand for ConstraintTemplate source files#4641

Open
venkatapgummadi wants to merge 2 commits into
open-policy-agent:masterfrom
venkatapgummadi:feat/gator-compile-4250
Open

feat(gator): add compile subcommand for ConstraintTemplate source files#4641
venkatapgummadi wants to merge 2 commits into
open-policy-agent:masterfrom
venkatapgummadi:feat/gator-compile-4250

Conversation

@venkatapgummadi

Copy link
Copy Markdown

Closes #4250.

Summary

Adds gator compile to render ConstraintTemplate manifests from separate policy source files, matching the gatekeeper-library authoring layout.

  • --source-dir compiles constraint.tmpl with gomplate-style file.Read snippets
  • --filename + --rego inject Rego into a template scaffold
  • --working-dir resolves file.Read paths (auto-inferred from src/ layout when omitted)
  • -o writes compiled output to a file
  • Unit tests in pkg/gator/compile
  • Docs in website/docs/gator.md

Related: #4329

Test plan

  • go test ./pkg/gator/compile/... -v
  • go build -o gator.exe ./cmd/gator
  • gator compile against gatekeeper-library requiredlabels (Rego + CEL embedded correctly)

Copilot AI review requested due to automatic review settings June 24, 2026 21:57
@venkatapgummadi venkatapgummadi requested a review from a team as a code owner June 24, 2026 21:57
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 24, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: venkatapgummadi / name: Venkata Pavan Kumar Gummadi (3590b8c, 928535d)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/compile core logic supporting three modes: rendering gomplate-style file.Read snippets (--source-dir), explicit template files (--filename + --working-dir), and injecting Rego/CEL into a template scaffold (--rego/--cel).
  • Wires up the new gator compile cobra command in cmd/gator/compile and registers it in cmd/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.

Comment on lines +280 to +285
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)
}
Comment on lines +67 to +69
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>
@venkatapgummadi venkatapgummadi force-pushed the feat/gator-compile-4250 branch from 3590b8c to a2c12be Compare June 28, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gator compile to compile constrainttemplates from template.yaml and rego file

2 participants