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

Add sprig function support #1206

Closed
bitsofinfo opened this issue Apr 19, 2019 · 18 comments
Closed

Add sprig function support #1206

bitsofinfo opened this issue Apr 19, 2019 · 18 comments

Comments

@bitsofinfo
Copy link

bitsofinfo commented Apr 19, 2019

Please consider adding sprig go templating function support, its used in helm etc and would be a great additional of capabilities into consul-template

https://github.com/Masterminds/sprig

@bitsofinfo
Copy link
Author

please, pretty please

@boarder981
Copy link

This would be amazing. Sprig has saved me a lot of trouble with helm charts, and would be great to have in consul-template as well!

@CasparChou
Copy link

need this

@bitsofinfo
Copy link
Author

func funcMap(i *funcMapInput) template.FuncMap {

might be collisions, might have to use a prefix name in the mappings: http://masterminds.github.io/sprig/

@Chinikins
Copy link

I think several of us would appreciate this PR picked up: #1312

@eikenb
Copy link
Contributor

eikenb commented Mar 11, 2022

#1312 was merged!

@eikenb eikenb closed this as completed Mar 11, 2022
@jakubgs
Copy link

jakubgs commented Mar 23, 2022

That's very cool, but are there any plans to upgrade to newer version, like 3.2.1?

The newer versions add support for a lot of useful operations for dicts, like for example get in 3.0.0 and dig in 3.2.0.

@eikenb
Copy link
Contributor

eikenb commented Mar 23, 2022

The problem is compatibility. Sprig 3 breaks compatibility with Sprig 2 which is what was merged. We could do a backwards incompatible update, but I don't like to do that if possible. Adding a whole other sprig3* namespace is not really appealing either. I'm going to think about the best solution.

If you'd like to keep this more on the radar, maybe file a separate feature request issues. Maybe others will have an better idea on integrating sprig3 or at least that will be a place to track interest and priority. Thanks.

@jakubgs
Copy link

jakubgs commented Mar 23, 2022

Thanks for explaining. Didn't know the switch to 3.0.0 was such a big break from 2.0.0.

I'm starting to think I'm better off trying to use hcat instead to build something more custom.

(Edit: Or just hack something together in Python + Consul2 + Jinja2.)

@eikenb
Copy link
Contributor

eikenb commented Mar 23, 2022

Consul-template will be ported to use hcat as soon as I have the time. That was always the plan. After that I was considering a v2 template library that would allow more modularity, allow for versioning and give template writers more flexibility... but that is mere speculation at this point. Lots to do to get there.

I also maintain hcat and would be happy to help with any usage. Be warned though that there are a couple more API breaking changes in the plans (not huge, but will require updates) before we hit a stable, semantic versioned, 1.0.

@eikenb
Copy link
Contributor

eikenb commented Mar 23, 2022

Thanks for explaining. Didn't know the switch to 3.0.0 was such a big break from 2.0.0.

I don't think it is that big of a change, but they do list a few backwards incompatibilities (they list 1 breaking change for ^ as necessitating the major version bump) and I have no way to tell if those will hit users or not. So even for small compatibility issues, breaking something working is still possible and is what I'd like to avoid.

@jakubgs
Copy link

jakubgs commented Mar 23, 2022

Yeah, that's what I thought it was for, and I'm aware it's unstable, that's fine by me.

My use case is that I have Consul service which represent backends/upstreams for an Nginx Reverse proxy, for which the config is generated using consul-template. This works fine if the relation of service <-> domain is 1 to 1. If it's N to 1 - meaning multiple upstream servers per domain - the templating I have simply uses the last service for the given domain in the query result. What I'd like to do is to first bunch up services together based on domain, and then render separate configs for each proxy so it can have multiple upstreams.

I was hoping to use dicts and get/dig/set and list operations to do that, but that might be just more complex than it needs to be. It might be simpler just to write this logic directly in Go or Python and have much more flexibility for extending it in the future. I think I would definitely be interesting in learning how to use hcat.

@eikenb
Copy link
Contributor

eikenb commented Mar 23, 2022

There might be a way to do that with CT, I've seen some pretty crazy setups. You could ask on the discuss forum where others who are much better at the templates than I am (shout-out to @blake ) could see it and have a chance to respond. Just a thought.

@eikenb
Copy link
Contributor

eikenb commented Mar 23, 2022

Discuss forum I mentioned... https://discuss.hashicorp.com/tags/c/consul/29/consul-template

@jakubgs
Copy link

jakubgs commented Mar 23, 2022

Will do, thanks.

@blake
Copy link
Member

blake commented Mar 23, 2022

@jakubgs, I shared a template earlier today sounds like it does some of what you're looking for. Take a look and let me know if this meets your needs. https://discuss.hashicorp.com/t/consul-template-nginx-vhost/4425/2?u=blake

@jakubgs
Copy link

jakubgs commented Mar 23, 2022

Thanks @blake, interesting. I will have to read into it and do some tests, it does appear to use some elements of dark magic.

@jakubgs
Copy link

jakubgs commented Mar 25, 2022

With very minimal modifications I managed to get your example working for my own setup!.

Thanks a bunch @blake, really cool stuff. And now I can even filter by just tags, which is what I wanted from the start.

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

No branches or pull requests

7 participants