Unable to interpolate environment variable inside POD via the helm chart for stable/elasticsearch-curator

11/27/2019

I am trying to make an helm chart for my custom elk stack. I use the stable/elasticsearch-curator chart as a dependency.

In my values.yaml file, I use some env variables to pass the elasticsearch host:

esClusterName: &esClusterName "elasticsearch-logs"

...

elasticsearch-curator:

  env:
    ES_CLUSTER_NAME: *esClusterName
    ELASTICSEARCH_HOST: $(ES_CLUSTER_NAME)-master

  configMaps:

    config_yml: |-
      ---
      client:
        hosts:
          - ${ELASTICSEARCH_HOST}
        port: 9200

But the variable is not correctly interpolated as shown by this error message:

HTTP N/A error: HTTPConnectionPool(host='$(es_cluster_name)-master', port=9200): Max retries exceeded with ...

Inside my pod, ELASTICSEARCH_HOST = '$(es_cluster_name)-master' --a string of the name of my variable in LOWERCASE and "-master"-- instead of "elasticsearch-logs-master".

I cannot wrap my head arround this. I have used the same technique -- env variable interpolation -- for the other dependencies and it works.

The only difference I see is that the helm chart for elasticsearch-curator passes env variables differently from the other charts:

# stable/elasticsearch-curator/templates/cronjob.yaml (The file is here)

            env:
{{- if .Values.env }}
{{- range $key,$value := .Values.env }}
              - name: {{ $key | upper | quote}}
                value: {{ $value | quote}}
{{- end }}
{{- end }}

And this template expects the values to be passed in values.yaml like so: ( the file is here)

env:
  MY_ENV_VAR: value1
  MY_OTHER_VAR: value2

whereas all other templates use this way: ( exemple file)

        env: {{ toYaml .Values.extraEnvs | nindent 10 }}

with a values.yaml like so: ( exemple file)

extraEnvs: 
  - name: MY_ENVIRONMENT_VAR
    value: the_value_goes_here

But I'm not sure if this difference explains my problem. So my question is: how do I make it work?

-- OLIVIER
elasticsearch-curator
environment-variables
kubernetes-helm

1 Answer

11/27/2019

I replaced ELASTICSEARCH_HOST with ES_HOST like so:

elasticsearch-curator:

  env:
    ES_CLUSTER_NAME: *esClusterName
    ES_HOST: $(ES_CLUSTER_NAME)-master

  configMaps:

    config_yml: |-
      ---
      client:
        hosts:
          - ${ES_HOST}
        port: 9200

and it just worked!

I think it comes from the fact that when values.yaml is parsed, the keys from the env: object are sorted in alphabetical order:

env: {
  ELASTICSEARCH_HOST: $(ES_CLUSTER_NAME)-master
  ES_CLUSTER_NAME: "elasticsearch-logs"
}

Then, when the pod tries to interpolate the value of ES_CLUSTER_NAME inside ELASTICSEARCH_HOST, it does not work since it doesn't know the value of ES_CLUSTER_NAME yet.

It would be nice to have confirmation (or infirmation) of this.

-- OLIVIER
Source: StackOverflow