Skip to content

Commit 8e5feb3

Browse files
committed
Add SnippetsPolicy support for NGINX configuration injection
1 parent 89aee48 commit 8e5feb3

35 files changed

+1683
-123
lines changed

apis/v1alpha1/policy_methods.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ func (p *UpstreamSettingsPolicy) GetPolicyStatus() gatewayv1.PolicyStatus {
3131
func (p *UpstreamSettingsPolicy) SetPolicyStatus(status gatewayv1.PolicyStatus) {
3232
p.Status = status
3333
}
34+
35+
func (p *SnippetsPolicy) GetTargetRefs() []gatewayv1.LocalPolicyTargetReference {
36+
return []gatewayv1.LocalPolicyTargetReference{p.Spec.TargetRef.LocalPolicyTargetReference}
37+
}
38+
39+
func (p *SnippetsPolicy) GetPolicyStatus() gatewayv1.PolicyStatus {
40+
return p.Status
41+
}
42+
43+
func (p *SnippetsPolicy) SetPolicyStatus(status gatewayv1.PolicyStatus) {
44+
p.Status = status
45+
}

apis/v1alpha1/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
4040
&SnippetsFilterList{},
4141
&UpstreamSettingsPolicy{},
4242
&UpstreamSettingsPolicyList{},
43+
&SnippetsPolicy{},
44+
&SnippetsPolicyList{},
4345
)
4446
// AddToGroupVersion allows the serialization of client types like ListOptions.
4547
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2025 The NGINX Gateway Fabric Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
22+
)
23+
24+
// +genclient
25+
// +kubebuilder:object:root=true
26+
// +kubebuilder:storageversion
27+
// +kubebuilder:subresource:status
28+
// +kubebuilder:resource:shortName=snipol
29+
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
30+
// +kubebuilder:printcolumn:name="Accepted",type=string,JSONPath=`.status.conditions[?(@.type=="Accepted")].status`
31+
// +kubebuilder:printcolumn:name="Reason",type=string,JSONPath=`.status.conditions[?(@.type=="Accepted")].reason`
32+
33+
// SnippetsPolicy provides a way to inject NGINX snippets into the configuration on Gateway level.
34+
type SnippetsPolicy struct {
35+
metav1.TypeMeta `json:",inline"`
36+
metav1.ObjectMeta `json:"metadata,omitempty"`
37+
38+
// Spec defines the desired state of the SnippetsPolicy.
39+
Spec SnippetsPolicySpec `json:"spec"`
40+
41+
// Status defines the current state of the SnippetsPolicy.
42+
Status gatewayv1.PolicyStatus `json:"status,omitempty"`
43+
}
44+
45+
// +kubebuilder:object:root=true
46+
47+
// SnippetsPolicyList contains a list of SnippetsPolicies.
48+
type SnippetsPolicyList struct {
49+
metav1.TypeMeta `json:",inline"`
50+
metav1.ListMeta `json:"metadata,omitempty"`
51+
Items []SnippetsPolicy `json:"items"`
52+
}
53+
54+
// SnippetsPolicySpec defines the desired state of the SnippetsPolicy.
55+
type SnippetsPolicySpec struct {
56+
// TargetRef is the reference to the Gateway that this policy should be applied to.
57+
// +kubebuilder:validation:XValidation:message="TargetRef Kind must be Gateway",rule="self.kind == 'Gateway'"
58+
// +kubebuilder:validation:XValidation:message="TargetRef Group must be gateway.networking.k8s.io",rule="self.group == 'gateway.networking.k8s.io'"
59+
//nolint:lll
60+
TargetRef SnippetsPolicyTargetRef `json:"targetRef"`
61+
62+
// Snippets is a list of snippets to be injected into the NGINX configuration.
63+
// +kubebuilder:validation:MaxItems=3
64+
// +kubebuilder:validation:XValidation:message="Only one snippet allowed per context",rule="self.all(s1, self.exists_one(s2, s1.context == s2.context))"
65+
// +kubebuilder:validation:XValidation:message="http.server.location context is not supported in SnippetsPolicy",rule="!self.exists(s, s.context == 'http.server.location')"
66+
Snippets []Snippet `json:"snippets"`
67+
}
68+
69+
// SnippetsPolicyTargetRef identifies an API object to apply the policy to.
70+
type SnippetsPolicyTargetRef struct {
71+
gatewayv1.LocalPolicyTargetReference `json:",inline"`
72+
}

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/nginx-gateway-fabric/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
246246
| `nginx.usage.resolver` | The nameserver used to resolve the NGINX Plus usage reporting endpoint. Used with NGINX Instance Manager. | string | `""` |
247247
| `nginx.usage.secretName` | The name of the Secret containing the JWT for NGINX Plus usage reporting. Must exist in the same namespace that the NGINX Gateway Fabric control plane is running in (default namespace: nginx-gateway). | string | `"nplus-license"` |
248248
| `nginx.usage.skipVerify` | Disable client verification of the NGINX Plus usage reporting server certificate. | bool | `false` |
249-
| `nginxGateway` | The nginxGateway section contains configuration for the NGINX Gateway Fabric control plane deployment. | object | `{"affinity":{},"autoscaling":{"enable":false},"config":{"logging":{"level":"info"}},"configAnnotations":{},"extraVolumeMounts":[],"extraVolumes":[],"gatewayClassAnnotations":{},"gatewayClassName":"nginx","gatewayControllerName":"gateway.nginx.org/nginx-gateway-controller","gwAPIExperimentalFeatures":{"enable":false},"gwAPIInferenceExtension":{"enable":false,"endpointPicker":{"disableTLS":false,"skipVerify":true}},"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric","tag":"edge"},"kind":"deployment","labels":{},"leaderElection":{"enable":true,"lockName":""},"lifecycle":{},"metrics":{"enable":true,"port":9113,"secure":false},"name":"","nodeSelector":{},"podAnnotations":{},"productTelemetry":{"enable":true},"readinessProbe":{"enable":true,"initialDelaySeconds":3,"port":8081},"replicas":1,"resources":{},"service":{"annotations":{},"labels":{}},"serviceAccount":{"annotations":{},"imagePullSecret":"","imagePullSecrets":[],"name":""},"snippetsFilters":{"enable":false},"terminationGracePeriodSeconds":30,"tolerations":[],"topologySpreadConstraints":[]}` |
249+
| `nginxGateway` | The nginxGateway section contains configuration for the NGINX Gateway Fabric control plane deployment. | object | `{"affinity":{},"autoscaling":{"enable":false},"config":{"logging":{"level":"info"}},"configAnnotations":{},"extraVolumeMounts":[],"extraVolumes":[],"gatewayClassAnnotations":{},"gatewayClassName":"nginx","gatewayControllerName":"gateway.nginx.org/nginx-gateway-controller","gwAPIExperimentalFeatures":{"enable":false},"gwAPIInferenceExtension":{"enable":false,"endpointPicker":{"disableTLS":false,"skipVerify":true}},"image":{"pullPolicy":"Always","repository":"ghcr.io/nginx/nginx-gateway-fabric","tag":"edge"},"kind":"deployment","labels":{},"leaderElection":{"enable":true,"lockName":""},"lifecycle":{},"metrics":{"enable":true,"port":9113,"secure":false},"name":"","nodeSelector":{},"podAnnotations":{},"productTelemetry":{"enable":true},"readinessProbe":{"enable":true,"initialDelaySeconds":3,"port":8081},"replicas":1,"resources":{},"service":{"annotations":{},"labels":{}},"serviceAccount":{"annotations":{},"imagePullSecret":"","imagePullSecrets":[],"name":""},"snippetsFilters":{"enable":false},"snippetsPolicies":{"enable":false},"terminationGracePeriodSeconds":30,"tolerations":[],"topologySpreadConstraints":[]}` |
250250
| `nginxGateway.affinity` | The affinity of the NGINX Gateway Fabric control plane pod. | object | `{}` |
251251
| `nginxGateway.autoscaling` | Autoscaling configuration for the NGINX Gateway Fabric control plane. | object | `{"enable":false}` |
252252
| `nginxGateway.autoscaling.enable` | Enable or disable Horizontal Pod Autoscaler for the control plane. | bool | `false` |
@@ -290,6 +290,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
290290
| `nginxGateway.serviceAccount.imagePullSecrets` | A list of secret names containing docker registry credentials for the control plane. Secrets must exist in the same namespace as the helm release. | list | `[]` |
291291
| `nginxGateway.serviceAccount.name` | The name of the service account of the NGINX Gateway Fabric control plane pods. Used for RBAC. | string | Autogenerated if not set or set to "" |
292292
| `nginxGateway.snippetsFilters.enable` | Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the generated NGINX config for HTTPRoute and GRPCRoute resources. | bool | `false` |
293+
| `nginxGateway.snippetsPolicies.enable` | Enable SnippetsPolicies feature. SnippetsPolicies allow inserting NGINX configuration into the generated NGINX config for Gateway resources. | bool | `false` |
293294
| `nginxGateway.terminationGracePeriodSeconds` | The termination grace period of the NGINX Gateway Fabric control plane pod. | int | `30` |
294295
| `nginxGateway.tolerations` | Tolerations for the NGINX Gateway Fabric control plane pod. | list | `[]` |
295296
| `nginxGateway.topologySpreadConstraints` | The topology spread constraints for the NGINX Gateway Fabric control plane pod. | list | `[]` |

charts/nginx-gateway-fabric/templates/clusterrole.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ rules:
132132
{{- if .Values.nginxGateway.snippetsFilters.enable }}
133133
- snippetsfilters
134134
{{- end }}
135+
{{- if .Values.nginxGateway.snippetsPolicies.enable }}
136+
- snippetspolicies
137+
{{- end }}
135138
verbs:
136139
- list
137140
- watch
@@ -145,6 +148,9 @@ rules:
145148
{{- if .Values.nginxGateway.snippetsFilters.enable }}
146149
- snippetsfilters/status
147150
{{- end }}
151+
{{- if .Values.nginxGateway.snippetsPolicies.enable }}
152+
- snippetspolicies/status
153+
{{- end }}
148154
verbs:
149155
- update
150156
{{- if .Values.nginxGateway.gwAPIInferenceExtension.enable }}

charts/nginx-gateway-fabric/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ spec:
110110
{{- if .Values.nginxGateway.snippetsFilters.enable }}
111111
- --snippets-filters
112112
{{- end }}
113+
{{- if .Values.nginxGateway.snippetsPolicies.enable }}
114+
- --snippets-policies
115+
{{- end }}
113116
{{- if .Capabilities.APIVersions.Has "security.openshift.io/v1/SecurityContextConstraints" }}
114117
- --nginx-scc={{ include "nginx-gateway.scc-name" . }}-nginx
115118
{{- end}}

charts/nginx-gateway-fabric/values.schema.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,20 @@
11481148
"title": "snippetsFilters",
11491149
"type": "object"
11501150
},
1151+
"snippetsPolicies": {
1152+
"properties": {
1153+
"enable": {
1154+
"default": false,
1155+
"description": "Enable SnippetsPolicies feature. SnippetsPolicies allow inserting NGINX configuration into the generated NGINX\nconfig for Gateway resources.",
1156+
"required": [],
1157+
"title": "enable",
1158+
"type": "boolean"
1159+
}
1160+
},
1161+
"required": [],
1162+
"title": "snippetsPolicies",
1163+
"type": "object"
1164+
},
11511165
"terminationGracePeriodSeconds": {
11521166
"default": 30,
11531167
"description": "The termination grace period of the NGINX Gateway Fabric control plane pod.",

charts/nginx-gateway-fabric/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ nginxGateway:
232232
# config for HTTPRoute and GRPCRoute resources.
233233
enable: false
234234

235+
snippetsPolicies:
236+
# -- Enable SnippetsPolicies feature. SnippetsPolicies allow inserting NGINX configuration into the generated NGINX
237+
# config for Gateway resources.
238+
enable: false
239+
235240
# -- The nginx section contains the configuration for all NGINX data plane deployments
236241
# installed by the NGINX Gateway Fabric control plane.
237242
nginx:

cmd/gateway/commands.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func createControllerCommand() *cobra.Command {
9595
usageReportCASecretFlag = "usage-report-ca-secret" //nolint:gosec // not credentials
9696
usageReportEnforceInitialReportFlag = "usage-report-enforce-initial-report"
9797
snippetsFiltersFlag = "snippets-filters"
98+
snippetsPoliciesFlag = "snippets-policies"
9899
nginxSCCFlag = "nginx-scc"
99100
)
100101

@@ -156,7 +157,8 @@ func createControllerCommand() *cobra.Command {
156157

157158
disableProductTelemetry bool
158159

159-
snippetsFilters bool
160+
snippetsFilters bool
161+
snippetsPolicies bool
160162

161163
plus bool
162164
nginxDockerSecrets = stringSliceValidatingValue{
@@ -282,6 +284,7 @@ func createControllerCommand() *cobra.Command {
282284
Values: flagValues,
283285
},
284286
SnippetsFilters: snippetsFilters,
287+
SnippetsPolicies: snippetsPolicies,
285288
NginxDockerSecretNames: nginxDockerSecrets.values,
286289
AgentTLSSecretName: agentTLSSecretName.value,
287290
NGINXSCCName: nginxSCCName.value,
@@ -512,6 +515,14 @@ func createControllerCommand() *cobra.Command {
512515
"generated NGINX config for HTTPRoute and GRPCRoute resources.",
513516
)
514517

518+
cmd.Flags().BoolVar(
519+
&snippetsPolicies,
520+
snippetsPoliciesFlag,
521+
false,
522+
"Enable SnippetsPolicies feature. SnippetsPolicies allow inserting NGINX configuration into the "+
523+
"generated NGINX config for Gateway resources.",
524+
)
525+
515526
cmd.Flags().Var(
516527
&nginxSCCName,
517528
nginxSCCFlag,

0 commit comments

Comments
 (0)