Skip to content

Commit 3aebdfc

Browse files
authored
add nginx content cache as cache policy (#8005)
1 parent 54159f9 commit 3aebdfc

Some content is hidden

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

42 files changed

+3207
-8
lines changed

.github/data/matrix-smoke-oss.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@
3939
"label": "policies 1/2",
4040
"image": "alpine",
4141
"type": "oss",
42-
"marker": "'policies and not policies_rl and not policies_ac and not policies_jwt and not policies_mtls'",
42+
"marker": "'policies and not policies_rl and not policies_ac and not policies_jwt and not policies_mtls and not policies_cache'",
4343
"platforms": "linux/arm64, linux/amd64"
4444
},
4545
{
4646
"label": "policies 2/2",
4747
"image": "alpine",
4848
"type": "oss",
49-
"marker": "'policies_rl or policies_ac or policies_jwt or policies_mtls or otel'",
49+
"marker": "'policies_rl or policies_ac or policies_jwt or policies_mtls or policies_cache or otel'",
5050
"platforms": "linux/arm64, linux/amd64"
5151
},
5252
{

.github/data/matrix-smoke-plus.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"label": "policies 1/3",
6868
"image": "ubi-9-plus",
6969
"type": "plus",
70-
"marker": "'policies and not policies_ac and not policies_jwt and not policies_mtls and not policies_rl'",
70+
"marker": "'policies and not policies_ac and not policies_jwt and not policies_mtls and not policies_rl and not policies_cache'",
7171
"platforms": "linux/arm64, linux/amd64"
7272
},
7373
{
@@ -81,7 +81,7 @@
8181
"label": "policies 3/3",
8282
"image": "ubi-9-plus",
8383
"type": "plus",
84-
"marker": "policies_rl",
84+
"marker": "'policies_rl or policies_cache'",
8585
"platforms": "linux/arm64, linux/amd64"
8686
},
8787
{

charts/nginx-ingress/templates/_helpers.tpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,24 @@ List of volumes for controller.
352352
{{- if eq (include "nginx-ingress.readOnlyRootFilesystem" .) "true" }}
353353
- name: nginx-etc
354354
emptyDir: {}
355+
{{- if .Values.controller.cache.enableShared }}
356+
- name: nginx-cache
357+
persistentVolumeClaim:
358+
claimName: {{ .Values.controller.cache.sharedPVCName }}
359+
{{- else }}
355360
- name: nginx-cache
356361
emptyDir: {}
362+
{{- end }}
357363
- name: nginx-lib
358364
emptyDir: {}
359365
- name: nginx-state
360366
emptyDir: {}
361367
- name: nginx-log
362368
emptyDir: {}
369+
{{- else if .Values.controller.cache.enableShared }}
370+
- name: nginx-cache
371+
persistentVolumeClaim:
372+
claimName: {{ .Values.controller.cache.sharedPVCName }}
363373
{{- end }}
364374
{{- if .Values.controller.appprotect.v5 }}
365375
{{ toYaml .Values.controller.appprotect.volumes }}
@@ -419,6 +429,9 @@ volumeMounts:
419429
name: nginx-state
420430
- mountPath: /var/log/nginx
421431
name: nginx-log
432+
{{- else if .Values.controller.cache.enableShared }}
433+
- mountPath: /var/cache/nginx
434+
name: nginx-cache
422435
{{- end }}
423436
{{- if .Values.controller.appprotect.v5 }}
424437
- name: app-protect-bd-config

charts/nginx-ingress/templates/controller-service.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ spec:
6565
{{- end }}
6666
selector:
6767
{{- include "nginx-ingress.selectorLabels" . | nindent 4 }}
68+
{{- if .Values.controller.service.sessionAffinity.enable }}
69+
sessionAffinity: {{ .Values.controller.service.sessionAffinity.type }}
70+
{{- if eq .Values.controller.service.sessionAffinity.type "ClientIP" }}
71+
sessionAffinityConfig:
72+
clientIP:
73+
timeoutSeconds: {{ .Values.controller.service.sessionAffinity.timeoutSeconds }}
74+
{{- end }}
75+
{{- end }}
6876
{{- if .Values.controller.service.externalIPs }}
6977
externalIPs:
7078
{{ toYaml .Values.controller.service.externalIPs | indent 4 }}

charts/nginx-ingress/values.schema.json

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,50 @@
14551455
"type": "object",
14561456
"ref": "https://raw.githubusercontent.com/nginxinc/kubernetes-json-schema/master/v1.33.1/_definitions.json#/definitions/io.k8s.api.core.v1.ServicePort"
14571457
}
1458+
},
1459+
"sessionAffinity": {
1460+
"type": "object",
1461+
"default": {},
1462+
"title": "The sessionAffinity Schema",
1463+
"required": [],
1464+
"properties": {
1465+
"enable": {
1466+
"type": "boolean",
1467+
"default": false,
1468+
"title": "Enable session affinity",
1469+
"examples": [
1470+
false
1471+
]
1472+
},
1473+
"type": {
1474+
"type": "string",
1475+
"default": "ClientIP",
1476+
"title": "Session affinity type",
1477+
"enum": [
1478+
"ClientIP"
1479+
],
1480+
"examples": [
1481+
"ClientIP"
1482+
]
1483+
},
1484+
"timeoutSeconds": {
1485+
"type": "integer",
1486+
"default": 3600,
1487+
"title": "Session affinity timeout in seconds",
1488+
"minimum": 1,
1489+
"maximum": 86400,
1490+
"examples": [
1491+
3600
1492+
]
1493+
}
1494+
},
1495+
"examples": [
1496+
{
1497+
"enable": false,
1498+
"type": "ClientIP",
1499+
"timeoutSeconds": 3600
1500+
}
1501+
]
14581502
}
14591503
},
14601504
"examples": [
@@ -1483,7 +1527,12 @@
14831527
"targetPort": 443,
14841528
"name": "https"
14851529
},
1486-
"customPorts": []
1530+
"customPorts": [],
1531+
"sessionAffinity": {
1532+
"enable": false,
1533+
"type": "ClientIP",
1534+
"timeoutSeconds": 3600
1535+
}
14871536
}
14881537
]
14891538
},

charts/nginx-ingress/values.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ controller:
164164
## Sets the log format of Ingress Controller. Options include: glog, json, text
165165
logFormat: glog
166166

167+
## Cache configuration options
168+
cache:
169+
## Enables shared cache across multiple pods using an external persistent volume
170+
## When enabled, the /var/cache/nginx directory will be mounted from a PVC instead of using emptyDir
171+
## User must create and configure a PVC with appropriate access mode
172+
enableShared: false
173+
174+
## The name of the PersistentVolumeClaim to use for shared cache, should match the name of the PVC created by the user
175+
sharedPVCName: "nginx-shared-cache"
176+
167177
## A list of custom ports to expose on the NGINX Ingress Controller pod. Follows the conventional Kubernetes yaml syntax for container ports.
168178
customPorts: []
169179

@@ -502,6 +512,15 @@ controller:
502512
## A list of custom ports to expose through the Ingress Controller service. Follows the conventional Kubernetes yaml syntax for service ports.
503513
customPorts: []
504514

515+
## Session affinity configuration for the Ingress Controller service, ensures requests from the same client IP go to the same pod
516+
sessionAffinity:
517+
## Enable session affinity. Valid values: None, ClientIP
518+
enable: false
519+
## Session affinity type. Currently only ClientIP is supported.
520+
type: ClientIP
521+
## Session affinity timeout in seconds (default: 3600 = 1 hour)
522+
timeoutSeconds: 3600
523+
505524
serviceAccount:
506525
## The annotations of the service account of the Ingress Controller pods.
507526
annotations: {}

config/crd/bases/k8s.nginx.org_policies.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,92 @@ spec:
109109
otherwise the secret will be rejected as invalid.
110110
type: string
111111
type: object
112+
cache:
113+
description: The Cache Key defines a cache policy for proxy caching
114+
properties:
115+
allowedCodes:
116+
description: |-
117+
AllowedCodes defines which HTTP response codes should be cached.
118+
Accepts either:
119+
- The string "any" to cache all response codes (must be the only element)
120+
- A list of HTTP status codes as integers (100-599)
121+
Examples: ["any"], [200, 301, 404], [200].
122+
Invalid: ["any", 200] (cannot mix "any" with specific codes).
123+
items:
124+
anyOf:
125+
- type: integer
126+
- type: string
127+
x-kubernetes-int-or-string: true
128+
type: array
129+
allowedMethods:
130+
description: |-
131+
AllowedMethods defines which HTTP methods should be cached.
132+
Only "GET", "HEAD", and "POST" are supported by NGINX proxy_cache_methods directive.
133+
GET and HEAD are always cached by default even if not specified.
134+
Maximum of 3 items allowed. Examples: ["GET"], ["GET", "HEAD", "POST"].
135+
Invalid methods: PUT, DELETE, PATCH, etc.
136+
items:
137+
type: string
138+
maxItems: 3
139+
type: array
140+
x-kubernetes-validations:
141+
- message: 'allowed methods must be one of: GET, HEAD, POST'
142+
rule: self.all(method, method in ['GET', 'HEAD', 'POST'])
143+
cachePurgeAllow:
144+
description: |-
145+
CachePurgeAllow defines IP addresses or CIDR blocks allowed to purge cache.
146+
This feature is only available in NGINX Plus.
147+
Examples: ["192.168.1.100", "10.0.0.0/8", "::1"].
148+
Invalid in NGINX OSS (will be ignored).
149+
items:
150+
type: string
151+
type: array
152+
cacheZoneName:
153+
description: |-
154+
CacheZoneName defines the name of the cache zone. Must start with a lowercase letter,
155+
followed by alphanumeric characters or underscores, and end with an alphanumeric character.
156+
Single lowercase letters are also allowed. Examples: "cache", "my_cache", "cache1".
157+
pattern: ^[a-z][a-zA-Z0-9_]*[a-zA-Z0-9]$|^[a-z]$
158+
type: string
159+
cacheZoneSize:
160+
description: |-
161+
CacheZoneSize defines the size of the cache zone. Must be a number followed by a size unit:
162+
'k' for kilobytes, 'm' for megabytes, or 'g' for gigabytes.
163+
Examples: "10m", "1g", "512k".
164+
pattern: ^[0-9]+[kmg]$
165+
type: string
166+
levels:
167+
description: |-
168+
Levels defines the cache directory hierarchy levels for storing cached files.
169+
Must be in format "X:Y" or "X:Y:Z" where X, Y, Z are either 1 or 2.
170+
This controls the number of subdirectory levels and their name lengths.
171+
Examples: "1:2", "2:2", "1:2:2".
172+
Invalid: "3:1", "1:3", "1:2:3".
173+
pattern: ^[12](?::[12]){0,2}$
174+
type: string
175+
overrideUpstreamCache:
176+
default: false
177+
description: |-
178+
OverrideUpstreamCache controls whether to override upstream cache headers
179+
(using proxy_ignore_headers directive). When true, NGINX will ignore
180+
cache-related headers from upstream servers like Cache-Control, Expires, etc.
181+
Default: false.
182+
type: boolean
183+
time:
184+
description: |-
185+
Time defines the default cache time. Required when allowedCodes is specified.
186+
Must be a number followed by a time unit:
187+
's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days.
188+
Examples: "30s", "5m", "1h", "2d".
189+
pattern: ^[0-9]+[smhd]$
190+
type: string
191+
required:
192+
- cacheZoneName
193+
- cacheZoneSize
194+
type: object
195+
x-kubernetes-validations:
196+
- message: time is required when allowedCodes is specified
197+
rule: '!has(self.allowedCodes) || (has(self.allowedCodes) && has(self.time))'
112198
egressMTLS:
113199
description: The EgressMTLS policy configures upstreams authentication
114200
and certificate verification.

deploy/crds.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,92 @@ spec:
280280
otherwise the secret will be rejected as invalid.
281281
type: string
282282
type: object
283+
cache:
284+
description: The Cache Key defines a cache policy for proxy caching
285+
properties:
286+
allowedCodes:
287+
description: |-
288+
AllowedCodes defines which HTTP response codes should be cached.
289+
Accepts either:
290+
- The string "any" to cache all response codes (must be the only element)
291+
- A list of HTTP status codes as integers (100-599)
292+
Examples: ["any"], [200, 301, 404], [200].
293+
Invalid: ["any", 200] (cannot mix "any" with specific codes).
294+
items:
295+
anyOf:
296+
- type: integer
297+
- type: string
298+
x-kubernetes-int-or-string: true
299+
type: array
300+
allowedMethods:
301+
description: |-
302+
AllowedMethods defines which HTTP methods should be cached.
303+
Only "GET", "HEAD", and "POST" are supported by NGINX proxy_cache_methods directive.
304+
GET and HEAD are always cached by default even if not specified.
305+
Maximum of 3 items allowed. Examples: ["GET"], ["GET", "HEAD", "POST"].
306+
Invalid methods: PUT, DELETE, PATCH, etc.
307+
items:
308+
type: string
309+
maxItems: 3
310+
type: array
311+
x-kubernetes-validations:
312+
- message: 'allowed methods must be one of: GET, HEAD, POST'
313+
rule: self.all(method, method in ['GET', 'HEAD', 'POST'])
314+
cachePurgeAllow:
315+
description: |-
316+
CachePurgeAllow defines IP addresses or CIDR blocks allowed to purge cache.
317+
This feature is only available in NGINX Plus.
318+
Examples: ["192.168.1.100", "10.0.0.0/8", "::1"].
319+
Invalid in NGINX OSS (will be ignored).
320+
items:
321+
type: string
322+
type: array
323+
cacheZoneName:
324+
description: |-
325+
CacheZoneName defines the name of the cache zone. Must start with a lowercase letter,
326+
followed by alphanumeric characters or underscores, and end with an alphanumeric character.
327+
Single lowercase letters are also allowed. Examples: "cache", "my_cache", "cache1".
328+
pattern: ^[a-z][a-zA-Z0-9_]*[a-zA-Z0-9]$|^[a-z]$
329+
type: string
330+
cacheZoneSize:
331+
description: |-
332+
CacheZoneSize defines the size of the cache zone. Must be a number followed by a size unit:
333+
'k' for kilobytes, 'm' for megabytes, or 'g' for gigabytes.
334+
Examples: "10m", "1g", "512k".
335+
pattern: ^[0-9]+[kmg]$
336+
type: string
337+
levels:
338+
description: |-
339+
Levels defines the cache directory hierarchy levels for storing cached files.
340+
Must be in format "X:Y" or "X:Y:Z" where X, Y, Z are either 1 or 2.
341+
This controls the number of subdirectory levels and their name lengths.
342+
Examples: "1:2", "2:2", "1:2:2".
343+
Invalid: "3:1", "1:3", "1:2:3".
344+
pattern: ^[12](?::[12]){0,2}$
345+
type: string
346+
overrideUpstreamCache:
347+
default: false
348+
description: |-
349+
OverrideUpstreamCache controls whether to override upstream cache headers
350+
(using proxy_ignore_headers directive). When true, NGINX will ignore
351+
cache-related headers from upstream servers like Cache-Control, Expires, etc.
352+
Default: false.
353+
type: boolean
354+
time:
355+
description: |-
356+
Time defines the default cache time. Required when allowedCodes is specified.
357+
Must be a number followed by a time unit:
358+
's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days.
359+
Examples: "30s", "5m", "1h", "2d".
360+
pattern: ^[0-9]+[smhd]$
361+
type: string
362+
required:
363+
- cacheZoneName
364+
- cacheZoneSize
365+
type: object
366+
x-kubernetes-validations:
367+
- message: time is required when allowedCodes is specified
368+
rule: '!has(self.allowedCodes) || (has(self.allowedCodes) && has(self.time))'
283369
egressMTLS:
284370
description: The EgressMTLS policy configures upstreams authentication
285371
and certificate verification.

0 commit comments

Comments
 (0)