Search code examples
kuberneteskubernetes-helm

Helm Exporting environments variables from env file located on volumeMounts


I have problem, I am trying to set env vars from file located on volumeMount (initContainer pulls the file from remote location puts on path /mnt/volume/ - custom solution) during container start, but it doesn't work.

- name: {{ .Chart.Name }}
      securityContext:
        {{- toYaml .Values.securityContext | nindent 12 }}
      image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
      imagePullPolicy: {{ .Values.image.pullPolicy }}
      command:
      - /bin/sh
      - -c
      - "echo 'export $(cat /mnt/volume/.env | xargs) && java -jar application/target/application-0.0.1-SNAPSHOT.jar"

and this doesn't work, than I tried simple export command.

      command:
  - /bin/sh
  - -c
  - "export ENV_TMP=tmp && java -jar application/target/application-0.0.1-SNAPSHOT.jar"

and it doesn't work either, also I tried to set appending .bashrc file, but still it doesn't work.

I am not sure how to handle this.

Thanks

EDIT: typo


Solution

  • By default there is no shared storage between the pod containers, so first step is checking if you have a mounted volume in the two pods, one of the best solutions is an emptyDir.

    Then you can write a variable in the init container and read it from the main container:

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pd
    spec:
      initContainers:
      - name: init-container
        image: alpine
        command:
        - /bin/sh
        - -c
        - 'echo "test_value" > /mnt/volume/var.txt'
        volumeMounts:
        - mountPath: /mnt/volume
          name: shared-storage
      containers:
      - image: alpine
        name: test-container
        command:
        - /bin/sh
        - -c
        - 'READ_VAR=$(cat /mnt/volume/var.txt) && echo "main_container: ${READ_VAR}"'
        volumeMounts:
        - mountPath: /mnt/volume
          name: shared-storage
      volumes:
      - name: shared-storage
        emptyDir: {}
    

    Here is the log:

    $ kubectl logs test-pd
    main_container: test_value