Skip to content

Commit 8b7ae44

Browse files
committed
add https server support and manifests made
1 parent d48dba2 commit 8b7ae44

File tree

11 files changed

+95
-22
lines changed

11 files changed

+95
-22
lines changed

config/tests/static-deployment.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ spec:
7070
volumeMounts:
7171
- name: nginx-conf
7272
mountPath: /etc/nginx/conf.d
73+
- name: nginx-stream-conf
74+
mountPath: /etc/nginx/stream-conf.d
7375
- name: module-includes
7476
mountPath: /etc/nginx/module-includes
7577
- name: nginx-secrets
@@ -98,6 +100,8 @@ spec:
98100
volumeMounts:
99101
- name: nginx-conf
100102
mountPath: /etc/nginx/conf.d
103+
- name: nginx-stream-conf
104+
mountPath: /etc/nginx/stream-conf.d
101105
- name: module-includes
102106
mountPath: /etc/nginx/module-includes
103107
- name: nginx-secrets
@@ -119,6 +123,8 @@ spec:
119123
volumes:
120124
- name: nginx-conf
121125
emptyDir: {}
126+
- name: nginx-stream-conf
127+
emptyDir: {}
122128
- name: module-includes
123129
emptyDir: {}
124130
- name: nginx-secrets

deploy/manifests/nginx-gateway-experimental.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ spec:
226226
volumeMounts:
227227
- name: nginx-conf
228228
mountPath: /etc/nginx/conf.d
229+
- name: nginx-stream-conf
230+
mountPath: /etc/nginx/stream-conf.d
229231
- name: module-includes
230232
mountPath: /etc/nginx/module-includes
231233
- name: nginx-secrets
@@ -254,6 +256,8 @@ spec:
254256
volumeMounts:
255257
- name: nginx-conf
256258
mountPath: /etc/nginx/conf.d
259+
- name: nginx-stream-conf
260+
mountPath: /etc/nginx/stream-conf.d
257261
- name: module-includes
258262
mountPath: /etc/nginx/module-includes
259263
- name: nginx-secrets
@@ -275,6 +279,8 @@ spec:
275279
volumes:
276280
- name: nginx-conf
277281
emptyDir: {}
282+
- name: nginx-stream-conf
283+
emptyDir: {}
278284
- name: module-includes
279285
emptyDir: {}
280286
- name: nginx-secrets

deploy/manifests/nginx-gateway.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ spec:
222222
volumeMounts:
223223
- name: nginx-conf
224224
mountPath: /etc/nginx/conf.d
225+
- name: nginx-stream-conf
226+
mountPath: /etc/nginx/stream-conf.d
225227
- name: module-includes
226228
mountPath: /etc/nginx/module-includes
227229
- name: nginx-secrets
@@ -250,6 +252,8 @@ spec:
250252
volumeMounts:
251253
- name: nginx-conf
252254
mountPath: /etc/nginx/conf.d
255+
- name: nginx-stream-conf
256+
mountPath: /etc/nginx/stream-conf.d
253257
- name: module-includes
254258
mountPath: /etc/nginx/module-includes
255259
- name: nginx-secrets
@@ -271,6 +275,8 @@ spec:
271275
volumes:
272276
- name: nginx-conf
273277
emptyDir: {}
278+
- name: nginx-stream-conf
279+
emptyDir: {}
274280
- name: module-includes
275281
emptyDir: {}
276282
- name: nginx-secrets

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ spec:
233233
volumeMounts:
234234
- name: nginx-conf
235235
mountPath: /etc/nginx/conf.d
236+
- name: nginx-stream-conf
237+
mountPath: /etc/nginx/stream-conf.d
236238
- name: module-includes
237239
mountPath: /etc/nginx/module-includes
238240
- name: nginx-secrets
@@ -261,6 +263,8 @@ spec:
261263
volumeMounts:
262264
- name: nginx-conf
263265
mountPath: /etc/nginx/conf.d
266+
- name: nginx-stream-conf
267+
mountPath: /etc/nginx/stream-conf.d
264268
- name: module-includes
265269
mountPath: /etc/nginx/module-includes
266270
- name: nginx-secrets
@@ -282,6 +286,8 @@ spec:
282286
volumes:
283287
- name: nginx-conf
284288
emptyDir: {}
289+
- name: nginx-stream-conf
290+
emptyDir: {}
285291
- name: module-includes
286292
emptyDir: {}
287293
- name: nginx-secrets

deploy/manifests/nginx-plus-gateway.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ spec:
229229
volumeMounts:
230230
- name: nginx-conf
231231
mountPath: /etc/nginx/conf.d
232+
- name: nginx-stream-conf
233+
mountPath: /etc/nginx/stream-conf.d
232234
- name: module-includes
233235
mountPath: /etc/nginx/module-includes
234236
- name: nginx-secrets
@@ -257,6 +259,8 @@ spec:
257259
volumeMounts:
258260
- name: nginx-conf
259261
mountPath: /etc/nginx/conf.d
262+
- name: nginx-stream-conf
263+
mountPath: /etc/nginx/stream-conf.d
260264
- name: module-includes
261265
mountPath: /etc/nginx/module-includes
262266
- name: nginx-secrets
@@ -278,6 +282,8 @@ spec:
278282
volumes:
279283
- name: nginx-conf
280284
emptyDir: {}
285+
- name: nginx-stream-conf
286+
emptyDir: {}
281287
- name: module-includes
282288
emptyDir: {}
283289
- name: nginx-secrets

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package http
44
type Server struct {
55
SSL *SSL
66
ServerName string
7+
Listen string
78
Locations []Location
89
Includes []string
9-
Port int32
1010
IsDefaultHTTP bool
1111
IsDefaultSSL bool
1212
GRPC bool

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ func createStreamMaps(conf dataplane.Configuration) []*http.Map {
6161
}
6262
}
6363

64+
for _, s := range conf.SSLServers {
65+
streamMap, ok := portsToMap[s.Port]
66+
67+
hostname := s.Hostname
68+
69+
if s.IsDefault {
70+
hostname = "default"
71+
}
72+
73+
if ok {
74+
streamMap.Parameters = append(streamMap.Parameters, http.MapParameter{
75+
Value: hostname,
76+
Result: "unix:/var/lib/nginx/" + s.Hostname + fmt.Sprint(s.Port) + ".sock",
77+
})
78+
}
79+
}
80+
6481
return maps
6582
}
6683

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,22 @@ func TestCreateStreamMaps(t *testing.T) {
259259
UpstreamName: "backend2",
260260
},
261261
},
262+
SSLServers: []dataplane.VirtualServer{
263+
{
264+
Hostname: "app.example.com",
265+
Port: 8080,
266+
},
267+
},
262268
}
263269

264270
maps := createStreamMaps(conf)
265271
g.Expect(maps).To(HaveLen(2))
266272

267273
g.Expect(maps[0].Parameters).To(HaveLen(1))
268-
g.Expect(maps[1].Parameters).To(HaveLen(2))
274+
g.Expect(maps[1].Parameters).To(HaveLen(3))
269275

270276
g.Expect(maps[0].Parameters[0].Result).To(Equal("unix:/var/lib/nginx/example.com8081.sock"))
271277
g.Expect(maps[1].Parameters[0].Result).To(Equal("unix:/var/lib/nginx/example.com8080.sock"))
272278
g.Expect(maps[1].Parameters[1].Result).To(Equal("unix:/var/lib/nginx/cafe.example.com8080.sock"))
279+
g.Expect(maps[1].Parameters[2].Result).To(Equal("unix:/var/lib/nginx/app.example.com8080.sock"))
273280
}

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var grpcBaseHeaders = []http.Header{
5858
}
5959

6060
func executeServers(conf dataplane.Configuration) []executeResult {
61-
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers)
61+
servers, httpMatchPairs := createServers(conf.HTTPServers, conf.SSLServers, conf.TLSServers)
6262

6363
serverResult := executeResult{
6464
dest: httpConfigFile,
@@ -141,30 +141,47 @@ func createIncludes(additions []dataplane.Addition) []string {
141141
return includes
142142
}
143143

144-
func createServers(httpServers, sslServers []dataplane.VirtualServer) ([]http.Server, httpMatchPairs) {
144+
func createServers(
145+
httpServers, sslServers []dataplane.VirtualServer,
146+
tlsServers []dataplane.Layer4Server,
147+
) ([]http.Server, httpMatchPairs) {
145148
servers := make([]http.Server, 0, len(httpServers)+len(sslServers))
146149
finalMatchPairs := make(httpMatchPairs)
147150

151+
ports := map[int32]bool{}
152+
153+
for _, tlsServer := range tlsServers {
154+
ports[tlsServer.Port] = true
155+
}
156+
148157
for serverID, s := range httpServers {
149158
httpServer, matchPairs := createServer(s, serverID)
150159
servers = append(servers, httpServer)
151160
maps.Copy(finalMatchPairs, matchPairs)
152161
}
153162

154163
for serverID, s := range sslServers {
155-
sslServer, matchPair := createSSLServer(s, serverID)
164+
sslServer, matchPair := createSSLServer(s, serverID, ports[s.Port])
156165
servers = append(servers, sslServer)
157166
maps.Copy(finalMatchPairs, matchPair)
158167
}
159168

160169
return servers, finalMatchPairs
161170
}
162171

163-
func createSSLServer(virtualServer dataplane.VirtualServer, serverID int) (http.Server, httpMatchPairs) {
172+
func createSSLServer(
173+
virtualServer dataplane.VirtualServer,
174+
serverID int,
175+
useSocket bool,
176+
) (http.Server, httpMatchPairs) {
177+
listen := fmt.Sprint(virtualServer.Port)
178+
if useSocket {
179+
listen = "unix:/var/lib/nginx/" + virtualServer.Hostname + fmt.Sprint(virtualServer.Port) + ".sock"
180+
}
164181
if virtualServer.IsDefault {
165182
return http.Server{
166183
IsDefaultSSL: true,
167-
Port: virtualServer.Port,
184+
Listen: listen,
168185
}, nil
169186
}
170187

@@ -177,17 +194,19 @@ func createSSLServer(virtualServer dataplane.VirtualServer, serverID int) (http.
177194
CertificateKey: generatePEMFileName(virtualServer.SSL.KeyPairID),
178195
},
179196
Locations: locs,
180-
Port: virtualServer.Port,
181197
GRPC: grpc,
182198
Includes: createIncludes(virtualServer.Additions),
199+
Listen: listen,
183200
}, matchPairs
184201
}
185202

186203
func createServer(virtualServer dataplane.VirtualServer, serverID int) (http.Server, httpMatchPairs) {
204+
listen := fmt.Sprint(virtualServer.Port)
205+
187206
if virtualServer.IsDefault {
188207
return http.Server{
189208
IsDefaultHTTP: true,
190-
Port: virtualServer.Port,
209+
Listen: listen,
191210
}, nil
192211
}
193212

@@ -196,7 +215,7 @@ func createServer(virtualServer dataplane.VirtualServer, serverID int) (http.Ser
196215
return http.Server{
197216
ServerName: virtualServer.Hostname,
198217
Locations: locs,
199-
Port: virtualServer.Port,
218+
Listen: listen,
200219
GRPC: grpc,
201220
Includes: createIncludes(virtualServer.Additions),
202221
}, matchPairs

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ js_preload_object matches from /etc/nginx/conf.d/matches.json;
55
{{- range $s := . -}}
66
{{ if $s.IsDefaultSSL -}}
77
server {
8-
listen {{ $s.Port }} ssl default_server;
8+
listen {{ $s.Listen }} ssl default_server;
99
1010
ssl_reject_handshake on;
1111
}
1212
{{- else if $s.IsDefaultHTTP }}
1313
server {
14-
listen {{ $s.Port }} default_server;
14+
listen {{ $s.Listen }} default_server;
1515
1616
default_type text/html;
1717
return 404;
1818
}
1919
{{- else }}
2020
server {
2121
{{- if $s.SSL }}
22-
listen {{ $s.Port }} ssl;
22+
listen {{ $s.Listen }} ssl;
2323
ssl_certificate {{ $s.SSL.Certificate }};
2424
ssl_certificate_key {{ $s.SSL.CertificateKey }};
2525
2626
if ($ssl_server_name != $host) {
2727
return 421;
2828
}
2929
{{- else }}
30-
listen {{ $s.Port }};
30+
listen {{ $s.Listen }};
3131
{{- end }}
3232
3333
server_name {{ $s.ServerName }};

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,12 +1024,12 @@ func TestCreateServers(t *testing.T) {
10241024
expectedServers := []http.Server{
10251025
{
10261026
IsDefaultHTTP: true,
1027-
Port: 8080,
1027+
Listen: "8080",
10281028
},
10291029
{
10301030
ServerName: "cafe.example.com",
10311031
Locations: getExpectedLocations(false),
1032-
Port: 8080,
1032+
Listen: "8080",
10331033
GRPC: true,
10341034
Includes: []string{
10351035
includesFolder + "/server-addition-1.conf",
@@ -1038,7 +1038,7 @@ func TestCreateServers(t *testing.T) {
10381038
},
10391039
{
10401040
IsDefaultSSL: true,
1041-
Port: 8443,
1041+
Listen: "8443",
10421042
},
10431043
{
10441044
ServerName: "cafe.example.com",
@@ -1047,7 +1047,7 @@ func TestCreateServers(t *testing.T) {
10471047
CertificateKey: expectedPEMPath,
10481048
},
10491049
Locations: getExpectedLocations(true),
1050-
Port: 8443,
1050+
Listen: "8443",
10511051
GRPC: true,
10521052
Includes: []string{
10531053
includesFolder + "/server-addition-1.conf",
@@ -1058,7 +1058,7 @@ func TestCreateServers(t *testing.T) {
10581058

10591059
g := NewWithT(t)
10601060

1061-
result, httpMatchPair := createServers(httpServers, sslServers)
1061+
result, httpMatchPair := createServers(httpServers, sslServers, []dataplane.Layer4Server{})
10621062

10631063
g.Expect(httpMatchPair).To(Equal(allExpMatchPair))
10641064
g.Expect(helpers.Diff(expectedServers, result)).To(BeEmpty())
@@ -1256,18 +1256,18 @@ func TestCreateServersConflicts(t *testing.T) {
12561256
expectedServers := []http.Server{
12571257
{
12581258
IsDefaultHTTP: true,
1259-
Port: 8080,
1259+
Listen: "8080",
12601260
},
12611261
{
12621262
ServerName: "cafe.example.com",
12631263
Locations: test.expLocs,
1264-
Port: 8080,
1264+
Listen: "8080",
12651265
},
12661266
}
12671267

12681268
g := NewWithT(t)
12691269

1270-
result, _ := createServers(httpServers, []dataplane.VirtualServer{})
1270+
result, _ := createServers(httpServers, []dataplane.VirtualServer{}, []dataplane.Layer4Server{})
12711271
g.Expect(helpers.Diff(expectedServers, result)).To(BeEmpty())
12721272
})
12731273
}

0 commit comments

Comments
 (0)