Add support for Go template to -tags flag #677
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
v7.4.0
About
I have added support for Go template to
-tags
flag and with this feature it is now possible to dynamically evaluate all the container inspect object fields and to add their values to Consul tags list, also prebuilt docker image is available on dockerhub.Static tags can be used as before (non-breaking change):
Or you can utilize Go template and dynamically evaluate all the fields from docker container inspect object:
Go Template functions
List of supported custom template functions (to check predefined global template functions click here):
strSlice
Slice string from start to end (same as
s[start:end]
wheres
represents string).Usage:
strSlice s start end
Example:
strSlice .ID 0 12
Output:
"e20f9c1a7656"
sIndex
Return element from slice or array
s
by specifiying indexi
(same ass[i]
wheres
represents slice or array - indexi
can also take negative values to extract elements in reverse order).Usage:
sIndex i s
Example:
sIndex 0 .Config.Env
Output:
"ENVIRONMENT=test"
mIndex
Return value for key
k
stored in the mapm
(same asm["k"]
).Usage:
mIndex k m
Example:
mIndex "com.amazonaws.ecs.task-arn" .Config.Labels
Output:
"arn:aws:ecs:region:xxxxxxxxxxxx:task/368f4403-0ee4-4f4c-b7a5-be50c57db5cf"
toUpper
Return string
s
with all letters mapped to their upper case.Usage:
toUpper s
Example:
toUpper "foo"
Output:
FOO
toLower
Return string
s
with all letters mapped to their lower case.Usage:
toLower s
Example:
toLower "FoO"
Output:
foo
replace
Replace all (-1) or first
n
occurrences ofold
withnew
found in the designated strings
.Usage:
replace n old new s
Example:
replace -1 "=" "" "=foo="
Output:
foo
join
Create a single string from all the elements found in the slice
s
wheresep
will be used as separator.Usage:
join sep s
Example:
join "," .Config.Env
Output:
"ENVIRONMENT=test,SERVICE_8105_NAME=foo,HOME=/home/foobar,SERVICE_9404_NAME=bar"
split
Split string
s
into all substrings separated bysep
and return a slice of the substrings between those separators.Usage:
split sep s
Example:
split "," "/proc/bus,/proc/fs,/proc/irq"
Output:
[/proc/bus /proc/fs /proc/irq]
splitIndex
split
andsIndex
function combined, indexi
can also take negative values to extract elements in reverse order.Same result can be achieved if using pipeline with both functions:
{{ split sep s | sIndex i }}
Usage:
splitIndex i sep s
Example:
splitIndex -1 "/" "arn:aws:ecs:region:xxxxxxxxxxxx:task/368f4403-0ee4-4f4c-b7a5-be50c57db5cf"
Output:
"368f4403-0ee4-4f4c-b7a5-be50c57db5cf"
matchFirstElement
Iterate through slice
s
and return first element that match regex expression.Usage:
matchFirstElement regex s
Example:
matchFirstElement "^SERVICE_" .Config.Env
Output:
"SERVICE_8105_NAME=foo"
matchAllElements
Iterate through slice
s
and return slice of all elements that match regex expression.Usage:
matchAllElements regex s
Example:
matchAllElements "^SERVICE_" .Config.Env
Output:
[SERVICE_8105_NAME=foo SERVICE_9404_NAME=bar]
httpGet
Fetch an object from URL.
Usage:
httpGet url
Example:
httpGet "https://ajpi.me/all"
Output:
[]byte
(e.g. JSON object)jsonParse
Extract value from JSON object by specifying exact path (nested objects). Keys in path has to be separated with double colon sign.
Usage:
jsonParse b key1::key2::key3::keyN
Example:
jsonParse b "Additional::Country"
Output:
"United States"
Examples
E1
Use custom (
strSlice
) or predefined global (slice
) template function to slice values as needed:For multiple tags you'd just have to add comma separated values.
Output:
E2
In this example we're going to use
mIndex
template function to extract value from the Docker label that has dots in key name, then saved value will be passed tosplitIndex
template function which will be used to split string by/
character and to return last element from the list.Inspect container object snippet:
Output:
E3
It is also possible to set all env vars as lower case tags.
Output:
Or just one specific environment variable that match regex expression (first match only).
Output:
Or list of all regex matches.
Output:
E4
Use
httpGet
function to fetch data from any external HTTP source (must return a JSON object).Output:
Or if running on AWS EC2 instance you can use this function and EC2 metadata API to fetch EC2
instanceId
andinstanceType
.Output:
Or you can use more advanced example that will allow you to use different sources and tags per container.
Now you can start one or more containers with special environment variables.
container 1
Output:
container 2
Output:
Usage
To start using this feature you have to start
registrator
with-tags
flag:docker run -it -d \ --name registrator \ --network host \ --restart always \ --volume /var/run/docker.sock:/tmp/docker.sock \ psyhomb/registrator:v7.4.0 -tags='container_id={{ strSlice .ID 0 12 }},container_ip={{ .NetworkSettings.IPAddress }}' consul://127.0.0.1:8500
Afterwards you can start any arbitrary container with
SERVICE_NAME
env variable and with one or multiple published ports:Check if container is started successfully:
docker ps -a -f "name=elated_bhabha"
Output:
Check registrator log:
Output:
And finally check what's registered on Consul:
Output:
This is especially useful if you are using registrator to register Prometheus targets, because you can use these tags as labels.