Skip to content

Commit 6762a95

Browse files
authored
Generate default http server if http listener exists (#320)
* Generate default http server if http listener exists * listeners -> httpsListeners
1 parent 894ec3f commit 6762a95

File tree

6 files changed

+155
-53
lines changed

6 files changed

+155
-53
lines changed

internal/nginx/config/generator_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,22 @@ func TestGenerate(t *testing.T) {
2424

2525
conf := state.Configuration{
2626
HTTPServers: []state.VirtualServer{
27+
{
28+
IsDefault: true,
29+
},
2730
{
2831
Hostname: "example.com",
2932
},
3033
},
3134
SSLServers: []state.VirtualServer{
35+
{
36+
IsDefault: true,
37+
},
3238
{
3339
Hostname: "example.com",
40+
SSL: &state.SSL{
41+
CertificatePath: "/etc/nginx/secrets/default",
42+
},
3443
},
3544
},
3645
Upstreams: []state.Upstream{
@@ -45,7 +54,7 @@ func TestGenerate(t *testing.T) {
4554
cfg := string(generator.Generate(conf))
4655

4756
if !strings.Contains(cfg, "listen 80") {
48-
t.Errorf("Generate() did not generate a config with an HTTP server; config: %s", cfg)
57+
t.Errorf("Generate() did not generate a config with a default HTTP server; config: %s", cfg)
4958
}
5059

5160
if !strings.Contains(cfg, "listen 443") {

internal/nginx/config/servers.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,55 @@ func executeServers(conf state.Configuration) []byte {
2121
}
2222

2323
func createServers(httpServers, sslServers []state.VirtualServer) []http.Server {
24-
confServers := append(httpServers, sslServers...)
24+
servers := make([]http.Server, 0, len(httpServers)+len(sslServers))
2525

26-
servers := make([]http.Server, 0, len(confServers)+2)
27-
28-
if len(httpServers) > 0 {
29-
defaultHTTPServer := createDefaultHTTPServer()
30-
31-
servers = append(servers, defaultHTTPServer)
32-
}
33-
34-
if len(sslServers) > 0 {
35-
defaultSSLServer := createDefaultSSLServer()
36-
37-
servers = append(servers, defaultSSLServer)
26+
for _, s := range httpServers {
27+
servers = append(servers, createServer(s))
3828
}
3929

40-
for _, s := range confServers {
41-
servers = append(servers, createServer(s))
30+
for _, s := range sslServers {
31+
servers = append(servers, createSSLServer(s))
4232
}
4333

4434
return servers
4535
}
4636

47-
func createServer(virtualServer state.VirtualServer) http.Server {
48-
s := http.Server{
49-
ServerName: virtualServer.Hostname,
37+
func createSSLServer(virtualServer state.VirtualServer) http.Server {
38+
if virtualServer.IsDefault {
39+
return createDefaultSSLServer()
5040
}
5141

52-
listenerPort := 80
53-
54-
if virtualServer.SSL != nil {
55-
s.SSL = &http.SSL{
42+
return http.Server{
43+
ServerName: virtualServer.Hostname,
44+
SSL: &http.SSL{
5645
Certificate: virtualServer.SSL.CertificatePath,
5746
CertificateKey: virtualServer.SSL.CertificatePath,
58-
}
47+
},
48+
Locations: createLocations(virtualServer.PathRules, 443),
49+
}
50+
}
51+
52+
func createServer(virtualServer state.VirtualServer) http.Server {
53+
if virtualServer.IsDefault {
54+
return createDefaultHTTPServer()
55+
}
5956

60-
listenerPort = 443
57+
return http.Server{
58+
ServerName: virtualServer.Hostname,
59+
Locations: createLocations(virtualServer.PathRules, 80),
6160
}
61+
}
62+
63+
func createLocations(pathRules []state.PathRule, listenerPort int) []http.Location {
64+
lenPathRules := len(pathRules)
6265

63-
if len(virtualServer.PathRules) == 0 {
64-
// generate default "/" 404 location
65-
s.Locations = []http.Location{{Path: "/", Return: &http.Return{Code: http.StatusNotFound}}}
66-
return s
66+
if lenPathRules == 0 {
67+
return []http.Location{{Path: "/", Return: &http.Return{Code: http.StatusNotFound}}}
6768
}
6869

69-
locs := make([]http.Location, 0, len(virtualServer.PathRules)) // FIXME(pleshakov): expand with rule.Routes
70+
locs := make([]http.Location, 0, lenPathRules) // FIXME(pleshakov): expand with rule.Routes
7071

71-
for _, rule := range virtualServer.PathRules {
72+
for _, rule := range pathRules {
7273
matches := make([]httpMatch, 0, len(rule.MatchRules))
7374

7475
for matchRuleIdx, r := range rule.MatchRules {
@@ -129,8 +130,7 @@ func createServer(virtualServer state.VirtualServer) http.Server {
129130
}
130131
}
131132

132-
s.Locations = locs
133-
return s
133+
return locs
134134
}
135135

136136
func createDefaultSSLServer() http.Server {

internal/nginx/config/servers_test.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
func TestExecuteServers(t *testing.T) {
2020
conf := state.Configuration{
2121
HTTPServers: []state.VirtualServer{
22+
{
23+
IsDefault: true,
24+
},
2225
{
2326
Hostname: "example.com",
2427
},
@@ -27,6 +30,9 @@ func TestExecuteServers(t *testing.T) {
2730
},
2831
},
2932
SSLServers: []state.VirtualServer{
33+
{
34+
IsDefault: true,
35+
},
3036
{
3137
Hostname: "example.com",
3238
SSL: &state.SSL{
@@ -76,48 +82,48 @@ func TestExecuteForDefaultServers(t *testing.T) {
7682
conf: state.Configuration{},
7783
httpDefault: false,
7884
sslDefault: false,
79-
msg: "no servers",
85+
msg: "no default servers",
8086
},
8187
{
8288
conf: state.Configuration{
8389
HTTPServers: []state.VirtualServer{
8490
{
85-
Hostname: "example.com",
91+
IsDefault: true,
8692
},
8793
},
8894
},
8995
httpDefault: true,
9096
sslDefault: false,
91-
msg: "only HTTP servers",
97+
msg: "only HTTP default server",
9298
},
9399
{
94100
conf: state.Configuration{
95101
SSLServers: []state.VirtualServer{
96102
{
97-
Hostname: "example.com",
103+
IsDefault: true,
98104
},
99105
},
100106
},
101107
httpDefault: false,
102108
sslDefault: true,
103-
msg: "only HTTPS servers",
109+
msg: "only HTTPS default server",
104110
},
105111
{
106112
conf: state.Configuration{
107113
HTTPServers: []state.VirtualServer{
108114
{
109-
Hostname: "example.com",
115+
IsDefault: true,
110116
},
111117
},
112118
SSLServers: []state.VirtualServer{
113119
{
114-
Hostname: "example.com",
120+
IsDefault: true,
115121
},
116122
},
117123
},
118124
httpDefault: true,
119125
sslDefault: true,
120-
msg: "both HTTP and HTTPS servers",
126+
msg: "both HTTP and HTTPS default servers",
121127
},
122128
}
123129

@@ -398,13 +404,19 @@ func TestCreateServers(t *testing.T) {
398404
}
399405

400406
httpServers := []state.VirtualServer{
407+
{
408+
IsDefault: true,
409+
},
401410
{
402411
Hostname: "cafe.example.com",
403412
PathRules: cafePathRules,
404413
},
405414
}
406415

407416
sslServers := []state.VirtualServer{
417+
{
418+
IsDefault: true,
419+
},
408420
{
409421
Hostname: "cafe.example.com",
410422
SSL: &state.SSL{CertificatePath: certPath},
@@ -494,13 +506,13 @@ func TestCreateServers(t *testing.T) {
494506
{
495507
IsDefaultHTTP: true,
496508
},
497-
{
498-
IsDefaultSSL: true,
499-
},
500509
{
501510
ServerName: "cafe.example.com",
502511
Locations: getExpectedLocations(false),
503512
},
513+
{
514+
IsDefaultSSL: true,
515+
},
504516
{
505517
ServerName: "cafe.example.com",
506518
SSL: &http.SSL{Certificate: certPath, CertificateKey: certPath},

internal/state/change_processor_test.go

+50-1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ var _ = Describe("ChangeProcessor", func() {
329329

330330
expectedConf := state.Configuration{
331331
HTTPServers: []state.VirtualServer{
332+
{
333+
IsDefault: true,
334+
},
332335
{
333336
Hostname: "foo.example.com",
334337
PathRules: []state.PathRule{
@@ -347,6 +350,9 @@ var _ = Describe("ChangeProcessor", func() {
347350
},
348351
},
349352
SSLServers: []state.VirtualServer{
353+
{
354+
IsDefault: true,
355+
},
350356
{
351357
Hostname: "foo.example.com",
352358
SSL: &state.SSL{CertificatePath: certificatePath},
@@ -432,6 +438,9 @@ var _ = Describe("ChangeProcessor", func() {
432438

433439
expectedConf := state.Configuration{
434440
HTTPServers: []state.VirtualServer{
441+
{
442+
IsDefault: true,
443+
},
435444
{
436445
Hostname: "foo.example.com",
437446
PathRules: []state.PathRule{
@@ -450,6 +459,9 @@ var _ = Describe("ChangeProcessor", func() {
450459
},
451460
},
452461
SSLServers: []state.VirtualServer{
462+
{
463+
IsDefault: true,
464+
},
453465
{
454466
Hostname: "foo.example.com",
455467
SSL: &state.SSL{CertificatePath: certificatePath},
@@ -535,6 +547,9 @@ var _ = Describe("ChangeProcessor", func() {
535547

536548
expectedConf := state.Configuration{
537549
HTTPServers: []state.VirtualServer{
550+
{
551+
IsDefault: true,
552+
},
538553
{
539554
Hostname: "foo.example.com",
540555
PathRules: []state.PathRule{
@@ -553,6 +568,9 @@ var _ = Describe("ChangeProcessor", func() {
553568
},
554569
},
555570
SSLServers: []state.VirtualServer{
571+
{
572+
IsDefault: true,
573+
},
556574
{
557575
Hostname: "foo.example.com",
558576
SSL: &state.SSL{CertificatePath: certificatePath},
@@ -637,6 +655,9 @@ var _ = Describe("ChangeProcessor", func() {
637655

638656
expectedConf := state.Configuration{
639657
HTTPServers: []state.VirtualServer{
658+
{
659+
IsDefault: true,
660+
},
640661
{
641662
Hostname: "foo.example.com",
642663
PathRules: []state.PathRule{
@@ -655,6 +676,9 @@ var _ = Describe("ChangeProcessor", func() {
655676
},
656677
},
657678
SSLServers: []state.VirtualServer{
679+
{
680+
IsDefault: true,
681+
},
658682
{
659683
Hostname: "foo.example.com",
660684
SSL: &state.SSL{CertificatePath: certificatePath},
@@ -736,6 +760,9 @@ var _ = Describe("ChangeProcessor", func() {
736760

737761
expectedConf := state.Configuration{
738762
HTTPServers: []state.VirtualServer{
763+
{
764+
IsDefault: true,
765+
},
739766
{
740767
Hostname: "foo.example.com",
741768
PathRules: []state.PathRule{
@@ -754,6 +781,9 @@ var _ = Describe("ChangeProcessor", func() {
754781
},
755782
},
756783
SSLServers: []state.VirtualServer{
784+
{
785+
IsDefault: true,
786+
},
757787
{
758788
Hostname: "foo.example.com",
759789
PathRules: []state.PathRule{
@@ -832,6 +862,9 @@ var _ = Describe("ChangeProcessor", func() {
832862

833863
expectedConf := state.Configuration{
834864
HTTPServers: []state.VirtualServer{
865+
{
866+
IsDefault: true,
867+
},
835868
{
836869
Hostname: "foo.example.com",
837870
PathRules: []state.PathRule{
@@ -850,6 +883,9 @@ var _ = Describe("ChangeProcessor", func() {
850883
},
851884
},
852885
SSLServers: []state.VirtualServer{
886+
{
887+
IsDefault: true,
888+
},
853889
{
854890
Hostname: "foo.example.com",
855891
SSL: &state.SSL{CertificatePath: certificatePath},
@@ -946,6 +982,9 @@ var _ = Describe("ChangeProcessor", func() {
946982

947983
expectedConf := state.Configuration{
948984
HTTPServers: []state.VirtualServer{
985+
{
986+
IsDefault: true,
987+
},
949988
{
950989
Hostname: "bar.example.com",
951990
PathRules: []state.PathRule{
@@ -964,6 +1003,9 @@ var _ = Describe("ChangeProcessor", func() {
9641003
},
9651004
},
9661005
SSLServers: []state.VirtualServer{
1006+
{
1007+
IsDefault: true,
1008+
},
9671009
{
9681010
Hostname: "bar.example.com",
9691011
SSL: &state.SSL{CertificatePath: certificatePath},
@@ -1038,8 +1080,15 @@ var _ = Describe("ChangeProcessor", func() {
10381080
)
10391081

10401082
expectedConf := state.Configuration{
1041-
HTTPServers: []state.VirtualServer{},
1083+
HTTPServers: []state.VirtualServer{
1084+
{
1085+
IsDefault: true,
1086+
},
1087+
},
10421088
SSLServers: []state.VirtualServer{
1089+
{
1090+
IsDefault: true,
1091+
},
10431092
{
10441093
Hostname: "~^",
10451094
SSL: &state.SSL{CertificatePath: certificatePath},

0 commit comments

Comments
 (0)