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 helper to select a single service in template #12575

Closed
mikenomitch opened this issue Apr 14, 2022 · 1 comment
Closed

Add helper to select a single service in template #12575

mikenomitch opened this issue Apr 14, 2022 · 1 comment

Comments

@mikenomitch
Copy link
Contributor

mikenomitch commented Apr 14, 2022

Proposal

Currently with Nomad Service Discovery, it is possible to list all of the Nomad services using the nomadService keyword. This is nice for something like a load balancer config.

It would be nice if you could select a single service from the list of Nomad Services. Ideally this could be semi-randomly selected for each allocation and not result in tons of restarts when Nomad service instances are added (or removed) from the list.

After speaking with the Consul team, they would like this on Consul services as well.

I think ideally we could name it hrwSelect or something like that (highest random weight), and also have another function that will select N services, not just one. In addition to hrwSelect an alias called something more generic/human friendly like pickOne or chooseOne might be nice.

Use-cases

This would allow writing an address/port to a config file for simple "load-balanced" service discovery without a load balancer.

Attempted Solutions

This WIP PR in Consul Template implements a simple chooseOne function that could achieve this (though there's a bug in real use).

Other Thoughts

Sometimes when dealing with lists in Template, you have to do a guard clause that checks if there are any items in the list before you then use that list. In my initial POC of this feature, I had to do something like this:

{{ $allocId := env "NOMAD_ALLOC_ID"}}
{{ $serviceList := nomadService "api" }}
{{ with $serviceList }}
{{ with chooseOne $allocId . }}
CURL_ADDR="http://{{.Address}}:{{.Port}}"
{{ end }}
{{ end }}

Which isn't a great UX. If we have to make a special function that has an empty-guard in it, I think that is better. I.E.

{{ $allocId := env "NOMAD_ALLOC_ID"}}
{{ $service := hrwNomadService "api" $allocId }}
{{ if $service }}
CURL_ADDR="http://{{$service.Address}}:{{$service.Port}}"
{{ end }}
{{ end }}
@github-actions
Copy link

github-actions bot commented Apr 9, 2023

I'm going to lock this issue because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant