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

fix: Fix reload errors due long matching conditions #1829

Merged
merged 29 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3cbc1ec
fix: fix reload errors due long matching conditions
salonichf5 Apr 9, 2024
26d766a
add redirect to export list
salonichf5 Apr 17, 2024
f0da36d
update based on reviews
salonichf5 Apr 22, 2024
a0e31b0
remove print statements
salonichf5 Apr 22, 2024
44c632d
fix unit test
salonichf5 Apr 22, 2024
e40a5cc
update unit tests
salonichf5 Apr 22, 2024
d96fcaa
Update internal/mode/static/nginx/config/generator.go
salonichf5 Apr 22, 2024
7897819
Update internal/mode/static/nginx/config/servers.go
salonichf5 Apr 22, 2024
81b5655
update key for match.json
salonichf5 Apr 22, 2024
6153ddb
format njs code
salonichf5 Apr 24, 2024
75cdb8f
Update internal/mode/static/nginx/config/generator.go
salonichf5 Apr 24, 2024
4ad2de1
Update internal/mode/static/nginx/config/servers.go
salonichf5 Apr 24, 2024
d0afa69
review comments
salonichf5 Apr 24, 2024
8f117c7
improve httpmatches.js
salonichf5 Apr 25, 2024
5ca49f7
address reviews
salonichf5 Apr 25, 2024
3683dd7
add more unit test coverage for njs unit tests
salonichf5 Apr 25, 2024
6e7e79e
Update internal/mode/static/nginx/config/servers.go
salonichf5 Apr 25, 2024
b459205
Update internal/mode/static/nginx/config/servers.go
salonichf5 Apr 25, 2024
02653ce
Update internal/mode/static/nginx/config/servers_test.go
salonichf5 Apr 25, 2024
7bf61f3
Update internal/mode/static/nginx/config/servers_test.go
salonichf5 Apr 25, 2024
0a4a7ac
Update internal/mode/static/nginx/config/servers.go
salonichf5 Apr 25, 2024
f63f317
Update internal/mode/static/nginx/config/servers.go
salonichf5 Apr 25, 2024
cc9a19e
update based on reviews
salonichf5 Apr 25, 2024
27f9e53
rearrange expect statements to avoid panic
salonichf5 Apr 25, 2024
c637c6d
fix unit test
salonichf5 Apr 25, 2024
07fbf39
fix httpmatches
salonichf5 Apr 29, 2024
2dcb7c4
update error message
salonichf5 Apr 29, 2024
b744b70
Merge branch 'main' into bug/http-match
salonichf5 Apr 29, 2024
377d9f7
Merge branch 'main' into bug/http-match
salonichf5 Apr 29, 2024
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
42 changes: 32 additions & 10 deletions internal/mode/static/nginx/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (

// configVersionFile is the path to the config version configuration file.
configVersionFile = httpFolder + "/config-version.conf"

// httpMatchVarsFile is the path to the http_match pairs configuration file.
httpMatchVarsFile = httpFolder + "/matches.json"
)

// ConfigFolders is a list of folders where NGINX configuration files are stored.
Expand Down Expand Up @@ -52,8 +55,13 @@ func NewGeneratorImpl(plus bool) GeneratorImpl {
return GeneratorImpl{plus: plus}
}

type executeResult struct {
dest string
data []byte
}

// executeFunc is a function that generates NGINX configuration from internal representation.
type executeFunc func(configuration dataplane.Configuration) []byte
type executeFunc func(configuration dataplane.Configuration) []executeResult

// Generate generates NGINX configuration files from internal representation.
// It is the responsibility of the caller to validate the configuration before calling this function.
Expand All @@ -66,7 +74,7 @@ func (g GeneratorImpl) Generate(conf dataplane.Configuration) []file.File {
files = append(files, generatePEM(id, pair.Cert, pair.Key))
}

files = append(files, g.generateHTTPConfig(conf))
files = append(files, g.generateHTTPConfig(conf)...)

files = append(files, generateConfigVersion(conf.Version))

Expand Down Expand Up @@ -106,24 +114,38 @@ func generateCertBundleFileName(id dataplane.CertBundleID) string {
return filepath.Join(secretsFolder, string(id)+".crt")
}

func (g GeneratorImpl) generateHTTPConfig(conf dataplane.Configuration) file.File {
var c []byte
func (g GeneratorImpl) generateHTTPConfig(conf dataplane.Configuration) []file.File {
fileBytes := make(map[string][]byte)

for _, execute := range g.getExecuteFuncs() {
c = append(c, execute(conf)...)
results := execute(conf)
for _, res := range results {
_, ok := fileBytes[res.dest]
if ok {
fileBytes[res.dest] = append(fileBytes[res.dest], res.data...)
} else {
fileBytes[res.dest] = res.data
}
}
}

return file.File{
Content: c,
Path: httpConfigFile,
Type: file.TypeRegular,
files := make([]file.File, 0, len(fileBytes))
for filepath, bytes := range fileBytes {
files = append(files, file.File{
Path: filepath,
Content: bytes,
Type: file.TypeRegular,
})
}

return files
}

func (g GeneratorImpl) getExecuteFuncs() []executeFunc {
return []executeFunc{
executeServers,
g.executeUpstreams,
executeSplitClients,
executeServers,
executeMaps,
}
}
Expand Down
28 changes: 19 additions & 9 deletions internal/mode/static/nginx/config/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config_test

import (
"fmt"
"sort"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -70,13 +71,16 @@ func TestGenerate(t *testing.T) {

files := generator.Generate(conf)

g.Expect(files).To(HaveLen(4))
g.Expect(files).To(HaveLen(5))
arrange := func(i, j int) bool {
return files[i].Path < files[j].Path
}
sort.Slice(files, arrange)

g.Expect(files[0]).To(Equal(file.File{
Type: file.TypeSecret,
Path: "/etc/nginx/secrets/test-keypair.pem",
Content: []byte("test-cert\ntest-key"),
}))
g.Expect(files[0].Type).To(Equal(file.TypeRegular))
g.Expect(files[0].Path).To(Equal("/etc/nginx/conf.d/config-version.conf"))
configVersion := string(files[0].Content)
g.Expect(configVersion).To(ContainSubstring(fmt.Sprintf("return 200 %d", conf.Version)))

g.Expect(files[1].Type).To(Equal(file.TypeRegular))
g.Expect(files[1].Path).To(Equal("/etc/nginx/conf.d/http.conf"))
Expand All @@ -88,12 +92,18 @@ func TestGenerate(t *testing.T) {
g.Expect(httpCfg).To(ContainSubstring("upstream"))
g.Expect(httpCfg).To(ContainSubstring("split_clients"))

g.Expect(files[2].Path).To(Equal("/etc/nginx/conf.d/matches.json"))
salonichf5 marked this conversation as resolved.
Show resolved Hide resolved
g.Expect(files[2].Type).To(Equal(file.TypeRegular))
g.Expect(files[2].Path).To(Equal("/etc/nginx/conf.d/config-version.conf"))
configVersion := string(files[2].Content)
g.Expect(configVersion).To(ContainSubstring(fmt.Sprintf("return 200 %d", conf.Version)))
expString := "{}"
g.Expect(string(files[2].Content)).To(Equal(expString))

g.Expect(files[3].Path).To(Equal("/etc/nginx/secrets/test-certbundle.crt"))
certBundle := string(files[3].Content)
g.Expect(certBundle).To(Equal("test-cert"))

g.Expect(files[4]).To(Equal(file.File{
Type: file.TypeSecret,
Path: "/etc/nginx/secrets/test-keypair.pem",
Content: []byte("test-cert\ntest-key"),
}))
}
8 changes: 4 additions & 4 deletions internal/mode/static/nginx/config/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type Server struct {

// Location holds all configuration for an HTTP location.
type Location struct {
Return *Return
ProxySSLVerify *ProxySSLVerify
Path string
ProxyPass string
HTTPMatchVar string
Rewrites []string
HTTPMatchKey string
ProxySetHeaders []Header
ProxySSLVerify *ProxySSLVerify
Return *Return
Rewrites []string
}

// Header defines a HTTP header to be passed to the proxied server.
Expand Down
8 changes: 6 additions & 2 deletions internal/mode/static/nginx/config/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import (

var mapsTemplate = gotemplate.Must(gotemplate.New("maps").Parse(mapsTemplateText))

func executeMaps(conf dataplane.Configuration) []byte {
func executeMaps(conf dataplane.Configuration) []executeResult {
maps := buildAddHeaderMaps(append(conf.HTTPServers, conf.SSLServers...))
return execute(mapsTemplate, maps)
result := executeResult{
dest: httpConfigFile,
data: execute(mapsTemplate, maps),
}
return []executeResult{result}
}

func buildAddHeaderMaps(servers []dataplane.VirtualServer) []http.Map {
Expand Down
5 changes: 4 additions & 1 deletion internal/mode/static/nginx/config/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ func TestExecuteMaps(t *testing.T) {
"map $http_upgrade $connection_upgrade {": 1,
}

maps := string(executeMaps(conf))
mapResult := executeMaps(conf)
g.Expect(mapResult).To(HaveLen(1))
maps := string(mapResult[0].data)
salonichf5 marked this conversation as resolved.
Show resolved Hide resolved
g.Expect(mapResult[0].dest).To(Equal(httpConfigFile))
for expSubStr, expCount := range expSubStrings {
g.Expect(expCount).To(Equal(strings.Count(maps, expSubStr)))
}
Expand Down
Loading
Loading