Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[template] Allow passing in a template string using StringTemplate #591

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
StringTemplate map[string]string // templates to render which are not accessable files, instead 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 StringTemplate
for filename, tmplData := range t.StringTemplate {
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 StringTemplate",
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",
},
StringTemplate: 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
Loading