Search code examples
kuberneteskubernetes-helm

Helm variables used at custom places


I am not able to reference variable inside a nested variable in Helm. Also, I am not able to do this as nested reference. I want to retrieve all client variables under client name using the value of the client_name variable. How can I do that?

Values.yaml

clients:
  client1:
    incomigBucket: databucket
    outgoingBucket: tempbucket
    webURL: http://example.com
  client2: 
     incomingBucket: databucket
    outgoingBucket: tempbucket
    webURL: http://example.com

I want to store client variables values in one variable and want to use it at different places in my Json file. if I use range function then it create section twice(as I have mentioned 2 clients), is there any thing in Helm I can use which can store these variables dynamically and use it in custom places in json file?

Sample File Section:

 "FileConfig": {
         "Client1": {
           "incomingLocationPath": "s3://{{ .Values.clients.client1.incomingBucket }}/dir1/dir2",
           "outgoingLocationPath": "s3://{{ .Values.clients.client1.outgoingBucket }}/dir1/dir2",
         },
         "Client2": {
           "incomingLocationPath": "s3://{{ .Values.clients.client2.incomingBucket }}/dir1/dir2",
           "outgoingLocationPath": "s3://{{ .Values.clients.client2.outgoingBucket }}/dir1/dir2",
         }
      }

Solution

  • I am not entirely sure where you intend to use this snippet. I assume it is inside a configMap. At least, I am not aware of any resource that had FileConfig section. I wouldn't know why it should be JSON either.

    The basic pattern could look like this.

    fileConfig:
      {{- range $client, $config := .Values.clients }}
      {{ $client }}:
        incomingLocationPath: "s3://{{ $config.incomingBucket }}/dir1/dir2"
        outgoingLocationPath: "s3://{{ $config.outgoingBucket }}/dir1/dir2"
      {{- end }}
    

    In something like a configMap to create JSON it could look like this.

    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: file-config-json
    data:
      config.json: |
        "fileConfig": {
          {{- range $client, $config := .Values.clients }}
          "{{ $client }}": {
            "incomingLocationPath": "s3://{{ $config.incomingBucket }}/dir1/dir2",
            "outgoingLocationPath": "s3://{{ $config.outgoingBucket }}/dir1/dir2"
          }
          {{- end }}
        }