-
Notifications
You must be signed in to change notification settings - Fork 828
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
Add support for templating in configs #6251
base: main
Are you sure you want to change the base?
Changes from all commits
1e4e7a2
aefdebd
5293fc7
1efd952
2e95e8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,13 @@ | |
package config | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
stdlog "log" | ||
"os" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
"gopkg.in/validator.v2" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
@@ -90,14 +93,28 @@ func Load(env string, configDir string, zone string, config interface{}) error { | |
// TODO: remove log dependency. | ||
stdlog.Printf("Loading config files=%v\n", files) | ||
|
||
templateFuncs := sprig.FuncMap() | ||
|
||
for _, f := range files { | ||
// This is tagged nosec because the file names being read are for config files that are not user supplied | ||
// #nosec | ||
data, err := os.ReadFile(f) | ||
if err != nil { | ||
return err | ||
} | ||
err = yaml.Unmarshal(data, config) | ||
|
||
tpl, err := template.New("config").Funcs(templateFuncs).Parse(string(data)) | ||
if err != nil { | ||
return fmt.Errorf("template parsing error: %w", err) | ||
} | ||
|
||
var rendered bytes.Buffer | ||
err = tpl.Execute(&rendered, nil) | ||
if err != nil { | ||
return fmt.Errorf("template execution error: %w", err) | ||
} | ||
|
||
err = yaml.Unmarshal(rendered.Bytes(), config) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It also would be very useful for debugging purpose to check what is in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed to put this into a |
||
if err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this compatible with the existing
config_template.yaml
? it looks like sprig provides a function used likeenv "VAR"
but the template is written with things like.Env.VAR
. do we have to migrate the template?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The template would need adjusting. Sprig's
default
argument ordering is reversed from thedockerize
version. There are probably a few other gotchas. I would suggest leaving the existing template as it is and creating a new template underconfig/docker.yaml
with the updated syntax so that containers using dockerize will continue to work until we deprecate that. Newer containers can use this new functionality with the updated config file. I'll add a working config file to this PR.