Skip to content

Commit ac3e9b9

Browse files
kate-osbornsalonichf5
authored andcommitted
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 927779c commit ac3e9b9

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
@@ -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.

cmd/gateway/commands.go

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

7172
// flag values
@@ -117,6 +118,8 @@ func createStaticModeCommand() *cobra.Command {
117118
usageReportServerURL = stringValidatingValue{
118119
validator: validateURL,
119120
}
121+
122+
snippetsFilters bool
120123
)
121124

122125
cmd := &cobra.Command{
@@ -242,6 +245,7 @@ func createStaticModeCommand() *cobra.Command {
242245
Names: flagKeys,
243246
Values: flagValues,
244247
},
248+
SnippetsFilters: snippetsFilters,
245249
}
246250

247251
if err := static.StartManager(conf); err != nil {
@@ -397,6 +401,14 @@ func createStaticModeCommand() *cobra.Command {
397401
"Disable client verification of the NGINX Plus usage reporting server certificate.",
398402
)
399403

404+
cmd.Flags().BoolVar(
405+
&snippetsFilters,
406+
snippetsFiltersFlag,
407+
false,
408+
"Enable SnippetsFilters feature. SnippetsFilters allow inserting NGINX configuration into the "+
409+
"generated NGINX config for HTTPRoute and GRPCRoute resources.",
410+
)
411+
400412
return cmd
401413
}
402414

cmd/gateway/commands_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func TestStaticModeCmdFlagValidation(t *testing.T) {
151151
"--usage-report-secret=default/my-secret",
152152
"--usage-report-server-url=https://my-api.com",
153153
"--usage-report-cluster-name=my-cluster",
154+
"--snippets-filters",
154155
},
155156
wantErr: false,
156157
},
@@ -366,6 +367,15 @@ func TestStaticModeCmdFlagValidation(t *testing.T) {
366367
wantErr: true,
367368
expectedErrPrefix: `invalid argument "$invalid*(#)" for "--usage-report-cluster-name" flag: invalid format`,
368369
},
370+
{
371+
name: "snippets-filters is not a bool",
372+
expectedErrPrefix: `invalid argument "not-a-bool" for "--snippets-filters" flag: strconv.ParseBool:` +
373+
` parsing "not-a-bool": invalid syntax`,
374+
args: []string{
375+
"--snippets-filters=not-a-bool",
376+
},
377+
wantErr: true,
378+
},
369379
}
370380

371381
// 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)