Skip to content

Commit dc1607b

Browse files
committed
add support for ipv6
1 parent 7bc0b6e commit dc1607b

File tree

17 files changed

+378
-53
lines changed

17 files changed

+378
-53
lines changed

apis/v1alpha1/nginxproxy_types.go

+19
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,31 @@ type NginxProxyList struct {
2727
Items []NginxProxy `json:"items"`
2828
}
2929

30+
// IPFamilyType specifies the IP family to be used by the server.
31+
//
32+
// +kubebuilder:validation:Enum=both;ipv4;ipv6
33+
type IPFamilyType string
34+
35+
const (
36+
// Dual specifies that the server will use both IPv4 and IPv6.
37+
Dual IPFamilyType = "dual"
38+
// IPv4 specifies that the server will use only IPv4.
39+
IPv4 IPFamilyType = "ipv4"
40+
// IPv6 specifies that the server will use only IPv6.
41+
IPv6 IPFamilyType = "ipv6"
42+
)
43+
3044
// NginxProxySpec defines the desired state of the NginxProxy.
3145
type NginxProxySpec struct {
3246
// Telemetry specifies the OpenTelemetry configuration.
3347
//
3448
// +optional
3549
Telemetry *Telemetry `json:"telemetry,omitempty"`
50+
// IPFamily specifies the IP family to be used by the server.
51+
// Default is "both", meaning the server will use both IPv4 and IPv6.
52+
//
53+
// +optional
54+
IPFamily IPFamilyType `json:"ipFamily,omitempty"`
3655
// DisableHTTP2 defines if http2 should be disabled for all servers.
3756
// Default is false, meaning http2 will be enabled for all servers.
3857
//

charts/nginx-gateway-fabric/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ The following table lists the configurable parameters of the NGINX Gateway Fabri
260260
| `metrics.enable` | Enable exposing metrics in the Prometheus format. | bool | `true` |
261261
| `metrics.port` | Set the port where the Prometheus metrics are exposed. Format: [1024 - 65535] | int | `9113` |
262262
| `metrics.secure` | Enable serving metrics via https. By default metrics are served via http. Please note that this endpoint will be secured with a self-signed certificate. | bool | `false` |
263-
| `nginx.config` | The configuration for the data plane that is contained in the NginxProxy resource. | object | `{}` |
263+
| `nginx.config` | The configuration for the data plane that is contained in the NginxProxy resource. | object | `{"ipFamily":"dual"}` |
264264
| `nginx.extraVolumeMounts` | extraVolumeMounts are the additional volume mounts for the nginx container. | list | `[]` |
265265
| `nginx.image.pullPolicy` | | string | `"Always"` |
266266
| `nginx.image.repository` | The NGINX image to use. | string | `"ghcr.io/nginxinc/nginx-gateway-fabric/nginx"` |

charts/nginx-gateway-fabric/values.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ nginx:
8484

8585
# -- The configuration for the data plane that is contained in the NginxProxy resource.
8686
config:
87-
{}
8887
# disableHTTP2: false
88+
ipFamily: dual
8989
# telemetry:
9090
# exporter:
9191
# endpoint: otel-collector.default.svc:4317

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

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ spec:
5252
DisableHTTP2 defines if http2 should be disabled for all servers.
5353
Default is false, meaning http2 will be enabled for all servers.
5454
type: boolean
55+
ipFamily:
56+
description: |-
57+
IPFamily specifies the IP family to be used by the server.
58+
Default is "both", meaning the server will use both IPv4 and IPv6.
59+
enum:
60+
- both
61+
- ipv4
62+
- ipv6
63+
type: string
5564
telemetry:
5665
description: Telemetry specifies the OpenTelemetry configuration.
5766
properties:

deploy/crds.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,15 @@ spec:
697697
DisableHTTP2 defines if http2 should be disabled for all servers.
698698
Default is false, meaning http2 will be enabled for all servers.
699699
type: boolean
700+
ipFamily:
701+
description: |-
702+
IPFamily specifies the IP family to be used by the server.
703+
Default is "both", meaning the server will use both IPv4 and IPv6.
704+
enum:
705+
- both
706+
- ipv4
707+
- ipv6
708+
type: string
700709
telemetry:
701710
description: Telemetry specifies the OpenTelemetry configuration.
702711
properties:

deploy/manifests/nginx-gateway-experimental.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ metadata:
295295
app.kubernetes.io/version: "edge"
296296
spec:
297297
controllerName: gateway.nginx.org/nginx-gateway-controller
298+
parametersRef:
299+
group: gateway.nginx.org
300+
kind: NginxProxy
301+
name: nginx-gateway-proxy-config
298302
---
299303
# Source: nginx-gateway-fabric/templates/nginxgateway.yaml
300304
apiVersion: gateway.nginx.org/v1alpha1
@@ -309,3 +313,15 @@ metadata:
309313
spec:
310314
logging:
311315
level: info
316+
---
317+
# Source: nginx-gateway-fabric/templates/nginxproxy.yaml
318+
apiVersion: gateway.nginx.org/v1alpha1
319+
kind: NginxProxy
320+
metadata:
321+
name: nginx-gateway-proxy-config
322+
labels:
323+
app.kubernetes.io/name: nginx-gateway
324+
app.kubernetes.io/instance: nginx-gateway
325+
app.kubernetes.io/version: "edge"
326+
spec:
327+
ipFamily: dual

deploy/manifests/nginx-gateway.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ metadata:
291291
app.kubernetes.io/version: "edge"
292292
spec:
293293
controllerName: gateway.nginx.org/nginx-gateway-controller
294+
parametersRef:
295+
group: gateway.nginx.org
296+
kind: NginxProxy
297+
name: nginx-gateway-proxy-config
294298
---
295299
# Source: nginx-gateway-fabric/templates/nginxgateway.yaml
296300
apiVersion: gateway.nginx.org/v1alpha1
@@ -305,3 +309,15 @@ metadata:
305309
spec:
306310
logging:
307311
level: info
312+
---
313+
# Source: nginx-gateway-fabric/templates/nginxproxy.yaml
314+
apiVersion: gateway.nginx.org/v1alpha1
315+
kind: NginxProxy
316+
metadata:
317+
name: nginx-gateway-proxy-config
318+
labels:
319+
app.kubernetes.io/name: nginx-gateway
320+
app.kubernetes.io/instance: nginx-gateway
321+
app.kubernetes.io/version: "edge"
322+
spec:
323+
ipFamily: dual

deploy/manifests/nginx-plus-gateway-experimental.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ metadata:
302302
app.kubernetes.io/version: "edge"
303303
spec:
304304
controllerName: gateway.nginx.org/nginx-gateway-controller
305+
parametersRef:
306+
group: gateway.nginx.org
307+
kind: NginxProxy
308+
name: nginx-gateway-proxy-config
305309
---
306310
# Source: nginx-gateway-fabric/templates/nginxgateway.yaml
307311
apiVersion: gateway.nginx.org/v1alpha1
@@ -316,3 +320,15 @@ metadata:
316320
spec:
317321
logging:
318322
level: info
323+
---
324+
# Source: nginx-gateway-fabric/templates/nginxproxy.yaml
325+
apiVersion: gateway.nginx.org/v1alpha1
326+
kind: NginxProxy
327+
metadata:
328+
name: nginx-gateway-proxy-config
329+
labels:
330+
app.kubernetes.io/name: nginx-gateway
331+
app.kubernetes.io/instance: nginx-gateway
332+
app.kubernetes.io/version: "edge"
333+
spec:
334+
ipFamily: dual

deploy/manifests/nginx-plus-gateway.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ metadata:
298298
app.kubernetes.io/version: "edge"
299299
spec:
300300
controllerName: gateway.nginx.org/nginx-gateway-controller
301+
parametersRef:
302+
group: gateway.nginx.org
303+
kind: NginxProxy
304+
name: nginx-gateway-proxy-config
301305
---
302306
# Source: nginx-gateway-fabric/templates/nginxgateway.yaml
303307
apiVersion: gateway.nginx.org/v1alpha1
@@ -312,3 +316,15 @@ metadata:
312316
spec:
313317
logging:
314318
level: info
319+
---
320+
# Source: nginx-gateway-fabric/templates/nginxproxy.yaml
321+
apiVersion: gateway.nginx.org/v1alpha1
322+
kind: NginxProxy
323+
metadata:
324+
name: nginx-gateway-proxy-config
325+
labels:
326+
app.kubernetes.io/name: nginx-gateway
327+
app.kubernetes.io/instance: nginx-gateway
328+
app.kubernetes.io/version: "edge"
329+
spec:
330+
ipFamily: dual

internal/mode/static/nginx/config/http/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Server struct {
1010
IsDefaultHTTP bool
1111
IsDefaultSSL bool
1212
GRPC bool
13+
IPv6Enabled bool
1314
}
1415

1516
// Location holds all configuration for an HTTP location.

internal/mode/static/nginx/config/servers.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
gotemplate "text/template"
1010

11+
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
1112
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
1213
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/nginx/config/http"
1314
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/dataplane"
@@ -58,7 +59,8 @@ var grpcBaseHeaders = []http.Header{
5859
}
5960

6061
func executeServers(conf dataplane.Configuration) []executeResult {
61-
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers)
62+
ipv6Enabled := isIPv6Enabled(conf.BaseHTTPConfig)
63+
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers, ipv6Enabled)
6264

6365
serverResult := executeResult{
6466
dest: httpConfigFile,
@@ -86,6 +88,14 @@ func executeServers(conf dataplane.Configuration) []executeResult {
8688
return allResults
8789
}
8890

91+
// getIPFamily returns whether or not the configuration is set to use IPv6.
92+
func isIPv6Enabled(baseHTTPConfig dataplane.BaseHTTPConfig) bool {
93+
if baseHTTPConfig.IPFamily == ngfAPI.IPv6 || baseHTTPConfig.IPFamily == ngfAPI.Dual {
94+
return true
95+
}
96+
return false
97+
}
98+
8999
func createAdditionFileResults(conf dataplane.Configuration) []executeResult {
90100
uniqueAdditions := make(map[string][]byte)
91101

@@ -141,17 +151,23 @@ func createIncludes(additions []dataplane.Addition) []string {
141151
return includes
142152
}
143153

144-
func createServers(httpServers, sslServers []dataplane.VirtualServer) ([]http.Server, httpMatchPairs) {
154+
func createServers(
155+
httpServers,
156+
sslServers []dataplane.VirtualServer,
157+
ipv6Enabled bool,
158+
) ([]http.Server, httpMatchPairs) {
145159
servers := make([]http.Server, 0, len(httpServers)+len(sslServers))
146160
finalMatchPairs := make(httpMatchPairs)
147161

148162
for serverID, s := range httpServers {
163+
s.IPv6Enabled = ipv6Enabled
149164
httpServer, matchPairs := createServer(s, serverID)
150165
servers = append(servers, httpServer)
151166
maps.Copy(finalMatchPairs, matchPairs)
152167
}
153168

154169
for serverID, s := range sslServers {
170+
s.IPv6Enabled = ipv6Enabled
155171
sslServer, matchPair := createSSLServer(s, serverID)
156172
servers = append(servers, sslServer)
157173
maps.Copy(finalMatchPairs, matchPair)
@@ -165,6 +181,7 @@ func createSSLServer(virtualServer dataplane.VirtualServer, serverID int) (http.
165181
return http.Server{
166182
IsDefaultSSL: true,
167183
Port: virtualServer.Port,
184+
IPv6Enabled: virtualServer.IPv6Enabled,
168185
}, nil
169186
}
170187

@@ -176,10 +193,11 @@ func createSSLServer(virtualServer dataplane.VirtualServer, serverID int) (http.
176193
Certificate: generatePEMFileName(virtualServer.SSL.KeyPairID),
177194
CertificateKey: generatePEMFileName(virtualServer.SSL.KeyPairID),
178195
},
179-
Locations: locs,
180-
Port: virtualServer.Port,
181-
GRPC: grpc,
182-
Includes: createIncludes(virtualServer.Additions),
196+
Locations: locs,
197+
Port: virtualServer.Port,
198+
GRPC: grpc,
199+
Includes: createIncludes(virtualServer.Additions),
200+
IPv6Enabled: virtualServer.IPv6Enabled,
183201
}, matchPairs
184202
}
185203

@@ -188,17 +206,19 @@ func createServer(virtualServer dataplane.VirtualServer, serverID int) (http.Ser
188206
return http.Server{
189207
IsDefaultHTTP: true,
190208
Port: virtualServer.Port,
209+
IPv6Enabled: virtualServer.IPv6Enabled,
191210
}, nil
192211
}
193212

194213
locs, matchPairs, grpc := createLocations(&virtualServer, serverID)
195214

196215
return http.Server{
197-
ServerName: virtualServer.Hostname,
198-
Locations: locs,
199-
Port: virtualServer.Port,
200-
GRPC: grpc,
201-
Includes: createIncludes(virtualServer.Additions),
216+
ServerName: virtualServer.Hostname,
217+
Locations: locs,
218+
Port: virtualServer.Port,
219+
GRPC: grpc,
220+
Includes: createIncludes(virtualServer.Additions),
221+
IPv6Enabled: virtualServer.IPv6Enabled,
202222
}, matchPairs
203223
}
204224

internal/mode/static/nginx/config/servers_template.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ js_preload_object matches from /etc/nginx/conf.d/matches.json;
66
{{ if $s.IsDefaultSSL -}}
77
server {
88
listen {{ $s.Port }} ssl default_server;
9+
{{- if $s.IPv6Enabled }}
10+
listen [::]:{{ $s.Port }} ssl default_server;
11+
{{- end }}
912
1013
ssl_reject_handshake on;
1114
}
1215
{{- else if $s.IsDefaultHTTP }}
1316
server {
1417
listen {{ $s.Port }} default_server;
18+
{{- if $s.IPv6Enabled }}
19+
listen [::]:{{ $s.Port }} default_server;
20+
{{- end }}
1521
1622
default_type text/html;
1723
return 404;
@@ -20,6 +26,9 @@ server {
2026
server {
2127
{{- if $s.SSL }}
2228
listen {{ $s.Port }} ssl;
29+
{{- if $s.IPv6Enabled }}
30+
listen [::]:{{ $s.Port }} ssl;
31+
{{- end }}
2332
ssl_certificate {{ $s.SSL.Certificate }};
2433
ssl_certificate_key {{ $s.SSL.CertificateKey }};
2534
@@ -28,6 +37,9 @@ server {
2837
}
2938
{{- else }}
3039
listen {{ $s.Port }};
40+
{{- if $s.IPv6Enabled }}
41+
listen [::]:{{ $s.Port }};
42+
{{- end }}
3143
{{- end }}
3244
3345
server_name {{ $s.ServerName }};
@@ -94,14 +106,14 @@ server {
94106
{{- end }}
95107
{{ end }}
96108
server {
97-
listen unix:/var/run/nginx/nginx-502-server.sock;
109+
listen unix:/var/lib/nginx/nginx-502-server.sock;
98110
access_log off;
99111
100112
return 502;
101113
}
102114
103115
server {
104-
listen unix:/var/run/nginx/nginx-500-server.sock;
116+
listen unix:/var/lib/nginx/nginx-500-server.sock;
105117
access_log off;
106118
107119
return 500;

0 commit comments

Comments
 (0)