Skip to content

Commit

Permalink
Trim trailing whitespace when rendering nginx template
Browse files Browse the repository at this point in the history
Configmaps without trailing whitespaces are easier to read when using
kubectl.
  • Loading branch information
cezarsa committed Dec 3, 2020
1 parent 0489595 commit 81fd394
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
22 changes: 14 additions & 8 deletions internal/pkg/rpaas/nginx/configuration_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down Expand Up @@ -237,7 +243,7 @@ worker_processes {{ . }};
include modules/*.conf;
{{- template "root" . }}
{{ template "root" . }}
events {
{{- with $config.WorkerConnections }}
Expand Down Expand Up @@ -331,7 +337,7 @@ http {
{{- end }}
init_by_lua_block {
{{- template "lua-server" . }}
{{ template "lua-server" . }}
}
init_worker_by_lua_block {
Expand All @@ -346,10 +352,10 @@ http {
{{- end }}
{{- end }}
{{- template "lua-worker" . }}
{{ template "lua-worker" . }}
}
{{- template "http" . }}
{{ template "http" . }}
server {
listen {{ managePort $instance }};
Expand Down Expand Up @@ -443,7 +449,7 @@ http {
{{- end}}
{{- end}}
{{- template "server" .}}
{{ template "server" .}}
}
}
`
32 changes: 16 additions & 16 deletions internal/pkg/rpaas/nginx/configuration_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
{
Expand Down Expand Up @@ -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)
},
},
{
Expand Down

0 comments on commit 81fd394

Please sign in to comment.