roboll / helmfile

Deploy Kubernetes Helm Charts
MIT License
4.05k stars 565 forks source link

templating of labels #446

Open sstarcher opened 5 years ago

sstarcher commented 5 years ago

With the addition of the new templating support being able to use release variables in a label is now more useful.

templates:
  default: &default
    missingFileHandler: Warn
    chart: test
    namespace: '{{`{{ .Release.Name }}`}}'
    labels:
      cluster: '{{`{{ .Release.Name }}`}}'
      environment: prod

Currently labels.cluster will not be the expected value.

pdemagny commented 5 years ago

Being new to helmfile i innocently tried this first, before realizing that it is was not possible. It would be nice if we could use templating in labels !

With v0.43.1, running: $ helmfile -f helmfile.yaml --selector release=redis sync

Currently, with:

templates:
  defaultStable: &defaultStable
    labels:
      release: '{{`{{ .Release.Name }}`}}'

releases:
  - name: redis
    namespace: primary
    version: 5.3.0
    <<: *defaultStable

I'm getting:

...
second-pass rendering result of "helmfile.yaml":
...
44:   - name: redis
45:     namespace: primary
46:     version: 5.3.0
47:     <<: *defaultStable
...
0 release(s) matching release=redis found in helmfile.yaml
...
err: no releases found that matches specified selector(release=redis) and environment(default), in any helmfile
james-mchugh commented 3 years ago

It would be awesome to see this implemented. The Release Template feature is a great addition, but it seems like it is missing a lot of functionality that a user would expect.

mumoshu commented 3 years ago

@james-mchugh Hey. I'm just not sure what a user would expect. You should always write specific github issue(s) for whatever you think missing, so that each issue can be addressed by anyone :)

Also, would you mind submitting a PR for this feature? I don't have my own use-case for this feature so this is relatively low in my loooong list of TODOs across many projects I maintain in my spare time.

But I can definitely imagine that this would be helpful for many people. I'm more than happy to review PRs related to this.

james-mchugh commented 3 years ago

@mumoshu I was considering attempting to submit a PR for this, as I think it would be super useful for my team's use case. Some of the specific features I was alluding to would be the ability to do templating in all of the fields under a release template instead of just a select few. For example, I think being able to use templating in the labels and condition fields would be super useful. Of course, I am not familiar with the code under the hood here, so there could be a major lift involved in enabling templating in these fields that is not present in the other fields.

From my perspective as a user, when I see the a term like release templates, I immediately think that I can use templating in all of the available fields. Just like @pdemagny, I naively attempted to use release templates to do templating in labels, but was dismayed to see that it was not possible. I don't think it's too unreasonable for a user to expect something like that to work.

I'm not super familiar with Go. I'll have to dive into the codebase a bit and try to understand what's going on under the hood here. With any luck, I'll be able to submit a PR in the coming days. I've been wanting to contribute back to some of the projects that have helped me for a while now, so this would be a good opportunity.

mumoshu commented 3 years ago

@james-mchugh Thanks!

BTW, if your use-case is exactly the same as @pdemagny's, can you just use the default release labels name and chart, whose values are automatically set to the release's respective fields?

So in other words, you can just use helmfile -l name=yourrelease instead of defining your own name equivalent using:

release: {{`{{ .Release.Name }}`}}

and helmfile -l chart=yourchart is available without writing:

chart: {{`{{ .Release.Chart }}`}}
Sajfer commented 2 years ago

From what I can see from some quick tests it works fine to use labels with template values.

repositories:
- name: bitnami
  url: https://charts.bitnami.com/bitnami

templates:
  defaultStable: &defaultStable
    labels:
      release: '{{`{{ .Release.Name }}`}}'

releases:
  - name: concourse
    chart: bitnami/concourse
    namespace: primary
    <<: *defaultStable

  - name: redis
    chart: bitnami/redis
    namespace: primary
    <<: *defaultStable

helmfile --debug -f helmfile.yaml --selector release=redis diff and helmfile --debug -f helmfile.yaml --selector release=concourse diff will select the correct release.