Skip to content

Commit

Permalink
[template] Allow passing in a template string using RawTemplate
Browse files Browse the repository at this point in the history
Right all templates are expected to be local files.
This change allows passing in optional templates via RawTemplate
where the template is a full templace string.

Signed-off-by: Martin Schuppert <[email protected]>
  • Loading branch information
stuggi committed Jan 10, 2025
1 parent d172b3a commit df5302f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
31 changes: 13 additions & 18 deletions modules/common/util/template_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Template struct {
InstanceType string // the CRD name in lower case, to separate the templates for each CRD in /templates
SecretType corev1.SecretType // Secrets only, defaults to "Opaque"
AdditionalTemplate map[string]string // templates which are common to multiple CRDs can be located in a shared folder and added via this type into the resulting CM/secret
RawTemplate map[string]string // templates to render which are not accessable files which were read by the caller from some other source, like a secret
CustomData map[string]string // custom data which won't get rendered as a template and just added to the resulting cm/secret
Labels map[string]string // labels to be set on the cm/secret
Annotations map[string]string // Annotations set on cm/secret
Expand Down Expand Up @@ -273,24 +274,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 @@ -330,5 +315,15 @@ func GetTemplateData(t Template) (map[string]string, error) {
data[filename] = renderedTemplate
}

// render templates passed in as string via the RawTemplate
for filename, tmplData := range t.RawTemplate {
renderedTemplate, err := ExecuteTemplateData(tmplData, opts)

if err != nil {
return nil, err
}
data[filename] = renderedTemplate
}

return data, nil
}
29 changes: 29 additions & 0 deletions modules/common/util/template_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,35 @@ func TestGetTemplateData(t *testing.T) {
},
error: false,
},
{
name: "Render TemplateTypeConfig templates with RawTemplate",
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",
},
RawTemplate: 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 df5302f

Please sign in to comment.