Skip to content

Commit b554d46

Browse files
authored
Watch SnippetsFilters when feature is enabled and add to graph (#2519)
Problem: SnippetsFilters need to be in graph before we can write status to them. Solution: Register controller for SnippetsFilter when the flag --snippets-filter is set. Add SnippetsFilter to graph so we can write its status. Validate SnippetsFilter.
1 parent 63d402e commit b554d46

File tree

26 files changed

+1333
-39
lines changed

26 files changed

+1333
-39
lines changed

apis/v1alpha1/snippetsfilter_types.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type SnippetsFilterSpec struct {
3838
// Snippets is a list of NGINX configuration snippets.
3939
// There can only be one snippet per context.
4040
// Allowed contexts: main, http, http.server, http.server.location.
41+
// +kubebuilder:validation:MinItems=1
42+
// +kubebuilder:validation:MaxItems=4
43+
// +kubebuilder:validation:XValidation:message="Only one snippet allowed per context",rule="self.all(s1, self.exists_one(s2, s1.context == s2.context))"
44+
//nolint:lll
4145
Snippets []Snippet `json:"snippets"`
4246
}
4347

@@ -47,6 +51,7 @@ type Snippet struct {
4751
Context NginxContext `json:"context"`
4852

4953
// Value is the NGINX configuration snippet.
54+
// +kubebuilder:validation:MinLength=1
5055
Value string `json:"value"`
5156
}
5257

@@ -104,7 +109,7 @@ const (
104109
// the condition is true.
105110
SnippetsFilterConditionReasonAccepted SnippetsFilterConditionReason = "Accepted"
106111

107-
// SnippetsFilterConditionTypeInvalid is used with the Accepted condition type when
112+
// SnippetsFilterConditionReasonInvalid is used with the Accepted condition type when
108113
// SnippetsFilter is invalid.
109-
SnippetsFilterConditionTypeInvalid SnippetsFilterConditionType = "Invalid"
114+
SnippetsFilterConditionReasonInvalid SnippetsFilterConditionReason = "Invalid"
110115
)

charts/nginx-gateway-fabric/README.md

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

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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ spec:
7575
{{- if .Values.nginx.usage.insecureSkipVerify }}
7676
- --usage-report-skip-verify
7777
{{- end }}
78+
{{- if .Values.nginxGateway.snippetsFilters.enable }}
79+
- --snippets-filters
80+
{{- end }}
7881
env:
7982
- name: POD_IP
8083
valueFrom:

charts/nginx-gateway-fabric/values.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ nginxGateway:
7878
# APIs installed from the experimental channel.
7979
enable: false
8080

81+
snippetsFilters:
82+
# -- Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the generated NGINX
83+
# config for HTTPRoute and GRPCRoute resources.
84+
enable: false
85+
8186
nginx:
8287
image:
8388
# -- The NGINX image to use.

cmd/gateway/commands.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func createStaticModeCommand() *cobra.Command {
6565
usageReportServerURLFlag = "usage-report-server-url"
6666
usageReportSkipVerifyFlag = "usage-report-skip-verify"
6767
usageReportClusterNameFlag = "usage-report-cluster-name"
68+
snippetsFiltersFlag = "snippets-filters"
6869
)
6970

7071
// flag values
@@ -116,6 +117,8 @@ func createStaticModeCommand() *cobra.Command {
116117
usageReportServerURL = stringValidatingValue{
117118
validator: validateURL,
118119
}
120+
121+
snippetsFilters bool
119122
)
120123

121124
cmd := &cobra.Command{
@@ -239,6 +242,7 @@ func createStaticModeCommand() *cobra.Command {
239242
Names: flagKeys,
240243
Values: flagValues,
241244
},
245+
SnippetsFilters: snippetsFilters,
242246
}
243247

244248
if err := static.StartManager(conf); err != nil {
@@ -394,6 +398,14 @@ func createStaticModeCommand() *cobra.Command {
394398
"Disable client verification of the NGINX Plus usage reporting server certificate.",
395399
)
396400

401+
cmd.Flags().BoolVar(
402+
&snippetsFilters,
403+
snippetsFiltersFlag,
404+
false,
405+
"Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the "+
406+
"generated NGINX config for HTTPRoute and GRPCRoute resources.",
407+
)
408+
397409
return cmd
398410
}
399411

cmd/gateway/commands_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func TestStaticModeCmdFlagValidation(t *testing.T) {
166166
"--usage-report-secret=default/my-secret",
167167
"--usage-report-server-url=https://my-api.com",
168168
"--usage-report-cluster-name=my-cluster",
169+
"--snippets-filters",
169170
},
170171
wantErr: false,
171172
},
@@ -381,6 +382,15 @@ func TestStaticModeCmdFlagValidation(t *testing.T) {
381382
wantErr: true,
382383
expectedErrPrefix: `invalid argument "$invalid*(#)" for "--usage-report-cluster-name" flag: invalid format`,
383384
},
385+
{
386+
name: "snippets-filters is not a bool",
387+
expectedErrPrefix: `invalid argument "not-a-bool" for "--snippets-filters" flag: strconv.ParseBool:` +
388+
` parsing "not-a-bool": invalid syntax`,
389+
args: []string{
390+
"--snippets-filters=not-a-bool",
391+
},
392+
wantErr: true,
393+
},
384394
}
385395

386396
// common flags validation is tested separately

config/crd/bases/gateway.nginx.org_snippetsfilters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,18 @@ spec:
6868
type: string
6969
value:
7070
description: Value is the NGINX configuration snippet.
71+
minLength: 1
7172
type: string
7273
required:
7374
- context
7475
- value
7576
type: object
77+
maxItems: 4
78+
minItems: 1
7679
type: array
80+
x-kubernetes-validations:
81+
- message: Only one snippet allowed per context
82+
rule: self.all(s1, self.exists_one(s2, s1.context == s2.context))
7783
required:
7884
- snippets
7985
type: object

0 commit comments

Comments
 (0)