Skip to content

Commit cfc3b95

Browse files
sjbermansalonichf5kate-osborn
authored
Add SnippetsFilter API (#2667)
Problem: As a user of NGF that has a need for an enhancement in the project, I want the ability to customize the NGINX configuration for NGF, So that I can utilize features that have not yet been exposed via configuration, Or so that I can work around a problem that has not yet been solved in NGF, but can be fixed through NGINX configuration. Solution: Add the new SnippetsFilter CRD to allow for injecting custom nginx configuration to a routing rule. Apply configuration of valid SnippetsFilters referenced in HTTPRoutes and GRPCRoutes to the appropriate contexts in the NGINX config. If the SnippetsFilter referenced is invalid (wrong group or kind), the routing rule is not configured. If the SnippetsFilter cannot be resolved, the routing rule is configured, but the route will return a 500. --------- Co-authored-by: salonichf5 <[email protected]> Co-authored-by: Kate Osborn <[email protected]>
1 parent 956c05f commit cfc3b95

File tree

79 files changed

+7864
-1396
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+7864
-1396
lines changed

apis/v1alpha1/register.go

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
4040
&ObservabilityPolicyList{},
4141
&ClientSettingsPolicy{},
4242
&ClientSettingsPolicyList{},
43+
&SnippetsFilter{},
44+
&SnippetsFilterList{},
4345
)
4446
// AddToGroupVersion allows the serialization of client types like ListOptions.
4547
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)

apis/v1alpha1/snippetsfilter_types.go

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package v1alpha1
2+
3+
import (
4+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5+
v1 "sigs.k8s.io/gateway-api/apis/v1"
6+
)
7+
8+
// +genclient
9+
// +kubebuilder:object:root=true
10+
// +kubebuilder:storageversion
11+
// +kubebuilder:subresource:status
12+
// +kubebuilder:resource:categories=nginx-gateway-fabric,shortName=snippetsfilter
13+
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
14+
15+
// SnippetsFilter is a filter that allows inserting NGINX configuration into the
16+
// generated NGINX config for HTTPRoute and GRPCRoute resources.
17+
type SnippetsFilter struct {
18+
metav1.TypeMeta `json:",inline"`
19+
metav1.ObjectMeta `json:"metadata,omitempty"`
20+
21+
// Spec defines the desired state of the SnippetsFilter.
22+
Spec SnippetsFilterSpec `json:"spec"`
23+
24+
// Status defines the state of the SnippetsFilter.
25+
Status SnippetsFilterStatus `json:"status,omitempty"`
26+
}
27+
28+
// +kubebuilder:object:root=true
29+
30+
// SnippetsFilterList contains a list of SnippetFilters.
31+
type SnippetsFilterList struct {
32+
metav1.TypeMeta `json:",inline"`
33+
metav1.ListMeta `json:"metadata,omitempty"`
34+
Items []SnippetsFilter `json:"items"`
35+
}
36+
37+
// SnippetsFilterSpec defines the desired state of the SnippetsFilter.
38+
type SnippetsFilterSpec struct {
39+
// Snippets is a list of NGINX configuration snippets.
40+
// There can only be one snippet per context.
41+
// Allowed contexts: main, http, http.server, http.server.location.
42+
// +kubebuilder:validation:MinItems=1
43+
// +kubebuilder:validation:MaxItems=4
44+
// +kubebuilder:validation:XValidation:message="Only one snippet allowed per context",rule="self.all(s1, self.exists_one(s2, s1.context == s2.context))"
45+
//nolint:lll
46+
Snippets []Snippet `json:"snippets"`
47+
}
48+
49+
// Snippet represents an NGINX configuration snippet.
50+
type Snippet struct {
51+
// Context is the NGINX context to insert the snippet into.
52+
Context NginxContext `json:"context"`
53+
54+
// Value is the NGINX configuration snippet.
55+
// +kubebuilder:validation:MinLength=1
56+
Value string `json:"value"`
57+
}
58+
59+
// NginxContext represents the NGINX configuration context.
60+
//
61+
// +kubebuilder:validation:Enum=main;http;http.server;http.server.location
62+
type NginxContext string
63+
64+
const (
65+
// NginxContextMain is the main context of the NGINX configuration.
66+
NginxContextMain NginxContext = "main"
67+
68+
// NginxContextHTTP is the http context of the NGINX configuration.
69+
// https://nginx.org/en/docs/http/ngx_http_core_module.html#http
70+
NginxContextHTTP NginxContext = "http"
71+
72+
// NginxContextHTTPServer is the server context of the NGINX configuration.
73+
// https://nginx.org/en/docs/http/ngx_http_core_module.html#server
74+
NginxContextHTTPServer NginxContext = "http.server"
75+
76+
// NginxContextHTTPServerLocation is the location context of the NGINX configuration.
77+
// https://nginx.org/en/docs/http/ngx_http_core_module.html#location
78+
NginxContextHTTPServerLocation NginxContext = "http.server.location"
79+
)
80+
81+
// SnippetsFilterStatus defines the state of SnippetsFilter.
82+
type SnippetsFilterStatus struct {
83+
// Controllers is a list of Gateway API controllers that processed the SnippetsFilter
84+
// and the status of the SnippetsFilter with respect to each controller.
85+
//
86+
// +kubebuilder:validation:MaxItems=16
87+
Controllers []ControllerStatus `json:"controllers,omitempty"`
88+
}
89+
90+
type ControllerStatus struct {
91+
// ControllerName is a domain/path string that indicates the name of the
92+
// controller that wrote this status. This corresponds with the
93+
// controllerName field on GatewayClass.
94+
//
95+
// Example: "example.net/gateway-controller".
96+
//
97+
// The format of this field is DOMAIN "/" PATH, where DOMAIN and PATH are
98+
// valid Kubernetes names
99+
// (https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names).
100+
//
101+
// Controllers MUST populate this field when writing status. Controllers should ensure that
102+
// entries to status populated with their ControllerName are cleaned up when they are no
103+
// longer necessary.
104+
ControllerName v1.GatewayController `json:"controllerName"`
105+
106+
// Conditions describe the status of the SnippetsFilter.
107+
//
108+
// +optional
109+
// +listType=map
110+
// +listMapKey=type
111+
// +kubebuilder:validation:MinItems=1
112+
// +kubebuilder:validation:MaxItems=8
113+
Conditions []metav1.Condition `json:"conditions,omitempty"`
114+
}
115+
116+
// SnippetsFilterConditionType is a type of condition associated with SnippetsFilter.
117+
type SnippetsFilterConditionType string
118+
119+
// SnippetsFilterConditionReason is a reason for a SnippetsFilter condition type.
120+
type SnippetsFilterConditionReason string
121+
122+
const (
123+
// SnippetsFilterConditionTypeAccepted indicates that the SnippetsFilter is accepted.
124+
//
125+
// Possible reasons for this condition to be True:
126+
//
127+
// * Accepted
128+
//
129+
// Possible reasons for this condition to be False:
130+
//
131+
// * Invalid.
132+
SnippetsFilterConditionTypeAccepted SnippetsFilterConditionType = "Accepted"
133+
134+
// SnippetsFilterConditionReasonAccepted is used with the Accepted condition type when
135+
// the condition is true.
136+
SnippetsFilterConditionReasonAccepted SnippetsFilterConditionReason = "Accepted"
137+
138+
// SnippetsFilterConditionReasonInvalid is used with the Accepted condition type when
139+
// SnippetsFilter is invalid.
140+
SnippetsFilterConditionReasonInvalid SnippetsFilterConditionReason = "Invalid"
141+
)

apis/v1alpha1/zz_generated.deepcopy.go

+138
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/nginx-gateway-fabric/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
294294
| `nginxGateway.replicaCount` | The number of replicas of the NGINX Gateway Fabric Deployment. | int | `1` |
295295
| `nginxGateway.resources` | The resource requests and/or limits of the nginx-gateway container. | object | `{}` |
296296
| `nginxGateway.securityContext.allowPrivilegeEscalation` | Some environments may need this set to true in order for the control plane to successfully reload NGINX. | bool | `false` |
297+
| `nginxGateway.snippetsFilters.enable` | Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the generated NGINX config for HTTPRoute and GRPCRoute resources. | bool | `false` |
297298
| `nodeSelector` | The nodeSelector of the NGINX Gateway Fabric pod. | object | `{}` |
298299
| `service.annotations` | The annotations of the NGINX Gateway Fabric service. | object | `{}` |
299300
| `service.create` | Creates a service to expose the NGINX Gateway Fabric pods. | bool | `true` |

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

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ rules:
104104
- nginxproxies
105105
- clientsettingspolicies
106106
- observabilitypolicies
107+
{{- if .Values.nginxGateway.snippetsFilters.enable }}
108+
- snippetsfilters
109+
{{- end }}
107110
verbs:
108111
- list
109112
- watch
@@ -113,6 +116,9 @@ rules:
113116
- nginxgateways/status
114117
- clientsettingspolicies/status
115118
- observabilitypolicies/status
119+
{{- if .Values.nginxGateway.snippetsFilters.enable }}
120+
- snippetsfilters/status
121+
{{- end }}
116122
verbs:
117123
- update
118124
{{- if .Values.nginxGateway.leaderElection.enable }}

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

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ spec:
102102
{{- if .Values.nginx.usage.insecureSkipVerify }}
103103
- --usage-report-skip-verify
104104
{{- end }}
105+
{{- if .Values.nginxGateway.snippetsFilters.enable }}
106+
- --snippets-filters
107+
{{- end }}
105108
env:
106109
- name: POD_IP
107110
valueFrom:

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

+14
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,20 @@
519519
"required": [],
520520
"title": "securityContext",
521521
"type": "object"
522+
},
523+
"snippetsFilters": {
524+
"properties": {
525+
"enable": {
526+
"default": false,
527+
"description": "Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the generated NGINX\nconfig for HTTPRoute and GRPCRoute resources.",
528+
"required": [],
529+
"title": "enable",
530+
"type": "boolean"
531+
}
532+
},
533+
"required": [],
534+
"title": "snippetsFilters",
535+
"type": "object"
522536
}
523537
},
524538
"required": [

charts/nginx-gateway-fabric/values.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ nginxGateway:
113113
# APIs installed from the experimental channel.
114114
enable: false
115115

116+
snippetsFilters:
117+
# -- Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the generated NGINX
118+
# config for HTTPRoute and GRPCRoute resources.
119+
enable: false
120+
116121
nginx:
117122
image:
118123
# -- The NGINX image to use.

0 commit comments

Comments
 (0)