I want to use the jsonQuery
syntax in dockerize to parse traefik's acme.json and emit cert/key files for TLS settings in another service.
jsonQuery accepts a string, which the example gives as an environment variable {{ .Env.myJson }}
How might I get the string contents of a file:
{{with $myJsonContent := <insert magic here> }}
# extract key to file
{{end}}
I can't see any way to add a function to the template, as dockerize doesn't expose the addition of functions to the template prior to parsing. So you'll either have to (1) get the contents of acme.json into an environment variable, or (2) modify dockerize to include a jsonFileQuery function in the templates.
Add the contents of acme.json to the environment variables before running dockerize - then access as in the example. This could be done with a small go program, added to the container and run via CMD prior to the CMD dockerize
Fork dockerize and change jsonQuery: Fork dockerize and change line 83 of template.go:
from: parser, err := gojq.NewStringQuery(jsonObj)
to: parser, err := gojq.NewFileQuery(jsonObj)
Then use:
{{with $myJsonContent := jsonQuery "/opt/traefik/acme.json" "toplevelobject" }}
# extract key to file
{{end}}
gojq.NewStringQuery() is a function behind the jsonQuery template function. The gojq.NewFileQuery() version has the same signature as the StringQuery but reads the file at the path in the input string instead of using the input string as json.
OR
Merge new jsonFileQuery template function into dockerize: submit an issue to dockerize to add jsonFileQuery to the template functions. Seems like it could be set up the same as jsonQuery but with the small difference above. In template.go, add the jsonFileQuery function and assign it to jsonFileQuery in the template.FuncMap{} within generateFile().
Go's text/template
doesn't natively support that. It looks like the dockerize tool provides a couple of extension functions but none of them allow this either.
(The nearest thing I can think of is that kubernetes-helm supports reading a file from a Helm chart, but that's implemented at the Go level by injecting a special accessor object that can provides the file-access API to template code, and it's intentionally limited to files physically located within the Helm chart directory.)