From 8dec8d8581eda995217abd69f9c1d118efee8b40 Mon Sep 17 00:00:00 2001 From: Amit Uniyal Date: Wed, 4 Sep 2024 13:28:16 +0530 Subject: [PATCH] Fixes extra newline from confs As we are using templates to dynamicaly create confs on conditions, Often extra newlines are getting added in confs, we should remove them. --- modules/common/util/template_util.go | 52 +++++++++++++++++++ modules/common/util/template_util_test.go | 4 ++ .../templates/testservice/config/bar.conf | 13 +++++ 3 files changed, 69 insertions(+) create mode 100644 modules/common/util/testdata/templates/testservice/config/bar.conf diff --git a/modules/common/util/template_util.go b/modules/common/util/template_util.go index a641bb89..1b1a9566 100644 --- a/modules/common/util/template_util.go +++ b/modules/common/util/template_util.go @@ -57,6 +57,48 @@ type Template struct { ConfigOptions map[string]interface{} // map of parameters as input data to render the templates SkipSetOwner bool // skip setting ownership on the associated configmap Version string // optional version string to separate templates inside the InstanceType/Type directory. E.g. placementapi/config/18.0 + PostRenderCleanup bool // optional boolean parameter to remove extra new lines from service confs, by default set to false +} + +// This function removes extra space and new-lines from conf data +func removeSubsequentNewLines(rawConf string) string { + lines := strings.Split(rawConf, "\n") + var result []string + var prevLineWasBlank bool + + for _, line := range lines { + trimmedLine := strings.TrimSpace(line) + + if trimmedLine == "" { + prevLineWasBlank = true + // if current line is blank, no need to add it in result + } else { + if strings.HasPrefix(trimmedLine, "[") && strings.HasSuffix(trimmedLine, "]") { + // section-header + if len(result) > 0 && !prevLineWasBlank { + result = append(result, "") + } + var sectionHeaderLine string + if len(result) > 0 { + // new section-hearder + sectionHeaderLine = "\n" + line + } else { + sectionHeaderLine = line + } + result = append(result, sectionHeaderLine) + } else { + // secion-body + result = append(result, line) + } + // reset flag + prevLineWasBlank = false + } + } + + // add an extra line at EOF + result = append(result, "") + + return strings.Join(result, "\n") } // GetTemplatesPath get path to templates, either running local or deployed as container @@ -135,6 +177,7 @@ func ExecuteTemplate(templateFile string, data interface{}) (string, error) { if err != nil { return "", err } + return renderedTemplate, nil } @@ -244,5 +287,14 @@ func GetTemplateData(t Template) (map[string]string, error) { data[filename] = renderedTemplate } + if t.PostRenderCleanup { + for filename, content := range data { + if strings.HasSuffix(filename, ".conf") { + // as of now, only clean for confs + data[filename] = removeSubsequentNewLines(content) + } + } + } + return data, nil } diff --git a/modules/common/util/template_util_test.go b/modules/common/util/template_util_test.go index ef0a8989..36d9ec26 100644 --- a/modules/common/util/template_util_test.go +++ b/modules/common/util/template_util_test.go @@ -136,8 +136,10 @@ func TestGetTemplateData(t *testing.T) { "Upper": "BAR", }, AdditionalTemplate: map[string]string{}, + PostRenderCleanup: true, }, want: map[string]string{ + "bar.conf": "[DEFAULT]\nstate_path = /var/lib/nova\ndebug=true\ncompute_driver = libvirt.LibvirtDriver\n\n[oslo_concurrency]\nlock_path = /var/lib/nova/tmp\n", "config.json": "{\n \"command\": \"/usr/sbin/httpd -DFOREGROUND\",\n}\n", "foo.conf": "username = foo\ncount = 1\nadd = 3\nlower = bar\n", }, @@ -173,11 +175,13 @@ func TestGetTemplateData(t *testing.T) { "Message": "some common func", }, AdditionalTemplate: map[string]string{"common.sh": "/common/common.sh"}, + PostRenderCleanup: true, }, want: map[string]string{ "config.json": "{\n \"command\": \"/usr/sbin/httpd -DFOREGROUND\",\n}\n", "foo.conf": "username = foo\ncount = 1\nadd = 3\nlower = bar\n", "common.sh": "#!/bin/bash\nset -e\n\nfunction common_func {\n echo some common func\n}\n", + "bar.conf": "[DEFAULT]\nstate_path = /var/lib/nova\ndebug=true\ncompute_driver = libvirt.LibvirtDriver\n\n[oslo_concurrency]\nlock_path = /var/lib/nova/tmp\n", }, error: false, }, diff --git a/modules/common/util/testdata/templates/testservice/config/bar.conf b/modules/common/util/testdata/templates/testservice/config/bar.conf new file mode 100644 index 00000000..b0c4ef64 --- /dev/null +++ b/modules/common/util/testdata/templates/testservice/config/bar.conf @@ -0,0 +1,13 @@ +[DEFAULT] +state_path = /var/lib/nova + + +debug=true + +compute_driver = libvirt.LibvirtDriver + + +[oslo_concurrency] +lock_path = /var/lib/nova/tmp + +