roboll / helmfile

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

[Question] How to reference Chart's Values #1260

Open l-rossetti opened 4 years ago

l-rossetti commented 4 years ago

I have the following values/myapp.dev.values.yaml :

image:
    pullPolicy: Always
    repository: <here I would like to use registryFqdn coming from environment>/path/myapp
    tag: 2.7.1-test

And helmfile.yaml:

releases:
    - name: myapp
      namespace: myapp
      installed: true
      atomic: true
      labels:
          app: myapp
      chart: myrepo/myapp
      version: 1.1.0
      missingFileHandler: Error
      values:
          - values/myapp.default.values.yaml
          - values/myapp.{{ .Environment.Name }}.values.yaml
      hooks:
          - events: [ "presync" ]  
            showlogs: true
            command: "skopeo"
            args: [ 
                    "copy", 
                    "--src-creds" , "{{`{{ requiredEnv \"SRC_USER\" }}`}}:{{`{{ requiredEnv \"SRC_PASSWORD\" }}`}}", 
                    "--dest-creds" , "{{`{{ requiredEnv \"DEST_REGISTRY_USER\" }}`}}:{{`{{ requiredEnv \"DEST_REGISTRY_PASSWORD\" }}`}}", 
                    "docker://docker-repo.example.net/path/myapp:{{`{{ .Values.image.tag }}`}}",
                    "docker://{{`{{ .Environment.Values.registryFqdn}}`}}/path/myapp:{{`{{ .Values.image.tag }}`}}"
                ]   

I face two issues. First In the hook, which is copying docker images from a registry to another one, I would like to make use of the image.tag value coming from myapp.dev.values.yaml, i tried:

Second How can I make the repository value in values/myapp.dev.values.yaml parametric with respect to values defined in dev environment ? Or there's maybe a better way by making use of what is defined in repositories?

mumoshu commented 4 years ago

Sorry, but you can't refer to chart values from helmfile template. Helmfile's {{.Values}}` is for helmfile state values, which is a completely different concept than chart values.

l-rossetti commented 4 years ago

and what about .Release.Values? Or what's the suggested approach for sharing a value between multiple files?

mumoshu commented 4 years ago

What's .Release.Values?

Anyway, you'd just add the same values.yaml under values of every release. Or just generate release/chart values from helmfile state values:

values:
- foo:
    bar: 1

releases:
- name: yourapp
   values:
   - {{ .Values | toYaml | nindent 4 }}

or

values:
- foo:
    bar: 1

releases:
- name: yourapp
   values:
   foobar: "{{ .Values.foo.bar }}"
l-rossetti commented 4 years ago

Thank you for answering. About .Release.Values I found it in issue https://github.com/roboll/helmfile/issues/978 and I blindly tried it.

About your proposal I'm not sure it can be applied to my context. Let me explain.

I have a docker image version 2.7.1-test and I would like this version to be re-used in multiple places avoiding misalignment :

Something like:

releases:
    - name: myapp
      chart: myrepo/myapp
      version: 1.1.0
      values:
          - values/myapp.default.values.yaml
          - image:
              tag: 2.7.1-test                         <----------------- here
      hooks:
          - events: [ "presync" ]  
            showlogs: true
            command: "skopeo"
            args: [ 
                    "copy", 
                    "docker://src.com/path/myapp:{{`{{ .Values.image.tag }}`}}",   <--- here
                    "docker://dest.com/path/myapp:{{`{{ .Values.image.tag }}`}}"  <--- here
                ] 
mumoshu commented 4 years ago

@l-rossetti Thanks. What you're trying to do is actually impossible. You'd need to lift it to use state values. I'd suggest something like the below:

values:
- myapp:
    image:
      tag: 2.7.1-test

releases:
    - name: myapp
      chart: myrepo/myapp
      version: 1.1.0
      values:
          - values/myapp.default.values.yaml
          - image:
              tag: {{ .Values.myapp.image.tag }}                         <----------------- here
      hooks:
          - events: [ "presync" ]  
            showlogs: true
            command: "skopeo"
            args: [ 
                    "copy", 
                    "docker://src.com/path/myapp:{{`{{ .Values.myapp.image.tag }}`}}",   <--- here
                    "docker://dest.com/path/myapp:{{`{{ .Values.myapp.image.tag }}`}}"  <--- here
                ] 
l-rossetti commented 4 years ago

@mumoshu I've tried your idea, but I got 2 issues:

  1. the utilization of .Values in hook causes: in ./helmfile.yaml: failed processing release myapp: hook[scripts/copy-docker-image.sh]: template: stringTemplate:1:10: executing "stringTemplate" at <.Values.myapp.image.tag>: map has no entry for key "Values"

  2. the utilization in release values cause: in ./helmfile.yaml: failed to read helmfile.yaml: reading document at index 1: yaml: invalid map key: map[interface {}]interface {}{".Values.myapp.image.tag":interface {}(nil)}

tried with helmfile 0.116.0 and 0.109.0 + helm 2.16.1

mumoshu commented 4 years ago

@l-rossetti Gotcha! What if you tried this? Please note that there's --- which ensures that the values are loaded and parsed before rendering the latter part of your helmfile.yaml

values:
- myapp:
    image:
      tag: 2.7.1-test

---

releases:
    - name: myapp
      chart: myrepo/myapp
      version: 1.1.0
      values:
          - values/myapp.default.values.yaml
          - image:
              tag: {{ .Values.myapp.image.tag }}                         <----------------- here
      hooks:
          - events: [ "presync" ]  
            showlogs: true
            command: "skopeo"
            args: [ 
                    "copy", 
                    "docker://src.com/path/myapp:{{`{{ .Values.myapp.image.tag }}`}}",   <--- here
                    "docker://dest.com/path/myapp:{{`{{ .Values.myapp.image.tag }}`}}"  <--- here
                ] 
l-rossetti commented 4 years ago

@mumoshu this works like a charm! Thanks for your prompt help! (and sorry for the little delay)

I also want to highlight that values in hook args can be referenced simply by {{ .Values.myapp.image.tag }} without using the {{` backtick notation.