alltej
alltej

Reputation: 7285

Does helm upgrade runs a Job post-install even if no update to values.yaml?

I have scripts that are mounted to a shared persistent volume. Part of the main Deployment chart is to run some bash scripts in the initContainers that will clone the scripts repository and copy/mount it to the shared persistent volume. My issue is sometimes there will be no changes in the main app or no update to the values.yaml file, so no helm upgrade will actually happen. I think this is fine but what I want to happen is have a task that will still clone the scripts repository and copy/mount it to the persistent volume.

I am reading about k8s Job (post-install hook) but I am not sure if this will accomplish what I need.

Upvotes: 0

Views: 5462

Answers (1)

Mr.KoopaKiller
Mr.KoopaKiller

Reputation: 3962

Since you are not changed anything in HELM side like values or spec/templates, the HELM will not perform any change. In this case your code is a external source and looking by HELM perspective it is correct.

I can propose some alternatives to achieve what you want:

  1. Use HELM with FORCE flag Use helm upgrade --force to upgrade your deployment. By Helm docs:

    --force - force resource updates through a replacement strategy

In this case Helm will recreate all resources of your chart, consequently the pods, and then re-run initContainers executing your script again.

  1. Use a Kubernetes CronJob In this case you will spawn a pod that will mount your volume and run a script/command you want.

Example of a Kubernetes CronJob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: nice-count
spec:
  schedule: "*/2 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: nice-job
            image: alpine
            command: ['sh', '-c', 'echo "HelloWorld" > /usr/share/nginx/html/index.html']
            volumeMounts:
              - mountPath: "/usr/share/nginx/html"
                name: task-pv-storage
          volumes:
            - name: task-pv-storage
              persistentVolumeClaim:
                claimName: task-pv-claim
          restartPolicy: Never

In this example, a CronJob will run each 2 hours, mounting the volume task-pv-storage on /usr/share/nginx/html and executing the command echo "HelloWorld" > /usr/share/nginx/html/index.html.

You should trigger the CronJob mannually creating a Job with the command:

kubectl create job --from=cronjob/<CRON_JOB_NAME> <JOB_NAME>

In the example above, the command looks like this:

kubectl create job --from=cronjob/nice-count nice-count-job

  1. Execute a Job manually or using CI/CD You can execute the job directly or if you have a CI/CD solution you can create a job to run once instead use a CronJob, in this case you should use this template:
apiVersion: batch/v1
kind: Job
metadata:
  name: nice-count-job
spec:
  template:
    spec:
      containers:
      - image: alpine
        name: my-job
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: task-pv-storage
        command:
        - sh
        - -c
        - echo "hello" > /usr/share/nginx/html/index.html
      restartPolicy: Never
      volumes:
      - name: task-pv-storage
        persistentVolumeClaim:
          claimName: task-pv-claim

I've tested this examples and works in both cases.

Please let me know if that helped!

Upvotes: 1

Related Questions