John
John

Reputation: 11831

How to set default value for Kubernetes ConfigMap (or does Helm overwrite ConfigMap values?)

Use case:

I want to be able to re-run a job from where the first job left off. I am using Helm to deploy into Kubernetes.

I have the idea of saving the state of the first job in a ConfigMap. The ConfigMap yaml defining the ConfigMap is packaged up with the job and both are deployed at the same time with Helm.

apiVersion: v1
kind: ConfigMap
metadata:
  name: NameOfMyConfigMap
data:
  someKey: someValue
  MY_STATE: state      <---- See below as to whether this should be included or not

The job is run with an ENV variable set from the ConfigMap:

env:
  - name: MY_STATE
    valueFrom:
      configMapKeyRef:
        name:  NameOfMyConfigMap
        key: MY_STATE

The job runs a script that looks to see if $MY_STATE is set and if it is not set then the job is being run for the first time, otherwise the job closes down the already running first job, saves the first job's state into the MY_STATE ConfigMap variable and launches the job again using the saved state.

If I don't declare the MY_STATE key in the initial ConfigMap definition then the first run of the job will fail, as the ENV definition above cannot find the ConfigMap variable.

If I do declare the value (MY_STATE: "") in the ConfigMap definition, then the first deployment will work. However, if I re-deploy the job with helm upgrade then does the value I enter in the definition not overwrite an existing value in the existing ConfigMap?

What is the best method of storing state in between runs of the same job?

Upvotes: 3

Views: 3667

Answers (1)

Javier Salmeron
Javier Salmeron

Reputation: 8825

Have you tried using volumes? In this case it should not be overwritten when using helm upgrade.

Could an example like this work? (From https://groups.google.com/forum/#!msg/kubernetes-users/v2806ezEdPk/1geJCO8-AQAJ)

apiVersion: batch/v1
kind: Job
metadata:
  name: keystore-configmap-job
spec:
  template:
    metadata:
      name: keystore-configmap
    spec:
      containers:
        - name: keystore
          image: ubuntu
          volumeMounts:
            - name: keystore-configmap-volume
              mountPath: /config-base64
          command: [ "sh", "-c", "cat /config-base64/keystore.jks | base64 --decode | sha256sum" ]

      restartPolicy: Never

      volumes:
      - name: keystore-configmap-volume
        configMap:
          name: keystore-configmap

Upvotes: 2

Related Questions