-
Notifications
You must be signed in to change notification settings - Fork 563
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
feat: Add ReadDir as a templating function #1934
Conversation
This function reads the contents of a specified directory and creates a list of all contained files
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.
LGTM. Thanks for your contribution!
Looks like this is appending the path incorrectly to be used with readFile as the example shows. Based on the code below I'd expect a configmap that has each filename and the contents of those files but instead I get a duplicated base path appended to the file path.
readFile checks for an absolute path and if it doesn't find it appends the basePath which is what you are seeing here. Suggest modifying
to work better with readFile Hacky work around as this is really great functionality overall:
|
@bhester01 Thanks for reporting! Ah, so this is definitely broken. It looks like we had to make this aware of the base path as we do for @nlueb Would you mind contributing a fix for that? You'd make |
Adds a new templating function called `readDir`. With `readDir` users can read the contents of a specified directory and get a list of all contained files. This is useful when reading a bunch of files from a directory. The following example shows a snippet of a values file for configuring a Logstash deployment. Using only `readFile`, a user must specify each file by hand and adjust this list as the number of files to be read grows. ```yaml logstash: configs: logstash.yml: | {{- tpl (readFile "config/logstash.yml.gotmpl") . | nindent 6 }} jvm.options: | {{- readFile "config/jvm.options" | nindent 6 }} pipelines.yml: | {{- readFile "config/pipelines.yml" | nindent 6 }} pipelines: beats-log4j.conf: | {{- readFile "config/pipelines/beats-log4j.conf" | nindent 6 }} nginx-access.conf: | {{- readFile "config/pipelines/nginx-access.conf" | nindent 6 }} nginx-error.conf: | {{- readFile "config/pipelines/nginx-error.conf" | nindent 6 }} syslog-logs.conf: | {{- readFile "config/pipelines/syslog-logs.conf" | nindent 6 }} tcp-logs.conf: | {{- readFile "config/pipelines/tcp-logs.conf" | nindent 6 }} udp-debug.conf: | {{- readFile "config/pipelines/udp-debug.conf" | nindent 6 }} udp-logs.conf: | {{- readFile "config/pipelines/udp-logs.conf" | nindent 6 }} certificates: ca.crt: | {{- readFile "config/certificates/ca.crt" | nindent 6 }} logstash.crt: | {{- readFile "config/certificates/logstash.crt" | nindent 6 }} logstash.key: | {{- readFile "config/certificates/logstash.key" | nindent 6 }} ``` With `readDir` the above snippet can be rewritten as follows: ```yaml logstash: configs: {{- range readDir "config" }} {{ base . }}: | {{- if hasSuffix "gotmpl" . }} {{- tpl (readFile .) $ | nindent 6 }} {{- else }} {{- readFile . | nindent 6 }} {{- end }} {{- end }} pipelines: {{- range readDir "config/pipelines" }} {{ base . }}: | {{- readFile . | nindent 6 }} {{- end }} certificates: {{- range readDir "config/certificates" }} {{ base . }}: | {{- readFile . | nindent 6 }} {{- end }} ```
This PR adds a new templating function called
readDir
. WithreadDir
users can read the contents of a specified directory and get a list of all contained files. This is useful when reading a bunch of files from a directory. The following example shows a snippet of a values file for configuring a Logstash deployment. Using onlyreadFile
, a user must specify each file by hand and adjust this list as the number of files to be read grows.With
readDir
the above snippet can be rewritten as follows:Disclaimers