Skip to content

Conversation

@fabian4
Copy link
Contributor

@fabian4 fabian4 commented Dec 13, 2025

Problem: Users need a mechanism to inject raw NGINX configuration snippets into the generated configuration to leverage advanced NGINX features or directives that are not yet exposed via the Gateway API or existing NGINX Gateway Fabric policies.

Solution: Implemented the SnippetsPolicy Custom Resource Definition (CRD).

  • API: The policy targets a Gateway and supports main, http, and server contexts.
  • Validation: Added validation logic to enforce a maximum snippet size (2KB), ensure unique contexts per policy, and disallow unsupported contexts (e.g., http.server.location).
  • Generation: The generator creates dedicated include files for each policy (e.g., includes/policy/<gateway>/SnippetsPolicy_main_...conf), ensuring clean separation of injected config.
  • Integration: Wired the new policy into the ChangeProcessor, Controller Manager, and Helm charts.

Testing:

  • Added unit tests for the Validator, Generator, and ChangeProcessor logic.
  • Added a new integration test suite (tests/suite/snippets_policy_test.go) covering:
    • Valid snippets in all supported contexts.
    • Invalid context validation.
    • Duplicate context validation.
  • Verified Helm chart generation.

Please focus on (optional): The validation logic in internal/controller/nginx/config/policies/snippetspolicy/validator.go, specifically the size limits and context checks.

Closes #4071

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

Release notes

 Added support for `SnippetsPolicy`, allowing users to inject custom NGINX configuration snippets into the `main`, `http`, and `server` contexts.

@nginx-bot
Copy link

nginx-bot bot commented Dec 13, 2025

Hi @fabian4!

Thanks for opening this pull request!
Be sure to check out our Contributing Guidelines while you wait for someone on the team to review this.

@nginx-bot nginx-bot bot added the community label Dec 13, 2025
@github-actions github-actions bot added helm-chart Relates to helm chart documentation Improvements or additions to documentation tests Pull requests that update tests labels Dec 14, 2025
@fabian4 fabian4 force-pushed the add_snippestspolicy branch from ebe3381 to 8e5feb3 Compare December 14, 2025 13:37
@github-actions github-actions bot removed the tests Pull requests that update tests label Dec 14, 2025
@fabian4 fabian4 force-pushed the add_snippestspolicy branch from 8e5feb3 to 63897e7 Compare December 14, 2025 13:52
@github-actions github-actions bot added the tests Pull requests that update tests label Dec 14, 2025
@fabian4 fabian4 marked this pull request as ready for review December 14, 2025 14:14
@fabian4 fabian4 requested a review from a team as a code owner December 14, 2025 14:14
Copy link
Collaborator

@sjberman sjberman left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on! It's not easy work.

// +kubebuilder:validation:XValidation:message="TargetRef Kind must be Gateway",rule="self.kind == 'Gateway'"
// +kubebuilder:validation:XValidation:message="TargetRef Group must be gateway.networking.k8s.io",rule="self.group == 'gateway.networking.k8s.io'"
//nolint:lll
TargetRef SnippetsPolicyTargetRef `json:"targetRef"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per our SnippetsPolicy design, we should be able to support multiple TargetRefs. We can also just use LocalPolicyTargetReference, no need to alias.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we also need to decide if we only want this to target Gateways, or also HTTP/GRPCRoutes. In our initial design, we proposed targeting all of these. However, I'm not convinced yet if we need it at the Route level. This could be enhanced in the future.

Based on the design here, this is a direct policy regardless, so we need the // +kubebuilder:metadata:labels="gateway.networking.k8s.io/policy=direct" label at the top level of the SnippetsPolicy struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Allowing SnippetsPolicy to attach directly to Routes may overlap with existing mechanisms such as SnippetsFilter, introducing ambiguity around ownership and precedence. In the Gateway API model, Route behavior is typically defined within the Route, while Gateway-level policies focus on shared concerns. This separation feels clearer, though feedback is welcome.

@fabian4 fabian4 requested a review from sjberman December 16, 2025 13:30
@fabian4 fabian4 force-pushed the add_snippestspolicy branch from e11ed2f to a155e01 Compare December 18, 2025 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community documentation Improvements or additions to documentation helm-chart Relates to helm chart tests Pull requests that update tests

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

Add support for Snippets at the Gateway level

3 participants