Skip to content

Commit ede4525

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 af6c02a commit ede4525

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

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
@@ -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.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
@@ -67,6 +67,7 @@ func createStaticModeCommand() *cobra.Command {
6767
usageReportServerURLFlag = "usage-report-server-url"
6868
usageReportSkipVerifyFlag = "usage-report-skip-verify"
6969
usageReportClusterNameFlag = "usage-report-cluster-name"
70+
snippetsFiltersFlag = "snippets-filters"
7071
)
7172

7273
// flag values
@@ -118,6 +119,8 @@ func createStaticModeCommand() *cobra.Command {
118119
usageReportServerURL = stringValidatingValue{
119120
validator: validateURL,
120121
}
122+
123+
snippetsFilters bool
121124
)
122125

123126
cmd := &cobra.Command{
@@ -243,6 +246,7 @@ func createStaticModeCommand() *cobra.Command {
243246
Names: flagKeys,
244247
Values: flagValues,
245248
},
249+
SnippetsFilters: snippetsFilters,
246250
}
247251

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

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

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)