diff --git a/internal/pkg/rpaas/nginx/configuration_render.go b/internal/pkg/rpaas/nginx/configuration_render.go index 462d381ae..561e4c5be 100644 --- a/internal/pkg/rpaas/nginx/configuration_render.go +++ b/internal/pkg/rpaas/nginx/configuration_render.go @@ -7,16 +7,18 @@ package nginx import ( "bytes" "fmt" + "regexp" "strconv" "strings" "text/template" - "k8s.io/apimachinery/pkg/api/resource" - "github.com/tsuru/rpaas-operator/api/v1alpha1" "github.com/tsuru/rpaas-operator/pkg/util" + "k8s.io/apimachinery/pkg/api/resource" ) +var trimTrailingSpacesRegex = regexp.MustCompile(`[ \t]+?\n`) + type ConfigurationRenderer interface { Render(ConfigurationData) (string, error) } @@ -42,7 +44,11 @@ type rpaasConfigurationRenderer struct { func (r *rpaasConfigurationRenderer) Render(c ConfigurationData) (string, error) { buffer := &bytes.Buffer{} err := r.t.Execute(buffer, c) - return buffer.String(), err + if err != nil { + return "", err + } + result := buffer.String() + return trimTrailingSpacesRegex.ReplaceAllString(result, "\n"), nil } func NewConfigurationRenderer(cb ConfigurationBlocks) (ConfigurationRenderer, error) { @@ -237,7 +243,7 @@ worker_processes {{ . }}; include modules/*.conf; -{{- template "root" . }} +{{ template "root" . }} events { {{- with $config.WorkerConnections }} @@ -331,7 +337,7 @@ http { {{- end }} init_by_lua_block { - {{- template "lua-server" . }} + {{ template "lua-server" . }} } init_worker_by_lua_block { @@ -346,10 +352,10 @@ http { {{- end }} {{- end }} - {{- template "lua-worker" . }} + {{ template "lua-worker" . }} } - {{- template "http" . }} + {{ template "http" . }} server { listen {{ managePort $instance }}; @@ -443,7 +449,7 @@ http { {{- end}} {{- end}} - {{- template "server" .}} + {{ template "server" .}} } } ` diff --git a/internal/pkg/rpaas/nginx/configuration_render_test.go b/internal/pkg/rpaas/nginx/configuration_render_test.go index 1e04e1815..d963e036e 100644 --- a/internal/pkg/rpaas/nginx/configuration_render_test.go +++ b/internal/pkg/rpaas/nginx/configuration_render_test.go @@ -40,17 +40,17 @@ func TestRpaasConfigurationRenderer_Render(t *testing.T) { assert.Regexp(t, `error_log /dev/stderr;`, result) assert.Regexp(t, `server {\n\s+listen 8800;\n\s+}\n+`, result) assert.Regexp(t, `server { -\s+listen 8080 default_server;\n+ -\s+location = /_nginx_healthcheck {\n+ -\s+access_log off;\n+ -\s+default_type "text/plain"; -\s+return 200 "WORKING\\n"; -\s+} -\s+location / { -\s+default_type "text/plain"; -\s+return 404 "instance not bound\\n"; -\s+} -\s+}\n+`, result) +[ ]+listen 8080 default_server;\n+ +[ ]+location = /_nginx_healthcheck {\n+ +[ ]+access_log off;\n+ +[ ]+default_type "text/plain"; +[ ]+return 200 "WORKING\\n"; +[ ]+} +[ ]+location / { +[ ]+default_type "text/plain"; +[ ]+return 404 "instance not bound\\n"; +[ ]+}\n+ +[ ]+}`, result) }, }, { @@ -250,11 +250,11 @@ func TestRpaasConfigurationRenderer_Render(t *testing.T) { Instance: &v1alpha1.RpaasInstance{}, }, assertion: func(t *testing.T, result string) { - assert.Regexp(t, `# some custom conf at root context`, result) - assert.Regexp(t, `# some custom conf at http context`, result) - assert.Regexp(t, `# some custom conf at server context`, result) - assert.Regexp(t, `# some custom conf at init_by_lua_block context`, result) - assert.Regexp(t, `# some custom conf at init_worker_by_lua_block context`, result) + assert.Regexp(t, `\s# some custom conf at root context`, result) + assert.Regexp(t, `\s# some custom conf at http context`, result) + assert.Regexp(t, `\s# some custom conf at server context`, result) + assert.Regexp(t, `\s# some custom conf at init_by_lua_block context`, result) + assert.Regexp(t, `\s# some custom conf at init_worker_by_lua_block context`, result) }, }, {