habitat-sh/habitat

RFE: In the template syntax, I want to know what topology a given service is in

irvingpop opened this issue · 4 comments

Currently, you have at the time of template writing whether or not you're binding to a service that is in a topology (e.g. leader) or not. when writing your template, you either choose something like this for finding the leader:

{{~ #if bind.database}}
  {{~ #eachAlive bind.database.members as |member|}}
    {{~ #if member.leader}}
PGHOST="{{member.sys.ip}}"
    {{~ /if}}
  {{~ /eachAlive}}
{{~ /if}}

but if the service isn't in a topology, member.leader will always be false and instead users are instructed to rely on the @first or @last helpers. There's no way to detect if a given service is in a topology or not, so you have to know this in advance and that makes reuse difficult.

Ideally, I would want to do something like this in my template:

{{~#if bind.database}}
  {{~#if bind.database.topology?}}
     {{~#findLeader bind.database.members as |member|}}
       PGHOST="{{member.sys.ip}}"
     {{~/findLeader}}
  {{else}}
     # loop through via #eachAlive
  {{~/if}}
{{~/if}}

or an even more terse way would be something like:

  {{~#if bind.database.topology?}}
     PGHOST={{bind.database.leader.sys.ip}}
  {{else}}
  {{~/if}}

pie in the sky, but a "just do the right thing" pointer that would choose the leader the in a leader topology or any alive member at random in a non-leader topology would be the nicest from a user perspective

@eeyun looks like this may help, perhaps add context from @robbkidd's suggestion in triage #4113

@irvingpop I've been in the templating code a fair bit lately, and have a lot of ideas for how we can improve things... some of those ideas are basically the last example you list here 😄

I've currently got #4823 in review, which lays the foundation for easily adding these kind of improvements in the future.

awesome!! and looks like that merged, so I'll close this out. Thanks @christophermaier !!!

Sorry @irvingpop... that PR just lays the foundation for this work, so I'm going to reopen this issue.

That being said, the PR that does address this is going up in a minute 😄