Skip to content

Make values available as .Values in termplates and helmfile.yaml #387

Open

Description

@nikolajbrinch

It would really help if values from different places were available as .Values (like in standard Helm), to be referred and used.
Not alle values are Environment values, example:

We have set up Open ID Connect using Dex and loginapp, they need to know about eachothers endpoints, being dex./dex and login./login.
The information is not globally Environment specific, but only local to this "deployment unit". The domain is though Environment specific, so this would be specified in the enviroment values.yaml
It would be nice if it was possible to just define:

environment.yaml

environments:
  dev:
    values:
    - dev-values.yaml
  test:
    values:
    - test-values.yaml

dev-values.yaml

domain: dev.example.org

test-values.yaml

domain: test.examle.org

values.yaml

dex:
  subDomain: dex
  path:/dex
login
  subDomain: login
  path: /login

On a higher level in a values file, and then in the deployment units (dex and loginapp) values (file og directly) reference these values like this:

dexUrl: {{ .Values.dex.subDomain }}.{{ .Environment.Values.domain }}{{ .Values.dex.path }}
loginUrl: {{ .Values.login.subDomain }}.{{ .Environment.Values.domain }}{{ .Values.login.path }}

For dev environment this would render:

dexUrl: dex.dev.example.org/dex
loginUrl: login.dev.example.org/login

dexUrl and loginUrl would then also be available as

{{ .Values.dexUrl }}
{{ .Values.loginUrl }}

Hope this makes sense?

Activity

  1. Tarick commented on Apr 11, 2019

    @Tarick

    I second this.
    My use-case is similar - I have multiple app roles from the same monolith repo using the same config template, where vars can be different depending on the environment AND app role.
    The only way to support this currently is to put this config template into each helm chart (not DRY) and this is hard to support further once that config is changed.
    It would be great to have all previously merged values available in subsequent gotmpl values files.

  2. mumoshu commented on Apr 11, 2019

    @mumoshu
    Collaborator

    @nikolajbrinch @Tarick What a coincidence! I just discussed about similar things with my colleague today.

    @nikolajbrinch In your example, where in helmfile.yaml do you want to define the values.yaml, which seems not like releases[].values but anywhere else?

    @Tarick Makes sense, but it you can still use environment values and a regular values file template, like this:

    environments:
      prod1:
        values:
         - prod.yaml
    
    releases:
    - name: role1
       values:
        - role1.values.yaml.gotmpl
    

    And in prod.yaml you have:

    values:
      appRole1:
        externalDnsName: role1.example.com
      appRole2:
        externalDnsName: role2.example.com
    

    role1.values.yaml.gotmpl:

    {{ .Environment.Values.values.appRole1 | toYAML }}
    

    It would be great to have all previously merged values available in subsequent gotmpl values files.

    So you want all the values.yaml files listed under releases[].values to be merged one by one, and the latest merged values to be available in the next values.yaml template, right?

    values:
    - foo.yaml.gotmpl
    - bar.yaml.gotmpl
    

    foo.yaml.gotmpl:

    foo: bar
    

    bar.yaml.gotmpl:

    bar: {{ .Values.foo }}
    

    Does the above result in the bar set to foo, and there's also foo: bar because it is merged into the second?

    foo: bar
    bar: bar
    
  3. Tarick commented on Apr 11, 2019

    @Tarick

    @mumoshu

    Makes sense, but it you can still use environment values and a regular values file template...

    I can't due to the need of config file content to be rendered in the end, it doesn't know anything about role name.
    e.g.
    config-file.yaml.gotmpl:

    application_config_content: |
       external_dns = {{ .Values.externalDnsName }}
    
    

    If I try to use .Environment.Values.values.appRole1 instead of .Values.externalDnsName this config-file.yaml.gotmpl will be valid only for that role and that breaks DRY.

    Does the above result in the bar set to foo, and there's also foo: bar because it is merged into the second?

    foo: bar
    bar: bar
    

    Yes, I would consider this as correct result.
    I may be too spoiled by Ansible, where they merge all values in order and they are available for next file templated vars. If I remember correctly even if there is no value for a tempated var at the moment of merging, Ansible does the second pass on all files with already merged set.

  4. sgandon commented on Apr 11, 2019

    @sgandon
    Contributor

    Actually at first I thought that the Default environment could be used for that but it turns out that the Default environment is not loaded if some other environment is specified in the command line.

    I would have expected that the default one would always be loaded and the specific environment could override it. But this is not the case.
    So may be we could have some global environment that is always loaded before all the other environments and that can also be referenced with Environment.Values from the user created environments (and the default one) and from the helmfile itseft and the releases values files.

    I hope I am clear. here is an example

    environments:
      global:                        <- always loaded before any chosen env
        values:
          - global.yaml          <- this cannot be templatized (only real values)  
      default:                       <- loaded as today and can use .Environment.Values from the global ones
         ...                                   and override global ones
      anyenv:                       <- same as default but when the cli user selects (like today)
    

    pros: augment an existing mechanism, values are still accessed using .Environment. Also environment values can override global ones.
    cons: add 2 new semantics, the global without templates and the overriding mechanism.

    Of course global can be change to something else (initial) or even move upward a level and can be even renamed values like this :

    values:                      <- always loaded before any chosen env
      - global.yaml          <- this cannot be templatized (only real values)  
    environments:
      default:                       <- loaded as today and can use .Values from the global ones
         ...                                   
      anyenv:                       <- same as default and when the cli user selects (like today)
    

    pros: decouple the 2 semantics
    cons: you need 2 mechanisms for getting access to values, Environment.Values and .Values. And you cannot override the global ones.

    I would be in favor of the first solution and hope I understood the initial concern.

  5. janekbaraniewski commented on Jul 2, 2019

    @janekbaraniewski

    Hey! I also could use some way of accessing values from values. My use-case is pretty similar to @Tarick s one - I have a huge monolith project which actually is multiple applications all using same image, all using same configuration, only difference being the run command. I wanted to create a setup with generic release definition and have a list of enabled applications/components for each environment (f.e. on production I want all components deployed but on testenv I might want only few of them, or on my dev cluster I want only component I'm developing, etc.).
    So in my environment.yaml file I would have something like this :

    componentsEnabled:
      - app1
      - app2
    

    and then in my helmfile I wanted to use range to generate release for each of those components:

    
    releases:
    {{- range .Environment.Values.componentsEnabled }}
      - name: {{ . }}
        namespace: my-namespace
        chart: my-chart
        version: 0.0.1
        values:
          - values/values-file.yaml.gotmpl
    {{- end }}
    
    

    But then I need some way of accessing Release.Name inside my values. I was thinking about just setting a value in my helmfile like that:

    ...
    values:
      - releaseName: {{ . }}
    

    But then again - I can't access it from within my values file.

    I need the release name to be injected to run command inside my values file. Am I correct that this cannot be achieved ATM or have I missed something?

  6. mumoshu commented on Jul 3, 2019

    @mumoshu
    Collaborator

    Hey! You're correct on that this can't be achieved today.

    Perhaps what we wan't isn't inheriting previous values.yaml contents to next ones, but rather a dedicated way to compose values injected to values.yaml templates.

    What if helmfile added a syntax like this?

    releases:
    - name: {{ . }}`
      chart: my-chart
      valuesFrom:
      - inline:
           foo: FOO
      - file:
          path: values/values.yaml
      - templateFile:
          path: values/values-file.yaml.gotmpl
          valuesFrom:
          - inlineTemplate:
              releaseName: `{{.Release.Name}}` #<- you'll need {{` `}} to defer rendering until the evaluation time of inlineTemplate. otherwise this fails due to that .Release.Name isn't available at loading-time of your helmfile.yaml :)

    values/values.yaml:

    bar: BAR

    values/values-file.yaml.gotmpl:

    baz: {{.Values.releaseName}}

    For app1, this would result in rendering my-chart with the following values:

    foo: FOO
    bar: BAR
    baz: app1
    

    and For app2:

    foo: FOO
    bar: BAR
    baz: app2
    
  7. janekbaraniewski commented on Jul 22, 2019

    @janekbaraniewski

    Hey, sorry that I took so long to get back to you. That looks like a nice feature, definitely would help with figuring out how are different values overwritten and where they come from.

    I was however able to solve my case! I used helmfile layering for that.
    I have my "template" helmfile component.yaml

    releases:
      - name: {{ .Environment.Values.releaseName }}
        namespace: my-namespace
        chart: my-chart
        version: 1.7.2
        values:
          - values/values-file.yaml.gotmpl

    Then, I have a helmfile.yaml file :

    helmfiles:
      - path: component.yaml
        values:
          - releaseName: app1
      - path: component.yaml
        values:
          - releaseName: app2

    Since this value is available both in underlying helmfile and in values that releases from it use, this solves my case and allows us to have nice, DRY definition of our environment :)

  8. maxchen-000 commented on Apr 1, 2021

    @maxchen-000

    There is a workaround with using setValueAtPath, readFile, fromYaml, and get.
    For your example, you would create something like these to get what you want:

    environment.yaml

    environments:
      dev:
        values:
        - dev-values.yaml
      test:
        values:
        - test-values.yaml
    

    dev-values.yaml

    domain: dev.example.org
    values_level_variables: ""
    

    test-values.yaml

    domain: test.examle.org
    values_level_variables: ""
    

    values_level_variables.yaml

    dex:
      subDomain: dex
      path:/dex
    login
      subDomain: login
      path: /login
    

    values.yaml.gotmpl

    {{ setValueAtPath "values_level_variables" (readFile "values_level_variables.yaml" | fromYaml) .Values | get "_discard_output_" "" }}
    
    dexUrl: {{ .Values.values_level_variables.dex.subDomain }}.{{ .Environment.Values.domain }}{{ .Values.values_level_variables.dex.path }}
    loginUrl: {{ .Values.values_level_variables.login.subDomain }}.{{ .Environment.Values.domain }}{{ .Values.values_level_variables.login.path }}
    

    For dev environment this would render using the above values.yaml.gotmpl:

    dexUrl: dex.dev.example.org/dex
    loginUrl: login.dev.example.org/login
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions