Skip to content

Commit 5941b19

Browse files
committed
[template] Allow passing in a template string as AdditionalTemplate
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]>
1 parent d172b3a commit 5941b19

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

modules/common/util/template_util.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -273,24 +273,8 @@ func ExecuteTemplateFile(filename string, data interface{}) (string, error) {
273273
return "", err
274274
}
275275
file := string(b)
276-
var buff bytes.Buffer
277-
funcs := template.FuncMap{
278-
"add": add,
279-
"execTempl": execTempl,
280-
"indent": indent,
281-
"lower": lower,
282-
"removeNewLines": removeNewLines,
283-
"removeNewLinesInSections": removeNewLinesInSections,
284-
}
285-
tmpl, err = template.New("tmp").Option("missingkey=error").Funcs(funcs).Parse(file)
286-
if err != nil {
287-
return "", err
288-
}
289-
err = tmpl.Execute(&buff, data)
290-
if err != nil {
291-
return "", err
292-
}
293-
return buff.String(), nil
276+
277+
return ExecuteTemplateData(file, data)
294278
}
295279

296280
// GetTemplateData - Renders templates specified via Template struct
@@ -320,10 +304,23 @@ func GetTemplateData(t Template) (map[string]string, error) {
320304
data[filepath.Base(file)] = renderedData
321305
}
322306
}
323-
// add additional template files from different directory, which
307+
// add additional template files from different directory,
324308
// e.g. can be common to multiple controllers
325-
for filename, file := range t.AdditionalTemplate {
326-
renderedTemplate, err := ExecuteTemplateFile(file, opts)
309+
// or when the value of the AdditionalTemplate is a string containing spaces
310+
// or new lins as a template as input string e.g. when the template was provided
311+
// via some other input, e.g. as part of a secret
312+
for filename, tmplData := range t.AdditionalTemplate {
313+
var renderedTemplate string
314+
var err error
315+
// if tmplData contains either new lines or spaces its expected
316+
// that tmplData is a template as a string, not a path to
317+
// a template file
318+
if strings.Contains(tmplData, "\n") || strings.Contains(tmplData, " ") {
319+
renderedTemplate, err = ExecuteTemplateData(tmplData, opts)
320+
} else {
321+
renderedTemplate, err = ExecuteTemplateFile(tmplData, opts)
322+
}
323+
327324
if err != nil {
328325
return nil, err
329326
}

modules/common/util/template_util_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func TestGetTemplateData(t *testing.T) {
398398
error: false,
399399
},
400400
{
401-
name: "Render TemplateTypeConfig templates with AdditionalTemplate",
401+
name: "Render TemplateTypeConfig templates with AdditionalTemplate which is a file",
402402
tmpl: Template{
403403
Name: "testservice",
404404
Namespace: "somenamespace",
@@ -421,6 +421,35 @@ func TestGetTemplateData(t *testing.T) {
421421
},
422422
error: false,
423423
},
424+
{
425+
name: "Render TemplateTypeConfig templates with AdditionalTemplate which is a template string",
426+
tmpl: Template{
427+
Name: "testservice",
428+
Namespace: "somenamespace",
429+
Type: TemplateTypeConfig,
430+
InstanceType: "testservice",
431+
Version: "",
432+
ConfigOptions: map[string]interface{}{
433+
"ServiceUser": "foo",
434+
"Count": 1,
435+
"Upper": "BAR",
436+
"Message": "some common func",
437+
},
438+
AdditionalTemplate: map[string]string{"common.sh": `#!/bin/bash
439+
set -e
440+
441+
function common_func {
442+
echo {{ .Message }}
443+
}`},
444+
},
445+
want: map[string]string{
446+
"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",
447+
"config.json": "{\n \"command\": \"/usr/sbin/httpd -DFOREGROUND\",\n}\n",
448+
"foo.conf": "username = foo\ncount = 1\nadd = 3\nlower = bar\n",
449+
"common.sh": "#!/bin/bash\nset -e\n\nfunction common_func {\n echo some common func\n}",
450+
},
451+
error: false,
452+
},
424453
{
425454
name: "Render TemplateTypeNone templates with AdditionalTemplate",
426455
tmpl: Template{

0 commit comments

Comments
 (0)