Skip to content

Commit

Permalink
[template] Allow passing in a template string as AdditionalTemplate
Browse files Browse the repository at this point in the history
right now AdditionalTemplate expects that the passed in information
is reference to a template file.
This change allows passing in a template via AdditinalTemplate where
the template is a full templace string. To differenciate between the
two its being checked if the value of the AdditionalTemplate map
has either spaces or new lines. If it has it is expected that its
a full template passed in as a string and not a path to a template
file which would be part of an operator image.

Signed-off-by: Martin Schuppert <[email protected]>
  • Loading branch information
stuggi committed Jan 9, 2025
1 parent d172b3a commit 5941b19
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
39 changes: 18 additions & 21 deletions modules/common/util/template_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,8 @@ func ExecuteTemplateFile(filename string, data interface{}) (string, error) {
return "", err
}
file := string(b)
var buff bytes.Buffer
funcs := template.FuncMap{
"add": add,
"execTempl": execTempl,
"indent": indent,
"lower": lower,
"removeNewLines": removeNewLines,
"removeNewLinesInSections": removeNewLinesInSections,
}
tmpl, err = template.New("tmp").Option("missingkey=error").Funcs(funcs).Parse(file)
if err != nil {
return "", err
}
err = tmpl.Execute(&buff, data)
if err != nil {
return "", err
}
return buff.String(), nil

return ExecuteTemplateData(file, data)
}

// GetTemplateData - Renders templates specified via Template struct
Expand Down Expand Up @@ -320,10 +304,23 @@ func GetTemplateData(t Template) (map[string]string, error) {
data[filepath.Base(file)] = renderedData
}
}
// add additional template files from different directory, which
// add additional template files from different directory,
// e.g. can be common to multiple controllers
for filename, file := range t.AdditionalTemplate {
renderedTemplate, err := ExecuteTemplateFile(file, opts)
// or when the value of the AdditionalTemplate is a string containing spaces
// or new lins as a template as input string e.g. when the template was provided
// via some other input, e.g. as part of a secret
for filename, tmplData := range t.AdditionalTemplate {
var renderedTemplate string
var err error
// if tmplData contains either new lines or spaces its expected
// that tmplData is a template as a string, not a path to
// a template file
if strings.Contains(tmplData, "\n") || strings.Contains(tmplData, " ") {
renderedTemplate, err = ExecuteTemplateData(tmplData, opts)
} else {
renderedTemplate, err = ExecuteTemplateFile(tmplData, opts)
}

if err != nil {
return nil, err
}
Expand Down
31 changes: 30 additions & 1 deletion modules/common/util/template_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func TestGetTemplateData(t *testing.T) {
error: false,
},
{
name: "Render TemplateTypeConfig templates with AdditionalTemplate",
name: "Render TemplateTypeConfig templates with AdditionalTemplate which is a file",
tmpl: Template{
Name: "testservice",
Namespace: "somenamespace",
Expand All @@ -421,6 +421,35 @@ func TestGetTemplateData(t *testing.T) {
},
error: false,
},
{
name: "Render TemplateTypeConfig templates with AdditionalTemplate which is a template string",
tmpl: Template{
Name: "testservice",
Namespace: "somenamespace",
Type: TemplateTypeConfig,
InstanceType: "testservice",
Version: "",
ConfigOptions: map[string]interface{}{
"ServiceUser": "foo",
"Count": 1,
"Upper": "BAR",
"Message": "some common func",
},
AdditionalTemplate: map[string]string{"common.sh": `#!/bin/bash
set -e
function common_func {
echo {{ .Message }}
}`},
},
want: map[string]string{
"bar.conf": "[DEFAULT]\nstate_path = /var/lib/nova\ndebug=true\nsome_parameter_with_brackets=[test]\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",
"common.sh": "#!/bin/bash\nset -e\n\nfunction common_func {\n echo some common func\n}",
},
error: false,
},
{
name: "Render TemplateTypeNone templates with AdditionalTemplate",
tmpl: Template{
Expand Down

0 comments on commit 5941b19

Please sign in to comment.