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

Additional functions in template #93

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

psemeniuk
Copy link

Hi. I created some functions that were helpful in creation custom images. For example we can pass multiple datasources by environment variables to tomcat xml configuration.

`

WEB-INF/web.xml ${catalina.base}/conf/web.xml
{{ range $value := envSlice "TOMCAT_CONTEXT_RESOURCE" }}
<Resource name="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_NAME }}"
          auth="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_AUTH "Container" }}"
          type="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_TYPE "javax.sql.DataSource" }}"
          maxTotal="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_MAXTOTAL "100" }}"
          maxIdle="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_MAXIDLE "30" }}"
          maxWaitMillis="{{ defaultEmpty $value.TOMCAT_CONTEXT_RESOURCE_MAXWAITMILLIS "10000" }}"
          username="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_USERNAME }}"
          password="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_PASSWORD }}"
          driverClassName="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_DRIVERCLASSNAME }}"
          url="{{ requiredEmpty $value.TOMCAT_CONTEXT_RESOURCE_URL }}"
/>
{{end}}

`

@chrismccracken
Copy link

I'd love to see more utility methods in the templating, this is a place where confd really shines by comparison.

@arunvelsriram
Copy link

These functions are very useful indeed. @jwilder please merge this. This addresses some pain points.

@powerman
Copy link
Contributor

Maintenance and development now happens in fork https://github.com/powerman/dockerize.

  • defaultEmpty $var $default - Returns a default value for one that does not exist or is empty in case of string. {{ default .Env.VERSION "0.1.2" }}

default already works this way, except order of params:

$ unset UNSET; EMPTY="" SPACE=" " dockerize -template /dev/stdin <<'EOF'
[{{ default "unset" .Env.UNSET }}]
[{{ default "empty" .Env.EMPTY }}]
[{{ default "space" .Env.SPACE }}] 
EOF                               
[unset]
[empty]
[ ]
  • required $val Returns a value passed as argument or error if value does not exist. {{ required .Env.VERSION }}
  • requiredEmpty $val Returns a value passed as argument or error if value does not exist or is empty in case of string. {{ requiredEmpty .Env.VERSION }}

These can be added, but to me it looks like right way to get error because of non-existing environment variable is to enable Option("missingkey=error") - this way it'll work automatically for all environment variables used in template, and you don't have to do anything manually. I plan to add new flag -template-strict which will enable that option.

Also current version provides empty and fail functions which you may find useful too.

Feel free to open PR at https://github.com/powerman/dockerize if you need something else and proposed flag and empty function doesn't do what you need (but please first review existing functions provided by Sprig).

  • envSlice - Returns the array filled with maps of environment variables passed as collection (variables that contains numeric suffix) and matched to $prefix {{ envSlice "VARIABLE_NAME" }}

This one is unique enough, but there are other functions which may do similar things:

  • splitList, split and regexSplit let you get multiple values from single env var if you can use some unique separator between values.
  • jsonQuery is much more powerful if you can store JSON inside env variable.

If proposed alternatives won't work for you then we can add envSlice at https://github.com/powerman/dockerize.

@mabrarov
Copy link

mabrarov commented Nov 11, 2020

Sorry for off-topic, but I have a concern against extending template functions. Ideally we need support of custom template functions loaded from user-provided files like j2cli supports (refer to --filters command line parameter).

Almost every time when one fills templates, he needs to think about quoting (to support whitespaces and to prevent configuration injection) and escaping (because of configuration file format and because of quoting). This sort of transformation can be complex. Here is an example of escaping for JBoss EAP / Wildlfy configuration - mabrarov/dockerfile-test/hollow-image/src/main/resources/rootfs/app/bin/filters.py.

So my personal preference would be to implement feature like j2cli provides - defining and loading external user-defined functions which could be used in dockerize template engine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants