Search code examples
kubernetes-helmhelmfile

How to get values in helmfile


bases:
  - common.yaml


releases:
  - name: controller
    values:
      - values/controller-values.yaml
    hooks:
    - events: [ "presync" ]
    ....
    - events: [ "postsync" ]
    .....

common.yaml

environments:
  default:
    values:
      - values/common-values.yaml

common-values

a:b

I want to move the values of the hooks to file when I added it to common.values it worked but I want to add it to different files and not to the common, so I tried to add base

bases:
  - common.yaml
  - hooks.yaml

releases:
  - name: controller
    values:
      - values/controller-values.yaml
    hooks:
{{ toYaml .Values.hooks | indent 6 }}

hooks.yaml

environments:
  default:
    values:
      - values/hooks-values.yaml

hooks-values.yaml

hooks:
  - events: [ "presync" ]
    ....
  - events: [ "postsync" ]
    .....
    

but I got an error parsing: template: stringTemplate:21:21: executing "stringTemplate" at <.Values.hooks>: map has no entry for key "hooks"

I tried also to change it o

hooks:
  - values/hooks-values.yaml

and I got an error line 22: cannot unmarshal !!str values/... into event.Hook


Solution

  • I think the first issue is when specifying both common.yaml and hooks.yaml under bases:, they are not merged properly. Since they provide same keys, most probably the one that is included later under bases: overrides the other.

    To solve that you can use a single entry in bases in helmfile:

    bases:
      - common.yaml
    

    and then add your value files to common.yaml:

    environments:
      default:
        values:
          - values/common-values.yaml
          - values/hooks-values.yaml
    

    I don't claim this is best practice, but it should work :)

    The second issue is that bases is treated specially, i.e. helmfile.yaml is rendered before base layering is processed, therefore your values (coming from bases) are not available at a point where you can reference them directly in the helmfile. If you embedded environments directly in the helmfile, it would be fine. But if you want to keep using bases, there seems to be couple of workarounds, and the simplest seemed to be adding --- after bases as explained in the next comment on the same thread.

    So, a working version of your helmfile could be:

    bases:
      - common.yaml
    
    ---
    
    releases:
      - name: controller
        chart: stable/nginx
        version: 1.24.1
        values:
          - values/controller-values.yaml
        hooks:
          {{ toYaml .Values.hooks | nindent 6 }}
    

    PS: chart: stable/nginx is just chosen randomly to be able to helmfile build.