Kubernetes puzzle: Populate environment variable from file (mounted volume)

5/17/2019

I have a Pod or Job yaml spec file (I can edit it) and I want to launch it from my local machine (e.g. using kubectl create -f my_spec.yaml)

The spec declares a volume mount. There would be a file in that volume that I want to use as value for an environment variable.

I want to make it so that the volume file contents ends up in the environment variable (without me jumping through hoops by somehow "downloading" the file to my local machine and inserting it in the spec).

P.S. It's obvious how to do that if you have control over the command of the container. But in case of launching arbitrary image, I have no control over the command attribute as I do not know it.

apiVersion: batch/v1
kind: Job
metadata:
  generateName: puzzle
spec:
  template:
    spec:
      containers:
      - name: main
        image: arbitrary-image
        env:
        - name: my_var
          valueFrom: <Contents of /mnt/my_var_value.txt>
        volumeMounts:
        - name: my-vol
          path: /mnt
      volumes:
      - name: my-vol
        persistentVolumeClaim:
          claimName: my-pvc
-- Ark-kun
kubernetes
volumes

1 Answer

5/18/2019

You can create deployment with kubectl endless loop which will constantly poll volume and update configmap from it. After that you can mount created configmap into your pod. It's a little bit hacky but will work and update your configmap automatically. The only requirement is that PV must be ReadWriteMany or ReadOnlyMany (but in that case you can mount it in read-only mode to all pods).

apiVersion: v1
kind: ServiceAccount
metadata:
  name: cm-creator
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: cm-creator
rules:
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create", "update", "get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cm-creator
  namespace: default
subjects:
- kind: User
  name: system:serviceaccount:default:cm-creator
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: cm-creator
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cm-creator
  namespace: default
  labels:
    app: cm-creator
spec:
  replicas: 1
  serviceAccountName: cm-creator
  selector:
    matchLabels:
      app: cm-creator
  template:
    metadata:
      labels:
        app: cm-creator
    spec:
      containers:
      - name: cm-creator
        image: bitnami/kubectl
        command:
        - /bin/bash
        - -c
        args:
        - while true; 
            kubectl create cm myconfig --from-file=my_var=/mnt/my_var_value.txt --dry-run -o yaml | kubectl apply -f-;
            sleep 60;
          done
        volumeMounts:
        - name: my-vol
          path: /mnt
          readOnly: true
      volumes:
      - name: my-vol
        persistentVolumeClaim:
          claimName: my-pvc
-- Vasily Angapov
Source: StackOverflow