Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate default http server in NGINX if http listener exists in Gateway #320

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/nginx/config/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ func TestGenerate(t *testing.T) {

conf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "example.com",
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "example.com",
SSL: &state.SSL{
CertificatePath: "/etc/nginx/secrets/default",
},
},
},
Upstreams: []state.Upstream{
Expand All @@ -45,7 +54,7 @@ func TestGenerate(t *testing.T) {
cfg := string(generator.Generate(conf))

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

if !strings.Contains(cfg, "listen 443") {
Expand Down
64 changes: 32 additions & 32 deletions internal/nginx/config/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,55 @@ func executeServers(conf state.Configuration) []byte {
}

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

servers := make([]http.Server, 0, len(confServers)+2)

if len(httpServers) > 0 {
defaultHTTPServer := createDefaultHTTPServer()

servers = append(servers, defaultHTTPServer)
}

if len(sslServers) > 0 {
defaultSSLServer := createDefaultSSLServer()

servers = append(servers, defaultSSLServer)
for _, s := range httpServers {
servers = append(servers, createServer(s))
}

for _, s := range confServers {
servers = append(servers, createServer(s))
for _, s := range sslServers {
servers = append(servers, createSSLServer(s))
}

return servers
}

func createServer(virtualServer state.VirtualServer) http.Server {
s := http.Server{
ServerName: virtualServer.Hostname,
func createSSLServer(virtualServer state.VirtualServer) http.Server {
if virtualServer.IsDefault {
return createDefaultSSLServer()
}

listenerPort := 80

if virtualServer.SSL != nil {
s.SSL = &http.SSL{
return http.Server{
ServerName: virtualServer.Hostname,
SSL: &http.SSL{
Certificate: virtualServer.SSL.CertificatePath,
CertificateKey: virtualServer.SSL.CertificatePath,
}
},
Locations: createLocations(virtualServer.PathRules, 443),
}
}

func createServer(virtualServer state.VirtualServer) http.Server {
if virtualServer.IsDefault {
return createDefaultHTTPServer()
}

listenerPort = 443
return http.Server{
ServerName: virtualServer.Hostname,
Locations: createLocations(virtualServer.PathRules, 80),
}
}

func createLocations(pathRules []state.PathRule, listenerPort int) []http.Location {
lenPathRules := len(pathRules)

if len(virtualServer.PathRules) == 0 {
// generate default "/" 404 location
s.Locations = []http.Location{{Path: "/", Return: &http.Return{Code: http.StatusNotFound}}}
return s
if lenPathRules == 0 {
return []http.Location{{Path: "/", Return: &http.Return{Code: http.StatusNotFound}}}
}

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

for _, rule := range virtualServer.PathRules {
for _, rule := range pathRules {
matches := make([]httpMatch, 0, len(rule.MatchRules))

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

s.Locations = locs
return s
return locs
}

func createDefaultSSLServer() http.Server {
Expand Down
34 changes: 23 additions & 11 deletions internal/nginx/config/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
func TestExecuteServers(t *testing.T) {
conf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "example.com",
},
Expand All @@ -27,6 +30,9 @@ func TestExecuteServers(t *testing.T) {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "example.com",
SSL: &state.SSL{
Expand Down Expand Up @@ -76,48 +82,48 @@ func TestExecuteForDefaultServers(t *testing.T) {
conf: state.Configuration{},
httpDefault: false,
sslDefault: false,
msg: "no servers",
msg: "no default servers",
},
{
conf: state.Configuration{
HTTPServers: []state.VirtualServer{
{
Hostname: "example.com",
IsDefault: true,
},
},
},
httpDefault: true,
sslDefault: false,
msg: "only HTTP servers",
msg: "only HTTP default server",
},
{
conf: state.Configuration{
SSLServers: []state.VirtualServer{
{
Hostname: "example.com",
IsDefault: true,
},
},
},
httpDefault: false,
sslDefault: true,
msg: "only HTTPS servers",
msg: "only HTTPS default server",
},
{
conf: state.Configuration{
HTTPServers: []state.VirtualServer{
{
Hostname: "example.com",
IsDefault: true,
},
},
SSLServers: []state.VirtualServer{
{
Hostname: "example.com",
IsDefault: true,
},
},
},
httpDefault: true,
sslDefault: true,
msg: "both HTTP and HTTPS servers",
msg: "both HTTP and HTTPS default servers",
},
}

Expand Down Expand Up @@ -398,13 +404,19 @@ func TestCreateServers(t *testing.T) {
}

httpServers := []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "cafe.example.com",
PathRules: cafePathRules,
},
}

sslServers := []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "cafe.example.com",
SSL: &state.SSL{CertificatePath: certPath},
Expand Down Expand Up @@ -494,13 +506,13 @@ func TestCreateServers(t *testing.T) {
{
IsDefaultHTTP: true,
},
{
IsDefaultSSL: true,
},
{
ServerName: "cafe.example.com",
Locations: getExpectedLocations(false),
},
{
IsDefaultSSL: true,
},
{
ServerName: "cafe.example.com",
SSL: &http.SSL{Certificate: certPath, CertificateKey: certPath},
Expand Down
51 changes: 50 additions & 1 deletion internal/state/change_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand All @@ -347,6 +350,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down Expand Up @@ -432,6 +438,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand All @@ -450,6 +459,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down Expand Up @@ -535,6 +547,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand All @@ -553,6 +568,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down Expand Up @@ -637,6 +655,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand All @@ -655,6 +676,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down Expand Up @@ -736,6 +760,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand All @@ -754,6 +781,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand Down Expand Up @@ -832,6 +862,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
PathRules: []state.PathRule{
Expand All @@ -850,6 +883,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "foo.example.com",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down Expand Up @@ -946,6 +982,9 @@ var _ = Describe("ChangeProcessor", func() {

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "bar.example.com",
PathRules: []state.PathRule{
Expand All @@ -964,6 +1003,9 @@ var _ = Describe("ChangeProcessor", func() {
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "bar.example.com",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down Expand Up @@ -1038,8 +1080,15 @@ var _ = Describe("ChangeProcessor", func() {
)

expectedConf := state.Configuration{
HTTPServers: []state.VirtualServer{},
HTTPServers: []state.VirtualServer{
{
IsDefault: true,
},
},
SSLServers: []state.VirtualServer{
{
IsDefault: true,
},
{
Hostname: "~^",
SSL: &state.SSL{CertificatePath: certificatePath},
Expand Down
Loading